user2326995
user2326995

Reputation: 151

A generic error occured in GDI+ - saving bitmap to desktop

I've created a fractal drawing program, and I've tried to incorporate a button in to save the current bitmap. I use a mandelbrot() method, but the button & code doesn't seem to work it just throws the error. Here's the code from drawing & saving the bitmap:

I thought it'd be because of the folder permissions, so I added the code to save it to the desktop of the current user. But still the same error.

Drawing the bitmap

        private Bitmap picture;
        private Graphics g1;

private void Form1_Paint(object sender, PaintEventArgs e)
        {
            //put the bitmap on the window
            Graphics windowG = e.Graphics;
            windowG.DrawImageUnscaled(picture, 0, 0);
        }

Saving the bitmap

private void savefractal_Click(object sender, EventArgs e)
        {
            try
            {              
               string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
               picture.Save(path);
               savefractal.Text = "Saved file.";

            }
            catch (Exception)
            {
                MessageBox.Show("There was a problem saving the file." +
                    " Check the file permissions.");
            }
        }

Upvotes: 0

Views: 173

Answers (1)

Dai
Dai

Reputation: 155598

string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
picture.Save(path)

That's not a filename, you're passing in the path of a directory, which won't work.

Use Path.Combine:

String fileName = Path.Combine( Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "MyBitmap.bmp" );
picture.Save( fileName );

Upvotes: 2

Related Questions