C# School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!
 

Code Snippets

  

C# Source Code



Convert Image to Icon

Converts an Image to an Icon (of a specified size), while keeping the aspect ratio of the original.

Submitted By: Nayana
Actions:
Rating:
Views: 23,453

Language: C#

Last Modified: January 23, 2008
Instructions: Assumes "using System.Drawing;"

Just paste into your code. Or make it public and put it in another class.

Snippet


  1.     /// <summary>
  2.     /// Converts an image into an icon.
  3.     /// </summary>
  4.     /// <param name="img">The image that shall become an icon</param>
  5.     /// <param name="size">The width and height of the icon. Standard
  6.     /// sizes are 16x16, 32x32, 48x48, 64x64.</param>
  7.     /// <param name="keepAspectRatio">Whether the image should be squashed into a
  8.     /// square or whether whitespace should be put around it.</param>
  9.     /// <returns>An icon!!</returns>
  10.     private Icon MakeIcon(Image img, int size, bool keepAspectRatio) {
  11.       Bitmap square = new Bitmap(size, size); // create new bitmap
  12.       Graphics g = Graphics.FromImage(square); // allow drawing to it
  13.  
  14.       int x, y, w, h; // dimensions for new image
  15.  
  16.       if(!keepAspectRatio || img.Height == img.Width) {
  17.         // just fill the square
  18.         x = y = 0; // set x and y to 0
  19.         w = h = size; // set width and height to size
  20.       } else {
  21.         // work out the aspect ratio
  22.         float r = (float)img.Width / (float)img.Height;
  23.  
  24.         // set dimensions accordingly to fit inside size^2 square
  25.         if(r > 1) { // w is bigger, so divide h by r
  26.           w = size;
  27.           h = (int)((float)size / r);
  28.           x = 0; y = (size - h) / 2; // center the image
  29.         } else { // h is bigger, so multiply w by r
  30.           w = (int)((float)size * r);
  31.           h = size;
  32.           y = 0; x = (size - w) / 2; // center the image
  33.         }
  34.       }
  35.  
  36.       // make the image shrink nicely by using HighQualityBicubic mode
  37.       g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
  38.       g.DrawImage(img, x, y, w, h); // draw image with specified dimensions
  39.       g.Flush(); // make sure all drawing operations complete before we get the icon
  40.  
  41.       // following line would work directly on any image, but then
  42.       // it wouldn't look as nice.
  43.       return Icon.FromHandle(square.GetHicon());
  44.     }

Copy & Paste


Comments

jdemarco7751 2008-12-10 11:59:22

Cool!

GHBLoos 2009-01-09 15:28:07

Really good! I have one problem with it. There appears a small line at the left and top border on the image. I also have the idea that any image will get a background, while they have a transparant background. Any solutions for that? I am not really good with images.

Stippadippa 2009-07-17 16:35:43

Awesome, does this work also with .Png files? That would be very useful as i got collection of .pngs that need to be converted. And also if possible to convert folder/multiple files to ico?

norz 2010-01-08 01:55:26

i need help.is this coding can do in for icon in mobile?where i have to paste this coding in c#?i have zero knowledge for visual c#.


Add comment


You must be registered and logged on to </dream.in.code> to leave comments.