网页中有的图片在ie6、7、8下无法显示,ie9下正常

这个主要是因为图片的模式问题:

RGB模式下的图片在ie6、7、8、9下都是正常的,但是CMYK模式的图片只在ie9下是正常的

解决方法:

在上传图片的时候对图片的模式进行判断并且把CMYG模式的图片转换为RGB模式

判断:

Bitmap bmp = new Bitmap(allow_fileStream);  //文件路径
 
  allowUpload = stringHelper.IsCMYK(bmp) == "true" ? false : true;  //返回true字符串则图片不是RGB模式的
 
    public string IsCMYK(System.Drawing.Image img)
    {
        string isCmyk;
        if ((GetImageFlags(img).IndexOf("Ycck") > -1) || (GetImageFlags(img).IndexOf("Cmyk") > -1))
        {
            isCmyk = "true";
        }
        else
        {
            isCmyk = "false";
        }
 
        return isCmyk;
    }
    public string GetImageFlags(System.Drawing.Image img)
    {
        ImageFlags FlagVals = (ImageFlags)Enum.Parse(typeof(ImageFlags), img.Flags.ToString());
        return FlagVals.ToString();
    }

转换:

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;

}

 

posted @ 2014-03-24 10:42  天殇月痕  阅读(333)  评论(0编辑  收藏  举报