• 上传图片时,CMKY转RGB


    在做项目时发现图片CMYK模式无法显示,

    下面使用将Image重新绘制为Format24bppRgb的方式来解决此问题: 

    using System.Drawing; 

    using System.Drawing.Drawing2D;
    using System.Drawing.Imaging;

    protected void Button1_Click(object sender, EventArgs e)
            {
                if (FileUpload1.HasFile)
                {
                    SavePostedImage(FileUpload1.PostedFile, FileUpload1.FileName, 1000,1000);
                }
            }

            public void SavePostedImage(HttpPostedFile postedFile, string destFileName, int maxHeight, int maxWidth)
            {
                System.Drawing.Imaging.ImageFormat imgFormat;
                if (destFileName.ToLower().EndsWith("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(7272);
                }

                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(Server.MapPath("~/") + destFileName, imgFormat);
                saveBmp.Dispose();
                postedFile.InputStream.Close();
            }

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


            public 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 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(00, 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;
            } 

        }下面使用将Image重新绘制为Format24bppRgb的方式来解决此问题:。


  • 相关阅读:
    金蝶天燕中间件将 web 应用存放于 applications 目录之外进行部署
    [Azure] 创建支持Apache,PHP以及MySQL的CentOS Web Virtual Machine Server
    [VSTS] 从零开始 Team Foundation Server 2010 安装配置详细图文教程
    [免费讲座] 成都软件技术沙龙 开启基于Scrum的敏捷开发全新征程讲座
    读[JQuery实现的后台框架(动易+Slashdot Menu)]有感
    [TechEd 2010] 回首微软TechEd十年,我们获益良多!
    [Windows Phone] 在中文版Visual Studio 2010中开发Windows Phone应用程序
    欢迎访问iOS开发者论坛:www.codeios.com!
    [Azure] Azure 中国服务使用注意事项及兼容版存储访问工具
    [VSTS] 玩转 Visual Studio 2010 自定义代码段特性
  • 原文地址:https://www.cnblogs.com/colder/p/2773115.html
Copyright © 2020-2023  润新知