我怎样才能解决这个GDI +通用的异常当我保存的图像? [英] How can I fix this GDI+ generic exception when I save images?

查看:261
本文介绍了我怎样才能解决这个GDI +通用的异常当我保存的图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能解决这个GDI通用的异常?

下面是个例外:

  System.Runtime.InteropServices.ExternalException了未处理
的HResult = -2147467259
消息= GDI +中发生一般性错误。
来源= System.Drawing中
错误code = -2147467259

code:

 公共位图大小调整(字符串文件名)
{
  字符串[]设置;
  字符串inputFolder =;
  字符串qrFolder =;
  字符串generalFolder =;
  字符串archiveFolder =;
  字符串resizedArchiveFolder =;
  串线;
  INT索引= 0;
  设置= System.IO.File.ReadAllLines(config.txt的);  的foreach(在设置字符串设定)
  {//获取配置文件信息
    VAR ARR = setting.Split('=');
    如果(指数== 0)
      inputFolder = ARR [1];
    否则如果(指​​数== 1)
      qrFolder = ARR [1];
    否则如果(指​​数== 2)
      generalFolder = ARR [1];
    否则,如果(指数== 3)
      resizedArchiveFolder = ARR [1];
    其他
      archiveFolder = ARR [1];    指数++;
  }  字符串TARGETPATH​​ = resizedArchiveFolder;
  如果(!System.IO.Directory.Exists(TARGETPATH​​))
  {
    System.IO.Directory.CreateDirectory(TARGETPATH​​);
  }  位图A2 =(位图)Image.FromFile(文件名);  //加载文件
  A2 =新位图(A2,新尺寸(a2.Width * 3/2,a2.Height * 3/2));
  a2.SetResolution(1920,1080);
  a2.Save(resizedArchiveFolder + System.IO.Path.GetFileName(文件名));  //在这里抛出当我保存
  返回A2;
}


解决方案

载入位图放在一个锁定的文件。尝试另一个图像保存到同一个文件将失败与此异常。你需要正确地做到这一点,处理位图是一个硬要求:

 位图NEWA2 = NULL;
  使用(VAR A2 =(位图)Image.FromFile(文件名)){
      NEWA2 =新位图(A2,新的大小(a2.Width * 3/2,a2.Height * 3/2));
      newa2.SetResolution(1920,1080);
  }
  newa2.Save(Path.Combine(resizedArchiveFolder,Path.GetFileName(文件名)));
  返回NEWA2;

使用的语句确保位图配置,文件将不再被锁定。该SetResolution()参数是无稽之谈BTW。

如果您仍然有问题再有就是code的程序中的另一个(不可见)线的地方,使用位图中的 resizedArchiveFolder 的。它很可能存在于另一个程序,就像一个图像浏览器。

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.

这篇关于我怎样才能解决这个GDI +通用的异常当我保存的图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆