Converting CMYK Jpegs to RBG format, Resizing Graphics, & Correcting Resolution.
The intent of this procedure is to save uploaded images to the web server and in the process correct the size, resolution, and colorspace/pixel format if necessary.
I basically have a function here at takes 4 input parameters:
1) The Posted File object (assuming it has been validated)
2) The destination (Path / File Name) to save the file to.
3) The Maximum Height Allowed
4) The Maximum Width Allowed
The 72 dpi resolution is hardcoded in the method since it would stay constant for my purposes but you can easily make it an additional input parameter.
public static void AddImage(HttpPostedFile MyFile, string Destination, int MaxHeight, int MaxWidth)
{
//Determine Format
System.Drawing.Imaging.ImageFormat MyFormat;
if (Utilities.Right(Destination, 3).ToLower() == "jpg")
{
MyFormat = ImageFormat.Jpeg;
}
else
{
MyFormat = ImageFormat.Gif;
}
//Declare Bitmap to work with
Bitmap MyBitmap = new Bitmap(MyFile.InputStream);
//Fix CMYKs
if (IsCMYK(MyBitmap))
{
MyBitmap = ConvertCMYK(MyBitmap);
}
//Fix Resolution if necessary
if ((MyBitmap.HorizontalResolution > 72) || (MyBitmap.VerticalResolution > 72))
{
MyBitmap.SetResolution(72, 72);
}
//Transfer Bitmap in hopes of avoiding GDI+ Error
Bitmap SaveMap;
//Resize if necessary
if ((MyBitmap.Height > MaxHeight) || (MyBitmap.Width > MaxWidth))
{
Double HeightRatio = Convert.ToDouble(MaxHeight) / Convert.ToDouble(MyBitmap.Height);
Double WidthRatio = Convert.ToDouble(MaxWidth) / Convert.ToDouble(MyBitmap.Width);
Double ScaleRatio;
//Use the smaller ratio
if (HeightRatio > WidthRatio)
{
ScaleRatio = WidthRatio;
}
else
{
ScaleRatio = HeightRatio;
}
int NewHeight = Convert.ToInt32(MyBitmap.Height * ScaleRatio);
int NewWidth = Convert.ToInt32(MyBitmap.Width * ScaleRatio);
//Resize
SaveMap = new Bitmap(MyBitmap, NewWidth, NewHeight);
}
else
{
SaveMap = new Bitmap(MyBitmap);
}
MyBitmap.Dispose();
SaveMap.Save(Destination, MyFormat);
SaveMap.Dispose();
MyFile.InputStream.Close();
}
//=================================================================
public static string GetImageFlags(System.Drawing.Image MyImage)
{
ImageFlags FlagVals = (ImageFlags)Enum.Parse(typeof(ImageFlags), MyImage.Flags.ToString());
return FlagVals.ToString();
}
//=================================================================
public static bool IsCMYK(System.Drawing.Image MyImage)
{
bool ReturnVal;
if ((GetImageFlags(MyImage).IndexOf("Ycck") > -1) || (GetImageFlags(MyImage).IndexOf("Cmyk") > -1))
{ ReturnVal = true; }
else
{ ReturnVal = false; }
return ReturnVal;
}
//=================================================================
public static Bitmap ConvertCMYK(Bitmap MyBitmap)
{
Bitmap NewBit = new Bitmap(MyBitmap.Width, MyBitmap.Height, PixelFormat.Format24bppRgb);
Graphics MyGraph = Graphics.FromImage(NewBit);
MyGraph.CompositingQuality = CompositingQuality.HighQuality;
MyGraph.SmoothingMode = SmoothingMode.HighQuality;
MyGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;
Rectangle MyRect = new Rectangle(0, 0, MyBitmap.Width, MyBitmap.Height);
MyGraph.DrawImage(MyBitmap, MyRect);
Bitmap ReturnBitmap = new Bitmap(NewBit);
MyGraph.Dispose();
NewBit.Dispose();
MyBitmap.Dispose();
return ReturnBitmap;
}
Notice that we have separate functions used within to main method to:
1) Derive the image flags
2) Determine if image is CMYK
3) Convert CMYK image to RGB
출처 : http://www.maxostudio.com/Tut_CS_CMYK.cfm