Adding and Auto Sizing the background image of a MDI Form in .NET Some months back I faced a problem of resizing an image on one of my tablet PC device applications. The image was on a Multiple Document Interface (
MDI) form. Recently only I remembered this and did some research on this, and thought that better to put an entry.
People who have tried should be knowing that we cannot simply add an image to a form and make it automatically re size when the form changes it's size.
In a normal form we can put a picture box and make it fill the entire screen and can set the image properties to always stretch the image. This will re size the background image when ever you re size the form.
But in
MDI forms this is not possible. Whatever control you put on a
MDI form will appear on top of all other controls, hiding even the child forms that are open. I have found two elegant methods which can be used.
Method 1 -
Insert the following code into the
MDI form code. Here we are using Paint event of the
MDI form.
Private Sub frmMDI_Paint(
ByVal sender As Object,
ByVal e As System.Windows.Forms.PaintEventArgs) Handles
MyBase.Paint
' Always the
MDI form will be the last control in the control collection. The following will take the
MDI forms reference so we can access the properties of it.
Dim
varControl As Control = Me.Controls(Me.Controls.Count - 1)
' Loading the background image required to place on the
MDI.
Dim
varImage As Image = Image.
FromFile("E:\Projects\Tablet\Arjuna\Images\Background-
Landsc.
jpg")
'
Resize the image and assign it to the
MDI form background.
varControl.
BackgroundImage = New Bitmap(
varImage, Me.Width, Me.Height)
End Sub
Method 2 -
Insert the following code into the
MDI form code. Here we are using
Resize event of the
MDI form.
Private Sub
frmMDI_
Resize(
ByVal sender As Object,
ByVal e As System.
EventArgs) Handles
MyBase.
Resize' Loading the background image required to place on the
MDI.
Dim
varImage As Image = Image.
FromFile("E:\Projects\Tablet\Arjuna\Images\Background-
Landsc.
jpg")
' If there are no space, no point of drawing so exit the sub procedure.
If Me.ClientSize.Width = 0
OrElse Me.ClientSize.Height <= Me.StatusBar1.Height Then Exit Sub
' Creating the bitmap image which is used as the background image.
Dim
varBitmap As New Bitmap(Me.ClientSize.Width, Me.ClientSize.Height
- Me.StatusBar1.Height, Imaging.PixelFormat.Format24bppRgb)
' Getting the drawing surface of the bitmap (
varBitmap).
Dim
varGraphics As Graphics = Graphics.
FromImage(
varBitmap)
' Sets the interpolation mode of the graphics object. Interpolation mode determines how intermediate values between two endpoints are calculated
varGraphics.
InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
' Draw the image on the bitmap.
varGraphics.
DrawImage(
varImage, 0, 0,
varBitmap.Width,
varBitmap.Height)
' Assign the bitmap as the background image of the
MDI form.
Me.
BackgroundImage =
varBitmapEnd Sub
If you don't have a status bar in your
MDI form just remove the codes that are coloured
Magenta.
Method 1 does have some problems like, it is visible when the background image re sizes and also I found that if you make the form small sometimes the background image will not be re sized.
So I prefer Method 2.