简体   繁体   中英

How can I fix this GDI+ generic exception when I save images?

How can I solve this GDI generic exception?

Here is the exception:

System.Runtime.InteropServices.ExternalException was unhandled
HResult=-2147467259
Message=A generic error occurred in GDI+.
Source=System.Drawing
ErrorCode=-2147467259

Code:

public Bitmap resize(string FileName)
{
  string[] settings;
  string inputFolder = "";
  string qrFolder = "";
  string generalFolder = "";
  string archiveFolder = "";
  string resizedArchiveFolder ="";
  string line;
  int index = 0;
  settings = System.IO.File.ReadAllLines("config.txt");

  foreach (string setting in settings)
  {//access to config file info
    var arr = setting.Split('=');
    if (index == 0)
      inputFolder = arr[1];
    else if (index == 1)
      qrFolder = arr[1];
    else if (index == 2)
      generalFolder = arr[1];
    else if (index == 3)
      resizedArchiveFolder = arr[1];
    else
      archiveFolder = arr[1];

    index++;
  }

  string targetPath = resizedArchiveFolder;
  if (!System.IO.Directory.Exists(targetPath))
  {
    System.IO.Directory.CreateDirectory(targetPath);
  }

  Bitmap a2 = (Bitmap)Image.FromFile(FileName);

  //load file
  a2 = new Bitmap(a2, new Size(a2.Width * 3 / 2, a2.Height * 3 / 2));
  a2.SetResolution(1920, 1080);
  a2.Save(resizedArchiveFolder + System.IO.Path.GetFileName(FileName)); 

  // it throws here when I save
  return a2;
}

Loading a bitmap puts a lock on the file. Trying to save another image to that same file will fail with this exception. You'll need to do this properly, disposing bitmaps is a hard requirement:

  Bitmap newa2 = null;
  using (var a2 = (Bitmap)Image.FromFile(FileName)) {
      newa2 = new Bitmap(a2, new Size(a2.Width * 3 / 2, a2.Height * 3 / 2));
      newa2.SetResolution(1920, 1080);
  }
  newa2.Save(Path.Combine(resizedArchiveFolder, Path.GetFileName(FileName))); 
  return newa2;

The using statement ensures that the bitmap is disposed and the file will be no longer locked. The SetResolution() arguments are nonsense btw.

If you still have trouble then there's another (invisible) line of code somewhere in your program that uses the bitmap in the resizedArchiveFolder . It could well exist in another program, like an image viewer.

I've had a similar issue before. Maybe the solution that worked for me will work for you?

My problem was that the Bitmap object was "in use", though I got the exact same exception you are receiving... just a general exception.

So just before saving I created a new Bitmap object Bitmap saveBitmap = new Bitmap(theBitmapYouwereTryingToSaveBefore); (pass the bitmap you've been working on there...) and just called Save() on this new object instead.

a2.SetResolution(1920, 1080);

This is an issue. You should read up the documentation on this function. It is certainly not what you think it is.

You are thinking it is the dimensions of the image but it is actually the DPI of the image. The standard DPI is 90. The DPI is used for you to define the actual size of your image in real world measurements.

You don't need to set the DPI in this case. I would remove that line.

I don't think this is the cause of your exception though.

EDIT:

Your code doesn't throw an error for me. It's either an issue with the image itself (have you tried another image instead?) or an issue with the path you are trying to save to (have you tried saving another file to the same path?).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM