John's Cave

DNN 探索频道
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

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

Posted on 2008-03-06 09:07  John.z  阅读(15100)  评论(1编辑  收藏  举报
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:
 1Bitmap bm1=(Bitmap)Image.FromFile("doodaa.gif");
 2Bitmap bm2=new Bitmap(bm1.Width,bm1.Height);
 3Graphics g=Graphics.FromImage(bm2);
 4g.DrawImageUnscaled(bm1,0,0);
 5//bm2 now contains a non-indexed version of the image.
 6//Now draw the X..
 7g.DrawLine(blah-blah.);
 8g.DrawLine(blah-blah.);
 9//get rid of the graphics
10g.Dispose();
11//and save a new gif
12bm2.Save("foobar.gif",ImageFormat.Gif);
13

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