9

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Tony
  • 487
  • 3
  • 9
  • 12

2 Answers2

15

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(@"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; 
Mozart
  • 2,117
  • 2
  • 20
  • 38
Tom Bushell
  • 5,865
  • 4
  • 45
  • 60
  • 4
    This works great for controls with no child controls, but when the control has child controls, `DrawToBitmap` draws the children in reverse the 'z' order (meaning behind controls are drawn in front of the real front controls). – Zach Johnson Dec 02 '09 at 23:25
  • 1
    cant find drawtobitmap method – Sujit.Warrier Aug 19 '16 at 08:36
1

You could try this, this works for me.

I used 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();
D J
  • 845
  • 1
  • 13
  • 27
Leinad
  • 111
  • 10