如何在 Control 上绘图以使绘图不会消失?
我想在 PictureBox 中显示图形文件,我有:
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);
}
}
那是 g
private void Form1_Load(object sender, EventArgs e)
{
g = picture.CreateGraphics();
}
但是当我将表单移到窗口之外时,我的图片就会消失。我怎样才能防止这种情况发生?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该在控件的
OnPaint
事件中执行任何自定义绘图,以使其持久化。这会导致每次绘制控件时都重新绘制绘图。然而,在这种情况下,使用设计的图片框会更容易:
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:
Windows 使用“按需绘制”原则。
因此,当它向您的控件发送 WM_PAINT 消息时,就会调用 OnPaint()。您应该准备好在重写的 OnPaint() 或 Paint 事件处理程序中(再次)绘制图像。
但是 Picturebox 可以为您完成这一切。
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.