.comment-link {margin-left:.6em;}


Thursday, November 25, 2004

Resizing the background picture of a MDI parent window

If you have a picture as background of your MDI, that picture is not resized when you resize your form. There seems no easy way (like a property) to accomplish this.

So I wrote a little piece of code that will create a new image starting from the original background image, but with the dimensions of the main form.

Declare a global variable for your form:
Private myBackground As Image

Add this code to the Resize-event of your form:
If myBackground Is Nothing Then
myBackground = Me.BackgroundImage
End If

If Me.ClientSize.Width = 0 Then Exit Sub 'Remark1

Dim bmp As New Bitmap(Me.ClientSize.Width, Me.ClientSize.Height, Imaging.PixelFormat.Format24bppRgb) 'Remark2
Dim gr As Graphics = Graphics.FromImage(bmp)
gr.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
gr.DrawImage(myBackground, 0, 0, bmp.Width, bmp.Height)
Me.BackgroundImage = bmp

If you have a statusbar (called myStatus in this example) on the bottom of your form, alter the line marked with 'Remark1 to
If Me.ClientSize.Width = 0 OrElse Me.ClientSize.Height <= Me.myStatus.Height Then Exit Sub
and change the line marked with 'Remark2 to
Dim bmp As New Bitmap(Me.ClientSize.Width, Me.ClientSize.Height - Me.myStatus.Height, Imaging.PixelFormat.Format24bppRgb)


  • Guys thank you very much for code you gave me.
    It Worked out.

    By Anonymous Anonymous, at 10:00 AM  

  • You're welcome. I appreciate your comment!

    By Blogger kHSw, at 6:17 PM  

  • Thanks a lot for this great piece of code :)

    By Blogger Hani Gamal, at 11:19 AM  

  • This comment has been removed by a blog administrator.

    By Blogger Hani Gamal, at 11:20 AM  

  • thanks thanks thanks!

    By Anonymous Anonymous, at 4:47 PM  

  • VERY NICE!!!

    It worked perfectly! ;)

    By Anonymous Anonymous, at 1:35 PM  

  • Worked for me, too. Several other code samples I found by googling didn't work, and I'd almost given up and started down the path of figuring it out for myself. You saved me a few hours. Thanks.

    By Anonymous Anonymous, at 10:55 PM  

  • Thanks for the code. I will bookmark your site. It is rare that code i find on the internet actually works.

    By Anonymous Anonymous, at 6:17 PM  

  • Thanks! Nice to know people find my blog useful!

    By Blogger kHSw, at 11:09 PM  

Post a Comment

Links to this post:

Create a Link

<< Home

Stefanie Worm is het liefste vrouwtje van de wereld.
Melina is de liefste schatsie van de wereld (Erik De Maeyer).