Drawing an image onto a Panel control gives artefacts when resizing

て烟熏妆下的殇ゞ 提交于 2019-12-19 17:46:20

问题


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

Draw an image onto the full area of a Panel control in Windows Forms. (Please ignore for the moment that I could use the BackgroundImage property)

The image to draw looks like this:

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

To draw, I'm using the Paint event of the Panel control:

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

This looks fine when initially displaying the form:

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

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

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 ClipRectangle 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 Invalidate on each resize)

Update, SOLUTION:

Thanks to Ryan's answer, I was able to find an acceptable solution. Basically I derived a class from Panel, did an override of OnPaintBackground 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);

UpdateStyles();

回答1:


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.



来源:https://stackoverflow.com/questions/9751373/drawing-an-image-onto-a-panel-control-gives-artefacts-when-resizing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!