天天看点

Win7下Bitmap.Clone方法处理CMYK图片OutOfMemory异常的解决办法

Winform下的图像处理比较郁闷,动不动就蹦出这个 OutOfMemory 异常而不给具体原因。刚才谈新客户,他发给我几张jpg图片,让我处理一下给效果图出来,我用自己的图像处理程序一打开,蹦的一下,蹦出来了个 OutOfMemory 异常。跟踪进去发现,PixelFormat值为 8207,见下图:

<a href="http://images.cnblogs.com/cnblogs_com/xiaotie/201103/20110309114709203.png"></a>

我的程序是将所有的图像都转化为 Format24bppRgb 或 Format32bppArgb  格式的图像,然后再进行处理,对于不是 Format24bppRgb 或 Format32bppArgb  这两种格式的图像,则使用 Bitmap.Clone()方法进行转化,而这个方法,在处理 PixelFormat 值为 8207 的图像时抛出了异常。

John Farrow: The problem is that there are some values missing from the Image.PixelFormat enumeration.  8207 is pixel format PixelFormat32bppCMYK (based on GdiPlusHeaders.h).  For some reason this value is not part of the PixelFormat enumeration. When the problem image is loaded on Windows XP, the .NET framework converts it from CMYK to RGB and thus it matches a value in the enumeration such as PixelFormat32bppRGB but when the image is loaded on Windows 7 it is not converted to RGB, but remains in CMYK format. So the solution is for the application to explicitly test for the 8207 value and treat the image as having PixelFormat32bppCMYK.

也就是说,8207 是一个未定义的 PixelFormat 枚举值,它应该是 PixelFormat32bppCMYK 格式的图像。

还是在该页面,JohnWein给了个解决办法:

private static Bitmap DownsampleImage(Bitmap srcImg, int destW, int destH, float dstDPI)  {      Bitmap bmPhoto = new Bitmap(destW, destH,PixelFormat.Format32bppRgb);      bmPhoto.SetResolution(dstDPI, dstDPI);      Graphics grPhoto = Graphics.FromImage(bmPhoto);      grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;      grPhoto.DrawImage(srcImg,                        new System.Drawing.Rectangle(0, 0, bmPhoto.Width, bmPhoto.Height),                        new System.Drawing.Rectangle(0, 0, srcImg.Width, srcImg.Height),                        GraphicsUnit.Pixel);      grPhoto.Dispose();      return bmPhoto;  }

据测试,该方法可行,问题解决。下面是我修改后的代码:

private unsafe void CreateFromBitmap(Bitmap map)      int height = map.Height;      int width = map.Width;     const int PixelFormat32bppCMYK = 8207;     PixelFormat format = map.PixelFormat;     if (this.Width != width || this.Height != height)      {          return;      }     Bitmap newMap = map;      Int32 step = SizeOfT();     switch (format)          case PixelFormat.Format24bppRgb:              break;          case PixelFormat.Format32bppArgb:          default:              if ((int)format == PixelFormat32bppCMYK)              {                  format = PixelFormat.Format24bppRgb;                  newMap = new Bitmap(width, height, format);                  using (Graphics g = Graphics.FromImage(newMap))                  {                      g.DrawImage(map, new Point());                  }              }              else                  format = PixelFormat.Format32bppArgb;                  newMap = map.Clone(new Rectangle(0, 0, width, height), PixelFormat.Format32bppArgb);      BitmapData data = newMap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadOnly, format);      Byte* line = (Byte*)data.Scan0;     …… 本文转自xiaotie博客园博客,原文链接http://www.cnblogs.com/xiaotie/archive/2011/03/09/1978121.html如需转载请自行联系原作者 xiaotie 集异璧实验室(GEBLAB)

继续阅读