Reputation: 627
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.
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.
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;
}
Upvotes: 2
Views: 3190
Reputation: 54433
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..
Note 1: Make sure to use a DoubleBiffered Control: Either a PictureBox or a Label or maybe a Panel with DoubleBuffered on.
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
only one points is in the current curve;FilleCircle
to display them in the Paint
event.Upvotes: 2
Reputation: 3358
Head over this code ScreenToGif on GitHub.
There is an implementation in the folder GifRecorder\Controls\FreeDrawPanel.cs
, it suports square and round brushes, eraser and saving the output image.
Upvotes: 0
Reputation: 1835
I would skip using the panel it's not designed for graphics as much as the ImageBox is - move on to that and then you can save the contents easily.
UPDATE PictureBox. I haven't used WinForms for a while :D
Upvotes: -1