上传图片时,使用GDI+中重绘方式将CMYK图片转为RGB图片

123 篇文章 2 订阅
116 篇文章 9 订阅

我们知道,如果网站上传图片时,如果用户上传的是CMYK图片,那么在网站上将是无法显示的,通常的现象是出现一个红叉。
下面使用将Image重新绘制为Format24bppRgb的方式来解决此问题:

public static void SavePostedImage(HttpPostedFile postedFile, string destFileName, int maxHeight, int maxWidth)
{
     System.Drawing.Imaging.ImageFormat imgFormat;
     if (destFileName.ToLower().EndWith("jpg"))
     {
          imgFormat = ImageFormat.Jpeg;
     }
     else //这里可以加更多选项,比如png,gif,tif....
     {
          imgFormat = ImageFormat.Gif;
     }

     Bitmap bmp = new Bitmap(postedFile.InputStream);

     if (IsCMYK(bmp))
     {
          bmp = ConvertCMYK(bmp);
     }

     if ((bmp.HorizontalResolution > 72) || (bmp.VerticalResolution > 72))
     {
          bmp.SetResolution(72, 72);
     }

     Bitmap saveBmp;
     if ((bmp.Height > maxHeight) || (bmp.Width > maxWidth))
     {
          Double heightRatio = Convert.ToDouble(maxHeight) / Convert.ToDouble(bmp.Height);
          Double widthRatio = Convert.ToDouble(maxWidth) / Convert.ToDouble(bmp.Width);
          Double scaleRatio;

          if (heightRatio > widthRatio)
          {
               scaleRatio = widthRatio;
          }
          else
          {
               scaleRatio = heightRatio;
          }

          int height = Convert.ToInt32(bmp.Height * scaleRatio);
          int width = Convert.ToInt32(bmp.Width * scaleRatio);

          saveBmp = new Bitmap(bmp, width, height);
     }
     else
     {
          saveBmp = new Bitmap(bmp);
     }

     bmp.Dispose();
     saveBmp.Save(destFileName, imgFormat);
     saveBmp.Dispose();
     postedFile.InputStream.Close();
}

public static string GetImageFlags(System.Drawing.Image img)
{
     ImageFlags FlagVals = (ImageFlags)Enum.Parse(typeof(ImageFlags), img.Flags.ToString());
     return FlagVals.ToString();
}


public static bool IsCMYK(System.Drawing.Image img)
{
     bool isCmyk;

     if ((GetImageFlags(img).IndexOf("Ycck") > -1) || (GetImageFlags(img).IndexOf("Cmyk") > -1))
     { isCmyk = true; }
     else
     { isCmyk = false; }

     return isCmyk;
}

public static Bitmap ConvertCMYK(Bitmap bmp)
{
     Bitmap tmpBmp = new Bitmap(bmp.Width, bmp.Height, PixelFormat.Format24bppRgb);

     Graphics g = Graphics.FromImage(tmpBmp);
     g.CompositingQuality = CompositingQuality.HighQuality;
     g.SmoothingMode = SmoothingMode.HighQuality;
     g.InterpolationMode = InterpolationMode.HighQualityBicubic;

     Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
     // 将CMYK图片重绘一遍,此时GDI+自动将CMYK格式转换为RGB了
     g.DrawImage(bmp, rect);

     Bitmap returnBmp = new Bitmap(tmpBmp);

     g.Dispose();
     tmpBmp.Dispose();
     bmp.Dispose();

     return returnBmp;


更多讨论:
(1)如何将RGB图片转换为CMYK图片?
(2)如何将RGB图片转换为索引图片?
(3)如何将RGB图片转换为灰度/黑白线条图片?
(4)上述转换如果是在WPF中,又如何进行呢?

留给读者去思考吧.

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值