有时候我们希望保存一个图像质量是高质量的。可是却出现了
ExternalException (0x80004005) A generic error occurred in GDI+
下面是代码
string filePath = Server.MapPath("~/...");//你的图片地址
Bitmap bmp = new Bitmap(filePath);
string htmlColor= "#"+Request.QueryString["color"];
Color newColor = System.Drawing.ColorTranslator.FromHtml(htmlColor);
IntRange red= new IntRange(217,217);
IntRange green = new IntRange(20,20);
IntRange blue = new IntRange(31,31);
ColorFiltering colorFilter = new ColorFiltering(red, green, blue);
colorFilter.FillOutsideRange = false;
colorFilter.FillColor = new AForge.Imaging.RGB(newColor);
Bitmap bitmap = colorFilter.Apply(AForge.Imaging.Image.Clone(bmp,PixelFormat.Format32bppRgb));
/*图片高质量保存*/
Encoder enc = Encoder.Quality;
EncoderParameters eps = new EncoderParameters(1);
EncoderParameter ep = new EncoderParameter(enc, 80L);
eps.Param[0] = ep;
ImageCodecInfo codecInfo = ImageCodecInfo.GetImageEncoders()[0];
MemoryStream stream = new MemoryStream();
/*如果直接保存的Reponse.OutputStream时候我们发现有一个错误ExternalException (0x80004005) A generic error occurred in GDI+
但是我发现直接保存成文件的时候却没有这个问题,后面就将这个放到MemoryStream再放到outputStream就正常了
*/
*/
//bmp.Save(Response.OutputStream, codecInfo, eps);
//bmp.Save(Response.OutputStream, ImageFormat.Bmp);
//bmp.Save(Response.OutputStream, ImageFormat.Bmp);
bitmap.Save(stream,codecInfo,eps); stream.WriteTo(Response.OutputStream);
Response.ContentType = "image/jpeg";
Response.End();
Response.ContentType = "image/jpeg";
Response.End();