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;

    ……

}

posted @   xiaotie  阅读(3025)  评论(2编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
历史上的今天:
2010-03-09 编写高效的C#图像处理程序——我的实验(续)
点击右上角即可分享
微信分享提示