Tony Tony - 5 months ago 39
C# Question

Saving Panel as an Image

I'm doing this paint application. It's kind of simple. It consist of a panel where I will draw on and then finally I will save as JPG or BMP or PNG file.

My application work perfectly but the problem I'm facing is that when I'm saving the output is not what drawn on the panel its black Image nothing just black.

all my work is been saved as

Thepic = new Bitmap(panel1.ClientRectangle.Width, this.ClientRectangle.Height);

and on the mouse (down,up thing) I have

snapshot = (Bitmap)tempDraw.Clone();

and it saved the work normally but again the rsult is black Image not what the panel contain.


I think the problem may be that you're using the "Clone" method.

Try "DrawToBitmap" - that's worked for me in the past.

Here's a sample that saves a bitmap from a control called "plotPrinter":

        int width = plotPrinter.Size.Width;
        int height = plotPrinter.Size.Height;

        Bitmap bm = new Bitmap(width, height);
        plotPrinter.DrawToBitmap(bm, new Rectangle(0, 0, width, height));

        bm.Save(@"C:\TestDrawToBitmap.bmp", ImageFormat.Bmp);