错误的代码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. shell脚本的执行方式

    编写好的shell脚本(如:test),可以采取两种方式进行运行: 一. $ sh test 一般不采用这种调用方式,尤其不采用"sh<test"的调用方式,因为这种方式将禁 ...

  2. CentOS6.5上安装MySQL5.6

    1.准备数据存放的文件系统 新建一个逻辑卷,并将其挂载至特定目录即可.这里不再给出过程. 这里假设其逻辑卷的挂载目录为/data,而后需要创建/data/mysqldata目录做为mysql数据的存放 ...

  3. vim编辑二进制文件

    首先,vim -b 方式打开二进制文件, 然后用 :%!xxd去展示二进制文件 再修改文件, 最后用 :%!xxd -r去还原文件原来的展示方式, 并保存退出.

  4. C语言乱谈(一) 20行代码生成BMP

    在学习图形图像的过程中,最简单和常见的格式是BMP和PPM.下面将给出生成BMP的极度精简代码,然后讲解BMP格式. #include <stdio.h> #include <std ...

  5. C# 解析json

    在接口开发的过程中经常通过接口获取数据返回是json格式字符串. 但是返回的字符串可能比较复杂,可能不止一种类型的数据. 例如: { "resultCode": "0&q ...

  6. Brophp框架开发时连接数据库读取UTF8乱码的解决(转)

    Brophp框架开发时连接数据库读取UTF8乱码的解决办法 (2012-09-15 10:41:22) 转载▼ 标签: 杂谈 it php 分类: 建站技术 Brophp框架开发时连接数据库读取UTF ...

  7. 关于apache httpd.conf脚本的理解

    新人一枚,这两天一直在研究lamp的搭建,感觉自己对apache理解的不够深彻,决定写这一篇(翻译)httpd.conf文件 未完待续 cat /usr/local/apache/conf/httpd ...

  8. C++ --- Hellowrod

    #include <iostream> int main() { ) { using namespace std; cout << "helloword"; ...

  9. 安装好grunt,cmd 提示"grunt不是内部或外部命令" 怎么办?

    Grunt和所有grunt插件都是基于nodejs来运行的,因此,必须安装node.js. (一) 去官网http://nodejs.org/ 下载安装包 node-v6.9.2.msi,直接点击安装 ...

  10. Entity Framework Code First数据库自动更新

    EF的Code First方式允许你先写Model,再通过Model生成数据库和表. 具体步骤如下: 1.建项目 2.在model文件夹中,添加一个派生自DbContext的类,和一些Model类. ...