ExternalException when calling Bitmap.Save(…) in Debug build only

249 Views Asked by At

I use the following code for output of my calculations which it should be a PNG picture. I do not understand why in Debug run the last line gives me a System.Runtime.InteropServices.ExternalException. On the Release run everything is OK.

I found on the net that this error may occur when the image is being used by other part of code, but in my case it is not true.

//using System;
//using System.Drawing;
//using System.Drawing.Imaging;

Bitmap png = new Bitmap(this.xPixels, this.yPixels, PixelFormat.Format32bppArgb);
Graphics g = Graphics.FromImage(png);
g.Clear(Color.White);
g.DrawString(currentTime, myFont, mySolidBrush, timeX, timeY, myXTitleFormat);

// write image to file
string path4 = string.Concat(Environment.CurrentDirectory, @"\Output\T1\" + this.fileName + ".png");
png.Save(path4, ImageFormat.Png);
1

There are 1 best solutions below

0
On

The online help says that "The image was saved with the wrong image format".

It's also strange that you do stuff with the Graphics object, but save using the Bitmap. Do the operations made with g influence the Bitmap?

The following code raises the exception whether in debug or release mode:

       {

            Bitmap png = new Bitmap(100, 100, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
            Graphics g = Graphics.FromImage(png);
            g.Clear(Color.White);
            g.DrawString("dummy", SystemFonts.DefaultFont, Brushes.Beige, RectangleF.FromLTRB(5,5,80,80), StringFormat.GenericDefault);

            // write image to file
            string path4 = @"C:\test.png";

            png.Save(path4, System.Drawing.Imaging.ImageFormat.Png);

        }