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

简介:

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

image

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

搜索表明,8027是CMYK格式的图像,这是一个在Win7下独有的bug,在xp下,.Net FW会自动把该格式的转换为 RGB 格式的图片(未验证),而Win7下不会[ Image has wrong Image.PixelFormat on Windows 7, not on XP]:

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: 
            break; 
        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); 
            } 
            break; 
    }

    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)

相关文章
|
4月前
|
计算机视觉 C++
win7系统OpenCV读取图片内存位置异常
win7系统OpenCV读取图片内存位置异常
30 0
|
11月前
|
Java
MAT无法打开较大的hprof的解决办法
MAT无法打开较大的hprof的解决办法
781 0
|
存储 缓存 Java
图片系列(6)高低版本 Bitmap 内存分配与回收原理对比
图片系列(6)高低版本 Bitmap 内存分配与回收原理对比
232 0
图片系列(6)高低版本 Bitmap 内存分配与回收原理对比
|
测试技术 Android开发 数据格式
|
iOS开发
使用AutoLayout约束, 为啥图片的大小(Image size)却还以实际大小显示?
问题 给一个 UIImageView 设置一张图片时,使用 AutoLayout 给 UIImageView 约束宽高,但是实际显示的大小,图片以实际的大小显示出来,代码也没有设置 frame,设置contentMode为UIViewContentModeScaleAspectFit 也不起作用。
1050 0
网上很多NV21数据直接使用BitmapFactory的代码是错误的
网上很多NV21数据直接使用BitmapFactory的代码是错误的
193 0
|
存储 缓存 Java
浅谈 Glide - BitmapPool 的存储时机 & 解答 ViewTarget 在同一View显示不同的图片时,总用同一个 Bitmap 引用的原因
作者:林冠宏 / 指尖下的幽灵 掘金:https://juejin.im/user/587f0dfe128fe100570ce2d8 博客:http://www.cnblogs.com/linguanh/ GitHub : https://github.com/af913337456/ 腾讯云专栏: https://cloud.tencent.com/developer/user/1148436/activities 这两天在改造我的私人APP 非ROOT版微信自动回复, 使之可以多开的时候,碰到一个这样的问题。
1503 0