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:
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