0

So i made a program similar to paint and i wanted to add a save option to it, however when ever i saved the drawing/image on my panel that i created the only thing i got was a white rectangle. Do you guys know what the problem is with my code? Any help would be appreciated!

    string path;
    private void button3_Click(object sender, EventArgs e)
    {
        SaveFileDialog sfd = new SaveFileDialog();
        sfd.Filter = "Bitmap | *.bmp";
        if(sfd.ShowDialog() == DialogResult.OK)
        {
            path = sfd.FileName;
            using(var Bitmap = new Bitmap(panel1.Width, panel1.Height))
            {
                panel1.DrawToBitmap(Bitmap, new Rectangle(0,0, Bitmap.Width, Bitmap.Height));
                try
                {
                    Bitmap.Save(path);
                }
                catch(Exception ex) { MessageBox.Show(ex.Message); }
            }
        }
    } 
  • can you change the `var Bitmap` to `Bitmap image`? then `image.Save(path,System.Drawing.Imaging.ImageFormat.Bitmap);` – Cyrille Con Morales Aug 03 '20 at 05:58
  • How do you draw on the panel? Just onto the panel? Then it isn’t saved anywhere. You should already draw on a bitmap, which is shown on the screen. Drawing directly on screen is ephemeral. – Sami Kuhmonen Aug 03 '20 at 06:21
  • If you don't draw into a Bitmap in the 1st place. you MUST draw in a Paint event and store all data needed to repeat the drawing. Only then DrawToBitmap will recreate them all in a bitmap you can save.. [See here for the distinction](https://stackoverflow.com/questions/27337825/picturebox-paintevent-with-other-method/27341797?r=SearchResults&s=2|50.2008#27341797) and among [many](https://stackoverflow.com/search?q=user%3A3152130+drawaction) others [here for an example](https://stackoverflow.com/questions/28714411/update-a-drawing-without-deleting-the-previous-one/28716887#28716887) – TaW Aug 03 '20 at 07:10

0 Answers0