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

浪子不回头ぞ 提交于 2019-12-11 01:38:42

问题


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;
}

回答1:


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.




回答2:


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.




回答3:


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?).



来源:https://stackoverflow.com/questions/20010789/how-can-i-fix-this-gdi-generic-exception-when-i-save-images

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!