Uwe Keim Uwe Keim - 1 year ago 101
C# Question

Drawing an image onto a Panel control gives artefacts when resizing

Currently I'm trying to do what I thought would be a simple task:

Draw an image onto the full area of a

control in Windows Forms. (Please ignore for the moment that I could use the

The image to draw looks like this:

enter image description here

I.e. a yellow box with an 1 pixel blue frame around.

To draw, I'm using the
event of the

private void panel1_Paint(object sender, PaintEventArgs e)
e.Graphics.DrawImage(Resources.MyImage, panel1.ClientRectangle);

This looks fine when initially displaying the form:

enter image description here

When resizing the form (and the docked panel, too), it either cuts the edges when being made smaller...

enter image description here

...or it draws artefacts, when being made larger:

enter image description here

I'm pretty sure that there is going on something rather simple and straight-forward but I really cannot understand the reason.

Since I'm ignoring the
and always draw everything, I thought the image would be scaled all the time.

My questions are:

  • What is the reason for the artefacts? (I love to understand this!)

  • What do I have to do in order to get rid of the artefacts? (beside calling
    on each resize)


Thanks to Ryan's answer, I was able to find an acceptable solution. Basically I derived a class from
, did an override of
and did not call the base method. Last, I added the following code to the constructor of my derived panel:

base.DoubleBuffered = true;

SetStyle(ControlStyles.AllPaintingInWmPaint, true);
SetStyle(ControlStyles.ResizeRedraw, true);
SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.OptimizedDoubleBuffer, true);


Answer Source

The reason for the artefacts is that the entire surface isn't redrawn when the form is resized; only the necessary parts are. The generally best solution is what you don't want to do, calling Invalidate on each resize. However, if this is in fact your situation, just use a PictureBox instead. If it's not, you might consider overriding OnPaint in your form instead, and using this.SetStyle(ControlStyles.ResizeRedraw, true) to do this automatically.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download