How to draw on Control so drawing doesn't disappear?

点点圈 提交于 2019-12-12 21:23:55

问题


I want to display graphic file in PictureBox I have:

private void btnLoad_Click(object sender, EventArgs e)
{
    if (dgOpenFile.ShowDialog() == DialogResult.OK)
    {
        Bitmap img = new Bitmap(dgOpenFile.FileName);
        picture.Width = img.Height;
        picture.Height = img.Height;
        g.DrawImage(img, 0f, 0f);
    }
}

That's g

private void Form1_Load(object sender, EventArgs e)
{
    g = picture.CreateGraphics();
}

But when I move my Form outside the window my picture disappears. How can I prevent that?


回答1:


You should do any custom drawing in the OnPaint event of the control to make it persistent. This causes your drawing to be redrawn every time the control is painted.

However, in this case it would be easier to use the picture box as it was designed:

picture.Image = img;



回答2:


Windows uses a Paint-on-Request principle.

So when it sends a WM_PAINT message to your Control, it's OnPaint() is called. You should be ready to draw the image (again) in an overridden OnPaint() or in a Paint event handler.

But a Picturebox will do all this for you.



来源:https://stackoverflow.com/questions/4593290/how-to-draw-on-control-so-drawing-doesnt-disappear

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