Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hi guys,

 

I am using a background in my form, is there anyway that if the size of form changes (gets bigger) the image streches, right now my form is tiling the image.

 

I tried to set the size of image but height and width are read only properties.

 

Any Suggestions?

 

I would apreciate your help.

  • *Experts*
Posted

Not sure this is the best way to do it, but I know this works.

 

Change the constructor of your form to this (it's in the WinForms

Generated Region):

 

   Public Sub New()
       MyBase.New()

       'This call is required by the Windows Form Designer.
       InitializeComponent()

       'Add any initialization after the InitializeComponent() call
       SetStyle(ControlStyles.ResizeRedraw, True)
       setstyle(ControlStyles.AllPaintingInWmPaint, True)
       setstyle(ControlStyles.DoubleBuffer, True)
   End Sub

And then add this to your Paint event of the form:

    Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
       Dim b As Bitmap = Me.BackgroundImage
       Dim g As Graphics = e.Graphics

       g.DrawImage(b, Me.DisplayRectangle)
   End Sub

divil will probably come by and tell me how much my method sucks,

but meh; I don't know of any other way. :p

  • *Gurus*
Posted

I would have a picturebox as the background of my form, with its dock mode set to Fill so it takes up the whole form. Then any child controls can be placed on it. You can set the Image property of the picturebox to load your picture in, then the SizeMode of it to StretchImage.

 

That should do what you need.

MVP, Visual Developer - .NET

 

Now you see why evil will always triumph - because good is dumb.

 

My free .NET Windows Forms Controls and Articles

Posted
I tried picture box, but the problem is that I am using transparent labels and when i use picture box, labels show the color of the form, and Form cant be transparent. is there anyway around it?
Posted
I havent tried yours volteface, I will try tomorrow and let you know, and thank you very much guys for the help. sso in your method the image will stretch by itself or i will have to set the size properties?
  • *Experts*
Posted

With my way the image will stretch by itself in the .DrawImage

method (that second parameter is the size to draw it as, and in

this case, we're setting it to the size of the form).

  • 1 year later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...