【发布时间】:2014-11-14 17:35:10
【问题描述】:
我做了一个绘图程序,绘图内容(来自System.Drawing)被绘制在面板上。我现在尝试使用这种方法进行简单的保存,但我只得到一个空白图像。
我的位图的属性 .RawData 为 0。不知道这是否重要。
当我隐藏屏幕并再次显示时,面板变为空白。
附带说明,当我调用面板的 pnlPaint.Refresh() 时,面板变为空白。图丢了。这是一个双缓冲的东西,就像它没有保留值一样?
private bool Save()
{
Bitmap bmpDrawing;
Rectangle rectBounds;
try
{
// Create bitmap for paint storage
bmpDrawing = new Bitmap(pnlPaint.Width, pnlPaint.Height);
// Set the bounds of the bitmap
rectBounds = new Rectangle(0, 0, bmpDrawing.Width, bmpDrawing.Height);
// Move drawing to bitmap
pnlPaint.DrawToBitmap(bmpDrawing, rectBounds);
// Save the bitmap to file
bmpDrawing.Save("a.bmp", ImageFormat.Bmp);
}
catch (Exception e)
{
MessageBox.Show("Error on saving. Message: " + e.Message);
}
return true;
}
【问题讨论】:
-
实施问题属于 Stack Overflow。
-
问题出在您未发布的代码中。您绘制面板的方式是错误的。使用它的 Paint 事件,不要使用 CreateGraphics()。
-
即便如此,我还是用鼠标左键向下绘图,这需要在移动时抓取设备上下文。所以我认为最好通过在鼠标按下事件中传递它来完成。我目前还没有成功使用面板的绘画事件。
-
No. 在
MouseDown开始绘图后,MouseMove可以收集点但绘图必须 在Paint事件中发生或被触发,否则DrawToBitmap调用将一事无成。 -
好的。我已经收集了积分。鼠标移动后如何调用绘制事件? pnlPaint.Refresh()/Invalidate() 将线条保持一秒钟,直到面板再次变为空白。