今天做照片管理软件遇到个问题,某些JPG照片不能在WEB显示,查询后,找到了原因:
网页支持的格式应该是RGB格式的。而有时候我们扫描或者接受别人发过来的图片时,图片是CMYK印刷格式的。这种图片在本地编辑、查看一点问题都没有,但一旦放在网页中,就会无法显示。当你无法确认这是哪种图片格式的时候,推荐使用最简单的方法:用PHOTOSHOP(简称PS)打开这个图片,然后菜单“图象”——“模式”,这时就可以看到在“RGB”还是 “CMYK”前面打着对勾,就说明这是什么格式的。想改变格式的话就可以点击相应的格式。改变后保存图片再上传就可以了。
然后,网上找来个CMYK格式转换成RGB格式的方法
public class SaveImgAsRGB
{
/// <summary>
/// 上传图片 集成cmyk→rgb
/// </summary>
/// <param name="postedFile">上载文件的访问</param>
/// <param name="filePaht">保存目录</param>
/// <returns>上传后的文件名</returns>
public string SavePostedImage(HttpPostedFile postedFile, string filePaht)
{
return SavePostedImage(postedFile,filePaht,"",0,0,null,0.8);
}
/// <summary>
/// 上传图片 集成cmyk→rgb
/// </summary>
/// <param name="postedFile">上载文件的访问</param>
/// <param name="filePaht">保存目录</param>
/// <param name="imgFormat">图片格式要求</param>
/// <returns>上传后的文件名</returns>
public string SavePostedImage(HttpPostedFile postedFile, string filePaht,System.Drawing.Imaging.ImageFormat imgFormat)
{
return SavePostedImage(postedFile,filePaht,"",0,0,imgFormat,0.8);
}
/// <summary>
/// 上传图片 集成cmyk→rgb
/// </summary>
/// <param name="postedFile">上载文件的访问</param>
/// <param name="filePaht">保存目录</param>
/// <param name="fileName">新名字</param>
/// <param name="imgFormat">图片格式要求</param>
/// <returns>上传后的文件名</returns>
public string SavePostedImage(HttpPostedFile postedFile, string filePaht,string fileName,System.Drawing.Imaging.ImageFormat imgFormat)
{
return SavePostedImage(postedFile,filePaht,fileName,0,0,imgFormat,0.8);
}
/// <summary>
/// 上传图片 集成cmyk→rgb
/// </summary>
/// <param name="postedFile">上载文件的访问</param>
/// <param name="filePaht">保存目录</param>
/// <param name="fileName">新名字</param>
/// <param name="maxHeight">限高</param>
/// <param name="maxWidth">限宽</param>
/// <param name="imgFormat">图片格式要求</param>
/// <param name="rate">压缩比(double:0--1)</param>
/// <returns>上传后的文件名</returns>
public string SavePostedImage(HttpPostedFile postedFile, string filePaht,string fileName, int maxHeight, int maxWidth,System.Drawing.Imaging.ImageFormat imgFormat,double rate)
{
try
{
if(imgFormat==null)
{
if (postedFile.FileName.ToLower().EndsWith("jpg"))
{
imgFormat = ImageFormat.Jpeg;
}
else if (postedFile.FileName.ToLower().EndsWith("gif"))
{
imgFormat = ImageFormat.Gif;
}
else if (postedFile.FileName.ToLower().EndsWith("bmp"))
{
imgFormat = ImageFormat.Bmp;
}
else if (postedFile.FileName.ToLower().EndsWith("png"))
{
imgFormat = ImageFormat.Png;
}
else if (postedFile.FileName.ToLower().EndsWith("tiff"))
{
imgFormat = ImageFormat.Tiff;
}
else if (postedFile.FileName.ToLower().EndsWith("icon"))
{
imgFormat = ImageFormat.Icon;
}
else
{
return "";
}
}
Bitmap bmp = new Bitmap(postedFile.InputStream);
bmp=IsCMYK(bmp)?ConvertCMYK(bmp, rate):bmp;
if ((bmp.HorizontalResolution > 72) || (bmp.VerticalResolution > 72))
{
bmp.SetResolution(72, 72);
}
Bitmap saveBmp;
if ((bmp.Height > maxHeight) || (bmp.Width > maxWidth))
{
if ((maxHeight==0) || (maxWidth==0))
{
saveBmp = new Bitmap(bmp);
}
else
{
Double heightRatio = Convert.ToDouble(maxHeight) / Convert.ToDouble(bmp.Height);
Double widthRatio = Convert.ToDouble(maxWidth) / Convert.ToDouble(bmp.Width);
Double scaleRatio;
scaleRatio = heightRatio > widthRatio ? widthRatio : heightRatio;
saveBmp = new Bitmap(bmp, Convert.ToInt32(bmp.Width * scaleRatio), Convert.ToInt32(bmp.Height * scaleRatio));
}
}
else
{
saveBmp = new Bitmap(bmp);
}
DateTime DateFile=DateTime.Now;
string NewFileName=DateFile.Year.ToString()+DateFile.Month.ToString()+DateFile.Day.ToString()+DateFile.Hour.ToString()+DateFile.Minute.ToString();
fileName=fileName.LastIndexOf('.') > 0 ? fileName.Substring(0, fileName.IndexOf('.')) : fileName.Replace(".","");
fileName=fileName!=""?NewFileName+fileName:NewFileName;
string path=HttpContext.Current.Server.MapPath(filePaht);
if(!Directory.Exists(path))
Directory.CreateDirectory(path);
fileName+="."+imgFormat.ToString();
saveBmp.Save(path + "//" + fileName , imgFormat);
bmp.Dispose();
saveBmp.Dispose();
postedFile.InputStream.Close();
}
catch (System.Exception)
{
fileName="";
}
return fileName;
}
#region 获取ImageFlags
private string GetImageFlags(System.Drawing.Image img)
{
ImageFlags FlagVals = (ImageFlags)Enum.Parse(typeof(ImageFlags), img.Flags.ToString());
return FlagVals.ToString();
}
#endregion
#region 判断CMYK
private bool IsCMYK(System.Drawing.Image img)
{
string FlagVals = GetImageFlags(img);
if ((FlagVals.IndexOf("Ycck") > -1) || (FlagVals.IndexOf("Cmyk") > -1))
{ return true; }
else
{ return false; }
}
#endregion
#region 压缩图片
private Bitmap ConvertCMYK(Bitmap bmp,double rate)
{
rate=rate>1.0?1.0:rate;
rate=rate<0.5?0.5:rate;
Bitmap tmpBmp = new Bitmap(bmp.Width, bmp.Height, PixelFormat.Format24bppRgb);
Graphics g = Graphics.FromImage(tmpBmp);
if (rate==0.5)//底质量
{
g.CompositingQuality = CompositingQuality.HighSpeed;
g.SmoothingMode = SmoothingMode.HighSpeed;
g.InterpolationMode = InterpolationMode.NearestNeighbor;
}
if (rate==1.0)//高质量
{
g.CompositingQuality = CompositingQuality.HighQuality;
g.SmoothingMode = SmoothingMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
}
else
{//一般质量
g.CompositingQuality = CompositingQuality.HighQuality;
g.SmoothingMode = SmoothingMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBilinear;
}
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;
}
#endregion
}