Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
192 views
in Technique[技术] by (71.8m points)

c# - How do I save a Winforms panel's drawing content to a file?

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;
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:est.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:

  • 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 the Paint event.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...