Background image resize

madcrow74

Member
Joined
Jul 1, 2003
Messages
15
Location
Italy
ive assigned a background image to a MDI form.
When i resize the form, the image retain the same size, but I want it to resize with the form.

ive tried in the Resize event this code:

Code:
   Graphic g = this.CreateGraphic();
   g.DrawImage(img, this.ClientRectangle);

but it doesnt works...

How can I do?
 
C#:
		Bitmap myBitmap = null;
		MdiClient client = null;

		private void Form1_Load(object sender, System.EventArgs e)
		{
			myBitmap = new Bitmap(@"c:\windows\coffee Bean.bmp");

			SetStyle(ControlStyles.ResizeRedraw, true);

			foreach(Control c in Controls)
			{
				if (c is MdiClient)
				{
					client = (MdiClient)c;
					break;
				}
			}
			if (client != null)
				client.Paint += new PaintEventHandler(PaintMdiClient);
		}

		private void PaintMdiClient(object sender, PaintEventArgs e)
		{
			e.Graphics.DrawImage(myBitmap, client.Bounds);
		}
 
GREAT!

But can you explain me two things?
The MDIClient control is part of the Form or is the Form itself?
What the "@" does? Instead of the Bitmap, can I use an Image loaded from a resource file?

TY :-)
 
The MDIClient is the part of the form which can hold children; an MDI window is made of two parts: the main window, same as all the other windows, and an MDI client window, which is the part with the (usually) gray background that can hold other forms.

In C#, preceding a string with @ tells it not to process escape codes (they are in this format: \x where x can be a number of different things). Without it, divil would have had to change the path to "C:\\windows\\coffee bean.bmp", because \\ is the escape code for \. However, telling it not to look at escape codes allows him to use \ without error.

To use an image from a resource file:
C#:
private Bitmap GetResourceImage(String name) {
        return new Bitmap(System.Reflection.Assembly.GetExecutingAssembly.GetManifestResourceStream(name));
}
Now as long as youve marked the image you want in your resource file as "Embedded Resource" (for Build Action), you can replace the line
C#:
myBitmap = new Bitmap(@"c:\windows\coffee Bean.bmp");
with this:
C#:
myBitmap = GetResourceImage("MyNamespaceName.ImageIdentifier");
 
Last edited by a moderator:
If I would not use the foreach, can I find the MDIClients index with a foreach, delete the foreach and permanently set the controls index to the found index?
 
Im actually trying to do something similar, except Im drawing a gradient across the MDI client area. The relevant code:

[VB]
Dim cli as MDIClient

Private Sub Form1_Load(.....)
Dim ctrl as Control
For Each ctrl in Me.Controls
If TypeOf (ctrl) Is MDIClient Then
cli = ctrl
AddHandler cli.Paint, AddressOf cli_Paint
End If
Next
End Sub

Private Sub cli_Paint(ByVal sender as Object, ByVal e as PaintEventArgs)

Dim G as Graphics = e.Graphics
Dim br As New System.Drawing.Drawing2D.LinearGradientBrush(New Point(0, 0), _
New Point(Me.Width, Me.Height), Color.Azure, Color.DarkBlue)
G.FillRectangle(br, e.ClipRectangle)
G.Dispose()

End Sub
[/VB]

My problem is with getting rid of the flicker when the form is resized. Ive tried putting these lines in the forms constructor:

[VB]
SetStyle(ControlStyles.DoubleBuffer, True)
SetStyle(ControlStyles.UserPaint, True)
SetStyle(ControlStyles.AllPaintingInWmPaint, True)
SetStyle(ControlStyles.ResizeRedraw, True)
[/VB]

If I comment out the last line, it doesnt flicker, but it doesnt redraw the gradient for the new client size, either. I thought about adding another handler for cli.Resize, but the Event Argument that gets passed doesnt contain a graphics object.

Does anyone know of a way to get rid of resizing flicker in this situation?
 
Last edited by a moderator:
Back
Top