본문 바로가기
Web Program/Asp.net Lecture

Converting CMYK Jpegs to RBG format, Resizing Graphics, & Correcting Resolution.

by 현이빈이 2009. 8. 4.
반응형

Converting CMYK Jpegs to RBG format, Resizing Graphics, & Correcting Resolution.
I was recently building a Photo Upload form for the classifieds section of a clients website. Although the form had the proper validation regarding image type allowed I was hesitant to check for (more on this later) and deny upload of CMYK jpegs. As you may know, they don't show on web browsers. I was hoping to programmatically convert them to RGB format without harssing the user. I did 2 days of research on this but never found any direct instructions or examples. However, I was able to gleam enough information off of various posts and tutorials to come up with this solution. I decided to document the solution here on Maxo and post links to it on all forums where other lost souls looking for this solution were left high and dry with no responses, or poor ones.

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

반응형