上傳圖片時,使用GDI+中重繪方式將CMYK圖片轉爲RGB圖片

我們知道,如果網站上傳圖片時,如果用戶上傳的是CMYK圖片,那麼在網站上將是無法顯示的(IE8瀏覽器或以下,火狐、谷歌瀏覽器可以),通常的現象是出現一個紅叉。
下面使用將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;
} 


PS:如果不要重繪,可以判斷是否爲CMYK模式,叫用戶用PS去重新修改圖片的模式

using System.Drawing.Imaging;

public partial class img : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string str1 = Server.MapPath(@"images/cmyk.jpg");
        string str2 = Server.MapPath(@"images/rgb.jpg");
        using (System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(str2))
        {
            bool flag = IsCMYK(bitmap);
            if (flag == true)
            {
                Response.Write("這圖是CMYK");
            }
            else
            {
                Response.Write("RGB啊,大哥");
            }
        }
    }
    //得到圖片的Flags
    public static string GetImageFlags(System.Drawing.Image img)
    {
        ImageFlags FlagVals = (ImageFlags)Enum.Parse(typeof(ImageFlags), img.Flags.ToString());
        return FlagVals.ToString();
    }

    //判斷是否爲CMYK
    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;
    }
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章