Saving a panel as an image

I am making this drawing application. It's simple. It consists of a panel on which I will draw, and then, finally, I save the JPG or BMP or PNG file.

My application works fine, but the problem I am facing is that when I save the output, this is not what is drawn in the panel with a black image, but just black.

all my work is saved as

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

and on the mouse (down, up) I

snapshot = (Bitmap)tempDraw.Clone();

and it kept working fine, but again rsult is black. Image does not contain a panel.

+5
source share
2 answers

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

" DrawToBitmap" - .

, , "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(@"D:\TestDrawToBitmap.bmp", ImageFormat.Bmp);
        Be aware of saving directly to the C directly as this is not 
        permitted with newer versions of window, try using SaveFileDialog.
    SaveFileDialog sf = new SaveFileDialog();
    sf.Filter = "Bitmap Image (.bmp)|*.bmp|Gif Image (.gif)|*.gif|JPEG Image (.jpeg)|*.jpeg|Png Image (.png)|*.png|Tiff Image (.tiff)|*.tiff|Wmf Image (.wmf)|*.wmf";
    sf.ShowDialog();
    var path = sf.FileName; 
+9

, , MemoryStream.

MemoryStream ms = new MemoryStream();
Bitmap bmp = new Bitmap(panel1.Width, panel1.Height);
panel1.DrawToBitmap(bmp, new System.Drawing.Rectangle(0, 0, panel1.Width, panel1.Height));
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); //you could ave in BPM, PNG  etc format.
byte[] Pic_arr = new byte[ms.Length];
ms.Position = 0;
ms.Read(Pic_arr, 0, Pic_arr.Length);
ms.Close();
0

All Articles