设为首页 收藏本站
查看: 834|回复: 0

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

[复制链接]

尚未签到

发表于 2015-5-14 11:52:52 | 显示全部楼层 |阅读模式
  Winform下的图像处理比较郁闷,动不动就蹦出这个 OutOfMemory 异常而不给具体原因。刚才谈新客户,他发给我几张jpg图片,让我处理一下给效果图出来,我用自己的图像处理程序一打开,蹦的一下,蹦出来了个 OutOfMemory 异常。跟踪进去发现,PixelFormat值为 8207,见下图:
DSC0000.png
  我的程序是将所有的图像都转化为 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;
  ……
  }

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-66913-1-1.html 上篇帖子: 关于在Win7 SP1操作系统下编译的ADO应用程序不能在比Win7 SP1版本低的操作系统上运行,微软给出的解释和解决方案 下篇帖子: win7自动壁纸切换小工具AutoDesk二:fade effect的壁纸切换
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表