解决A generic error occurred in GDI+的问题

DNN Fck editor中的上传图片没有改变size的功能感觉很不方便,所以准备加一个改变图像size的功能。

因为不想改变UploadFile()这个DNN core method所以准备只是改动了fck editor 上传的页面
改动后的工作流程是:
1. 选择图片上传,调用DNN UploadFile()上传图片
2. 读取上传到server的file 另存为一个临时的Image
3. 调用GDI+ GetThumbnailImage改变Image尺寸覆盖原来的文件

问题随之而来,出现在第三步上运行到存储的时候总是 出现A generic error occurred in GDI+

最后发现原因是:
To retain access to the source bits, GDI+ locks any source file, and forces the application to maintain the life of any source stream, for the life of the Bitmap or the Image object.

解决的方法是创建Index or Non-Indexed Image , 用Graphics.DrawImage() 来copy Image到一个新建立的Bitmap 对象然后多这个新的Image 执行Resize

Sample Code:

 1 Bitmap bm1 = (Bitmap)Image.FromFile( " doodaa.gif " );
 2 Bitmap bm2 = new  Bitmap(bm1.Width,bm1.Height);
 3 Graphics g = Graphics.FromImage(bm2);
 4 g.DrawImageUnscaled(bm1, 0 , 0 );
 5 // bm2 now contains a non-indexed version of the image.
 6 // Now draw the X..
 7 g.DrawLine(blah - blah.);
 8 g.DrawLine(blah - blah.);
 9 // get rid of the graphics
10 g.Dispose();
11 // and save a new gif
12 bm2.Save( " foobar.gif " ,ImageFormat.Gif);
13


这里是微软的KB对这个问题的叙述:
http://support.microsoft.com/?id=814675

转载于:https://www.cnblogs.com/love-summer/archive/2011/12/01/2270089.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值