错误的代码g对象继续占用 未释放资源 如果路径不一样 没问题 相同路径 获取图片进行

缩略会造成GDI错误

 /// <summary>
/// 生成缩略图
/// </summary>
/// <param name="originalImagePath">源图路径(物理路径)</param>
/// <param name="thumbnailPath">缩略图路径(物理路径)</param>
/// <param name="width">缩略图宽度</param>
/// <param name="height">缩略图高度</param> public void CreateMinImage(string originalImagePath, string thumbnailPath, int width, int height)
{
System.Drawing.Image originalImage = System.Drawing.Image.FromFile(HttpContext.Current.Server.MapPath(originalImagePath)); int towidth = width;
int toheight = height; int ow = originalImage.Width;
int oh = originalImage.Height; //新建一个bmp图片
System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight); //新建一个画板
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap); //设置高质量插值法
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //设置高质量,低速度呈现平滑程度
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; //清空画布并以透明背景色填充
g.Clear(System.Drawing.Color.Transparent); //在指定位置并且按指定大小绘制原图片的指定部分
g.DrawImage(originalImage, new System.Drawing.Rectangle(, , towidth, toheight)); try
{
//以jpg格式保存缩略图
string FileExt = Path.GetFileNameWithoutExtension(originalImagePath);
//这里报错 bitmap.Save(HttpContext.Current.Server.MapPath(thumbnailPath) + FileExt + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch (System.Exception e)
{
throw e;
}
finally
{
originalImage.Dispose();
bitmap.Dispose();
g.Dispose();
} } //修改后的代码
public void CreateMinImageAndDel(string originalImagePath, string thumbnailPath, int width, int height)
{
Graphics draw = null;
string FileExt = ""; System.Drawing.Image originalImage = System.Drawing.Image.FromFile(HttpContext.Current.Server.MapPath(originalImagePath)); int towidth = width;
int toheight = height; int ow = originalImage.Width;
int oh = originalImage.Height; //新建一个bmp图片
System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);
System.Drawing.Image bitmap2 = new System.Drawing.Bitmap(towidth, toheight);
//新建一个画板
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap); //设置高质量插值法
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //设置高质量,低速度呈现平滑程度
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; //清空画布并以透明背景色填充
g.Clear(System.Drawing.Color.Transparent); //在指定位置并且按指定大小绘制原图片的指定部分
g.DrawImage(originalImage, new System.Drawing.Rectangle(, , towidth, toheight)); try
{
//以jpg格式保存缩略图
FileExt = Path.GetFileNameWithoutExtension(originalImagePath);
          //用新建立的image对象拷贝bitmap对象 让g对象可以释放资源
draw = Graphics.FromImage(bitmap2);
draw.DrawImage(bitmap, , ); }
catch (System.Exception e)
{
throw e;
}
finally
{
originalImage.Dispose();
bitmap.Dispose();
g.Dispose();
          //保存调整在这里即可
bitmap2.Save(HttpContext.Current.Server.MapPath(thumbnailPath) + FileExt + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
}
}

GDI+ 发生一般性错误解决办法的更多相关文章

  1. “GDI+ 发生一般错误” 解决方法

    System.Runtime.InteropServices.ExternalException: GDI+ 发生一般错误 对应的帐户没有写权限,给予帐户写权限 版权声明:本文博主原创文章,博客,未经 ...

  2. 关于生成缩略图及水印图片时出现GDI+中发生一般性错误解决方法

    System.Drawing.Image OldImage = null; oldImage = System.Drawing.Image.FromFile(ImageUrl); 使用该方法读取图片时 ...

  3. C# GDI+发生一般性错误(A generic error occurred in GDI+))

    解决思路: 1. 因为 .net GDI+ 是对底层 的封装. 所以可以尝试用 Marshal.GetLastWin32Error();函数获得底层错误代码. try{ image.Save(file ...

  4. GDI+中发生一般性错误的解决办法(转帖)

    今天在开发.net引用程序中,需要System.Drawing.Image.Save 创建图片,debug的时候程序一切正常,可是发布到IIS后缺提示出现“GDI+中发生一般性错误”的异常.于是开始“ ...

  5. 【转】GDI+中发生一般性错误的解决办法

    今天在开发.net引用程序中,需要System.Drawing.Image.Save 创建图片,debug的时候程序一切正常,可是发布到IIS后缺提示出现“GDI+中发生一般性错误”的异常. 于是开始 ...

  6. GDI+中发生一般性错误的解决办法(转)

    今天在开发.net引用程序中,需要System.Drawing.Image.Save 创建图片,debug的时候程序一切正常,可是发布到IIS后缺提示出现"GDI+中发生一般性错误" ...

  7. GDI+中发生一般性错误的解决办法(转载)

    今天在开发.net引用程序中,需要System.Drawing.Image.Save 创建图片,debug的时候程序一切正常,可是发布到IIS后缺提示出现"GDI+中发生一般性错误" ...

  8. gdi+ 中发生一般性错误 wpf解决方法

    错误背景:原来在winform程序中写了一个窗口,在wpf应用程序中调用显示了这个窗口,有个头像功能,加载本地的一个图片文件,加载前进行了各种逻辑判断,效果如下: 而加载的关键代码如下面: pictu ...

  9. 【排除解决】System.Runtime.InteropServices.ExternalException (0x80004005): GDI+ 中发生一般性错误

    前言: 今天项目发布上线,发布到正式环境验证功能的时候忽然方向之前做的一个图片合成的功能报错了提示:System.Runtime.InteropServices.ExternalException ( ...

随机推荐

  1. MAC系统生成RSA公钥私钥

    进入openssl然后主要就是三条命令: 1.genrsa -out rsa_private_key.pem 1024 这句是生成原始私钥文件 2.pkcs8 -topk8 -inform PEM - ...

  2. Windows下Eclipse+Scala+Spark开发环境搭建

    1.安装JDK及配置java环境变量 本文使用版本为jdk1.7.0_79,过程略 2.安装scala 本文使用版本为2.11.8,过程略 3.安装spark 本文使用版本为spark-2.0.1-b ...

  3. grep sed 大批量替换字符串

    sed -i s/"str1"/"str2"/g `grep "str1" -rl --include="*.[ch]" ...

  4. 多层数据库应用基于Delphi DataSnap方法调用的实现(一)返回数据集

    从Delphi 2009开始,DataSnap技术发生了很大的变化,并在Delphi 2010和Delphi XE的后续版本中得到了持续的改进.Delphi 2009之前的DataSnap,虽然也实现 ...

  5. inotifywait命令

    [命令格式]: inotifywait [ options ] file1 [ file2 ] [ file3 ] [ ... ][命令原意]: inote file system wait[命令路径 ...

  6. sqlserver 查看正在执行sql

    SELECT    [session_id],  [request_id],  [cpu_time],  [start_time] AS '开始时间',  [status] AS '状态',  [co ...

  7. C++ MFC打开文件的流程

    打开文件的步骤如下: 弹出打开文件对话框 -> 获取选择的文件,并将文件显示在视图中. 我们程序中经常需要定制的操作如下: 1. 定制弹出的文件对话框,例如需要修改打开文件的类型或扩展名 2. ...

  8. git ssh端口号变更之后所需要的修改

    假设原本的repository地址为:git@gitlab.cjx.com:jinxin/project2.git 端口变更之后,需要调整为:ssh://git@gitlab.cjx.com:PORT ...

  9. svn:ignore eclipse开发一般忽略文件

    target.project.classpath.settings

  10. swift 之 闭包

    一.闭包 格式:{  (  参数名:类型, 参数名:类型 ..  )   in 内容体  return  返回值   }  最完整的闭包 1.省略参数类型 {  (  参数名, 参数名..  )   ...