【发布时间】:2016-07-16 16:18:21
【问题描述】:
我编写了这段代码来在我的 Winforms 应用程序中的图片框上绘制图像,我想知道是否有一种方法可以清除单个图像而不是清除所有内容。
public void DrawImage(Image img, Point pos)
{
if (GraphicsRenderer!=null)
{
GraphicsRenderer.DrawImage(img, pos);
}
}
DrawImage(Image.FromFile("image.png"), new Point(0, 0));
// then this to draw a rectangle
public void DrawRectangle(Rectangle rect, Pen pen)
{
if (GraphicsRenderer == null)
GraphicsRenderer = Renderer.CreateGraphics();
GraphicsRenderer.DrawRectangle(pen, rect);
}
DrawRectangle(new Rectangle(new Point(0, 0), new Size(40, 40)), Pens.Red);
// now how do i clear only the image?
感谢任何帮助或见解!
【问题讨论】:
-
如果
Renderer是PictureBox是一个不要操作它上面的图形。PictureBox旨在显示您加载的静态图像。如果您想在表单上绘图,只需创建一个空面板并在OnPaint事件中进行绘图并使用e.Graphics对象而不是使用CreateGraphics。