如何将Winforms面板的图形内容保存到文件中? [英] How do I save a Winforms panel's drawing content to a file?
问题描述
我制作了一个绘画程序,并且在面板上绘制了绘图内容(来自System.Drawing)。我现在尝试使用此方法进行简单保存,但只得到空白图像。
I made a paint program, and the drawing content (from System.Drawing) is drawn on the panel. I attempted this method to do a simple save for now, and I only get a blank image.
我的位图的属性.RawData为0。不知道这是否重要。
My bitmap has its property .RawData to 0. Don't know if that matters.
当我隐藏屏幕,然后再次显示,面板变为空白。
When I hide the screen, and show it again, the panel becomes blank.
另一方面,当我调用面板的pnlPaint.Refresh()时,面板将变为空白。图纸丢失了。这是双重缓冲吗,就像不保留值一样?
On a side note, when I call the panel's pnlPaint.Refresh(), the panel goes blank. The drawing is lost. Is this a double buffer thing, like it's not retaining the values?
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;
}
推荐答案
这是一个最小的Doodle程序
This is a minimal doodle program which lets you draw persistent lines:
List<Point> curPoints = new List<Point>();
List<List<Point>> allPoints = new List<List<Point>>();
private void pnlPaint_MouseDown(object sender, MouseEventArgs e)
{
if (curPoints.Count > 1)
{
// begin fresh line or curve
curPoints.Clear();
// startpoint
curPoints.Add(e.Location);
}
}
private void pnlPaint_MouseUp(object sender, MouseEventArgs e)
{
if (curPoints.Count > 1)
{
// ToList creates a copy
allPoints.Add(curPoints.ToList());
curPoints.Clear();
}
}
private void pnlPaint_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button != MouseButtons.Left) return;
// here we should check if the distance is more than a minimum!
curPoints.Add(e.Location);
// let it show
pnlPaint.Invalidate();
}
private void pnlPaint_Paint(object sender, PaintEventArgs e)
{
// here you can use DrawLines or DrawCurve
// current line
if (curPoints.Count > 1) e.Graphics.DrawCurve(Pens.Red, curPoints.ToArray());
// other lines or curves
foreach (List<Point> points in allPoints)
if (points.Count > 1) e.Graphics.DrawCurve(Pens.Red, points.ToArray());
}
private void btn_undo_Click(object sender, EventArgs e)
{
if (allPoints.Count > 1)
{
allPoints.RemoveAt(allPoints.Count - 1);
pnlPaint.Invalidate();
}
}
private void btn_save_Click(object sender, EventArgs e)
{
string fileName = @"d:\test.bmp";
Bitmap bmp = new Bitmap(pnlPaint.ClientSize.Width, pnlPaint.ClientSize.Width);
pnlPaint.DrawToBitmap(bmp, pnlPaint.ClientRectangle);
bmp.Save(fileName, ImageFormat.Bmp);
}
添加您的保存代码,如果您有任何疑问,请直接说。.
Add your save code and if you have problems just say so..
更新::我添加了两个代码段,它们分别进行保存和(无限)撤消操作。
Update: I have added two code pieces which do a save and an (unlimited) undo..
注1:确保使用DoubleBiffered控件:PictureBox或Label或带有 DoubleBuffered 的面板
Note 1: Make sure to use a DoubleBiffered Control: Either a PictureBox or a Label or maybe a Panel with DoubleBuffered on.
注2:这不支持单击创建点。由于没有 DrawPoint
,如果需要的话,就必须解决:
Note 2: This doesn't support single clicks to create Points. As there is no DrawPoint
anyway one would have to workaround if needed:
- 要么加一个点当
MouseUp
仅在当前曲线中有一个点时,距离1个像素; - 或允许单个画点并添加
FilleCircle
将其显示在Paint
事件中。
- Either add an extra point 1 pixel away when upon
MouseUp
only one points is in the current curve; - or allow single paoints and add a
FilleCircle
to display them in thePaint
event.
这篇关于如何将Winforms面板的图形内容保存到文件中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!