GDI+ Error on saving image

Any ideas why this throws a GDI+ error. If I remove the SaveDialog it works


 using (var bitmap = new Bitmap(pictureBox.Width, pictureBox.Height))
            {
                pictureBox.DrawToBitmap(bitmap, pictureBox.ClientRectangle);

                SaveFileDialog saveFileDialog1 = new SaveFileDialog();

                saveFileDialog1.Filter = "png files (*.png)|*.png";
                saveFileDialog1.RestoreDirectory = true;

                if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    if ((saveFileDialog1.OpenFile()) != null)
                    {  
                        bitmap.Save(saveFileDialog1.FileName, ImageFormat.Png);
                        MessageBox.Show("Image Export & Saved!" + System.Environment.NewLine + saveFileDialog1.FileName, Constants.ProgramName, MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }                            
                }
            }
LVL 1
JElsterAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
Ji JElster;

When posting a question please don't just post something like this, "Any ideas why this throws a GDI+ error", when you get an error post the exception and inner exception which is thrown.

I believe the issue is that a statement like this, saveFileDialog1.OpenFile(), will open the file up for you for reading and writing. Then you attempt to save the bitmap with this statement,  bitmap.Save(saveFileDialog1.FileName, ImageFormat.Png);, well the file is already open and this is what I think is causing the error. If it does not correct the issue please post the except and inner exception.

Try modifying the code as follows.

Stream myStream;

if((myStream = saveFileDialog1.OpenFile()) != null)
{  
    bitmap.Save(myStream, ImageFormat.Png);
    MessageBox.Show("Image Export & Saved!" + System.Environment.NewLine + saveFileDialog1.FileName, Constants.ProgramName, MessageBoxButtons.OK, MessageBoxIcon.Information);
}

Open in new window

0
JElsterAuthor Commented:
Hi.. I get diff exception.
ExceptionObject = {"The process cannot access the file 'C:\\Temp\\4.png' because it is being used by another process."}
0
Fernando SotoRetiredCommented:
Can you please copy the section of the code as you modified it as I have stated and paste it here so I can have a look at it.
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

JElsterAuthor Commented:
Original Error

{"A generic error occurred in GDI+."}
at System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, EncoderParameters encoderParams)
0
JElsterAuthor Commented:
private void Export_Click(object sender, EventArgs e)
        {
            using (var bitmap = new Bitmap(pictureBox.Width, pictureBox.Height))
            {
                pictureBox.DrawToBitmap(bitmap, pictureBox.ClientRectangle);

                SaveFileDialog saveFileDialog1 = new SaveFileDialog();

                saveFileDialog1.Title = "Save / Export  Photo";
                saveFileDialog1.Filter = "png files (*.png)|*.png";
                saveFileDialog1.RestoreDirectory = true;

                if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    if ((saveFileDialog1.OpenFile()) != null)
                    {

                        System.IO.Stream myStream;

                        if ((myStream = saveFileDialog1.OpenFile()) != null)
                        {
                            bitmap.Save(myStream, ImageFormat.Png);
                            MessageBox.Show("Image Export & Saved!" + System.Environment.NewLine + saveFileDialog1.FileName, Constants.ProgramName, MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }

                             }
                }
            }

        }
0
Fernando SotoRetiredCommented:
The file that you opened with the file dialog box, does it have data in it already? Or is it completely empty?
0
Fernando SotoRetiredCommented:
@Ji JElster;

I used the code you provided with the modification I asked you to make and created a test program and it did work. I did make one small modification because I had not created an object called Constants.ProgramName so I changed it to "Test Program".

When the statement to save the bitmap to a file was executed the system came back with a dialog box to Confirm Save As because a file with that name already exist and do you want to replace. I replied with Yes and the image was written to the file system without any issues.

This is the cod I used.

        private void button1_Click(object sender, EventArgs e)
        {
            using (var bitmap = new Bitmap(pictureBox.Width, pictureBox.Height))
            {
                pictureBox.DrawToBitmap(bitmap, pictureBox.ClientRectangle);

                SaveFileDialog saveFileDialog1 = new SaveFileDialog();

                saveFileDialog1.Filter = "png files (*.png)|*.png";
                saveFileDialog1.RestoreDirectory = true;

                if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    Stream myStream;
                    if ((myStream = saveFileDialog1.OpenFile()) != null)
                    {
                        bitmap.Save(myStream, ImageFormat.Png);
                        MessageBox.Show("Image Export & Saved!" + System.Environment.NewLine + saveFileDialog1.FileName, "Test Program", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }
        }

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JElsterAuthor Commented:
It works , but throws an IO error when trying to overwrite an existing file.
0
JElsterAuthor Commented:
This seemed to work

  saveFileDialog1.OverwritePrompt = false;
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.