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)

相关文章
|
运维 JavaScript 应用服务中间件
怎么微信WeixinJSBridge.invoke支付成功居然不跳转?还把我页面给关了!这篇文章就告诉你What should I do!
怎么微信WeixinJSBridge.invoke支付成功居然不跳转?还把我页面给关了!这篇文章就告诉你What should I do!
1560 0
怎么微信WeixinJSBridge.invoke支付成功居然不跳转?还把我页面给关了!这篇文章就告诉你What should I do!
|
存储 JSON 自然语言处理
手把手教你使用ModelScope训练一个文本分类模型
手把手教你使用ModelScope训练一个文本分类模型
执行apt-get install xxx 遇到无法定位软件包解决方法
执行apt-get install xxx 遇到无法定位软件包解决方法
4052 0
执行apt-get install xxx 遇到无法定位软件包解决方法
|
SQL 数据采集 关系型数据库
大数据采集和抽取怎么做?这篇文章终于说明白了!
数据是数据中台\数据平台核心中的核心,因此数据汇聚必然是数据中台/平台的入口,本文详细讲述采集模块的方方面面、采集框架的使用选型以及企业真实落地
大数据采集和抽取怎么做?这篇文章终于说明白了!
|
SQL 监控 Oracle
关系型数据库Oracle并行执行
【7月更文挑战第12天】
326 14
|
SQL 分布式计算 DataWorks
DataWorks产品使用合集之如何将本地的CSV文件上传到DataWorks的表中
DataWorks作为一站式的数据开发与治理平台,提供了从数据采集、清洗、开发、调度、服务化、质量监控到安全管理的全套解决方案,帮助企业构建高效、规范、安全的大数据处理体系。以下是对DataWorks产品使用合集的概述,涵盖数据处理的各个环节。
|
Kubernetes 安全 测试技术
超大规模商用 K8s 场景下,阿里巴巴如何动态解决容器资源的按需分配问题?
超大规模商用 K8s 场景下,阿里巴巴如何动态解决容器资源的按需分配问题?
|
12月前
|
人工智能 算法 NoSQL
GraphRAG 与 RAG 的比较分析
Graph RAG 技术通过引入图结构化的知识表示和处理方法,显著增强了传统 RAG 系统的能力。它不仅提高了信息检索的准确性和完整性,还为复杂查询和多步推理提供了更强大的支持。
1349 10
|
JSON 数据格式 索引
python之JMESPath:JSON 查询语法库示例详解
python之JMESPath:JSON 查询语法库示例详解
255 0
|
机器学习/深度学习 人工智能 数据可视化
号称能打败MLP的KAN到底行不行?数学核心原理全面解析
Kolmogorov-Arnold Networks (KANs) 是一种新型神经网络架构,挑战了多层感知器(mlp)的基础,通过在权重而非节点上使用可学习的激活函数(如b样条),提高了准确性和可解释性。KANs利用Kolmogorov-Arnold表示定理,将复杂函数分解为简单函数的组合,简化了神经网络的近似过程。与mlp相比,KAN在参数量较少的情况下能达到类似或更好的性能,并能直观地可视化,增强了模型的可解释性。尽管仍需更多研究验证其优势,KAN为深度学习领域带来了新的思路。
5025 5