0

So I'm making a paint app and I'm trying to save the paint on a Panel Control into a .Jpeg, however, my code doesn't work. It saves a .Jpeg image but it's just a full white image.

My Code:

        SaveFileDialog dialog = new SaveFileDialog();
        dialog.Filter = "Jpeg Image | *.jpeg";
        if (dialog.ShowDialog() == DialogResult.OK)
        {
            Bitmap bmp = new Bitmap(pnlPaintPanel.Width, pnlPaintPanel.Height);
            Rectangle rect = new Rectangle(0, 0, pnlPaintPanel.Width, pnlPaintPanel.Height);
            pnlPaintPanel.DrawToBitmap(bmp, rect);
            bmp.Save(dialog.FileName, ImageFormat.Jpeg);
        }

The panel is defiantly painted when I try and save, can anyone help?

Chai_Latte
  • 59
  • 4
  • 1
    _The panel is defiantly painted when I try and save, can anyone help?_ Most likely you painted onto the panel using `CreateGraphics()`? You need to instead use the `e.Graphics` supplied in the `Paint()` event...or give us more details about **HOW** you "painted" in the panel in the first place. – Idle_Mind Jun 09 '17 at 19:34
  • Yeah I used pnlPaintPanel.CreateGraphics() , and assigned it to a variable (g) – Chai_Latte Jun 09 '17 at 19:41
  • 1
    Right...you can't do that. Store the "data" about what to paint at class level and then draw it with `e.Graphics` in the `Paint()` event of your Panel. Call `pnlPaintPanel.Invalidate();` to make it refresh itself. – Idle_Mind Jun 09 '17 at 19:58
  • [You may want to study this post on how to draw properly](https://stackoverflow.com/questions/31988079/copying-free-hand-drawing-from-panel-in-visual-studio-2013/32112426?s=12|0.0863#32112426) - Once you got that working DrawToBitmap will work too.. – TaW Jun 09 '17 at 21:01
  • Thanks a lot for the advice I'll try it out! – Chai_Latte Jun 09 '17 at 21:10

0 Answers0