7 Replies - 2768 Views - Last Post: 06 March 2016 - 08:53 AM

#1 Nayana  Icon User is offline

  • DIC Hawk - 나야나 नयन:
  • member icon

Reputation: 32
  • View blog
  • Posts: 824
  • Joined: 14-November 07

Convert Image to Icon

Posted 23 January 2008 - 01:35 AM

Description: Assumes "using System.Drawing;" Just paste into your code. Or make it public and put it in another class.Converts an Image to an Icon (of a specified size), while keeping the aspect ratio of the original.
    /// 
    /// Converts an image into an icon.
    /// 
    /// The image that shall become an icon
    /// The width and height of the icon. Standard
    /// sizes are 16x16, 32x32, 48x48, 64x64.
    /// Whether the image should be squashed into a
    /// square or whether whitespace should be put around it.
    /// An icon!!
    private Icon MakeIcon(Image img, int size, bool keepAspectRatio) {
      Bitmap square = new Bitmap(size, size); // create new bitmap
      Graphics g = Graphics.FromImage(square); // allow drawing to it

      int x, y, w, h; // dimensions for new image

      if(!keepAspectRatio || img.Height == img.Width) {
        // just fill the square
        x = y = 0; // set x and y to 0
        w = h = size; // set width and height to size
      } else {
        // work out the aspect ratio
        float r = (float)img.Width / (float)img.Height;

        // set dimensions accordingly to fit inside size^2 square
        if(r > 1) { // w is bigger, so divide h by r
          w = size;
          h = (int)((float)size / r);
          x = 0; y = (size - h) / 2; // center the image
        } else { // h is bigger, so multiply w by r
          w = (int)((float)size * r);
          h = size;
          y = 0; x = (size - w) / 2; // center the image
        }
      }

      // make the image shrink nicely by using HighQualityBicubic mode
      g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
      g.DrawImage(img, x, y, w, h); // draw image with specified dimensions
      g.Flush(); // make sure all drawing operations complete before we get the icon

      // following line would work directly on any image, but then
      // it wouldn't look as nice.
      return Icon.FromHandle(square.GetHicon());
    }


Is This A Good Question/Topic? 1
  • +

Replies To: Convert Image to Icon

#2 jdemarco7751  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 10-October 08

Re: Convert Image to Icon

Posted 10 December 2008 - 09:59 AM

Cool!
Was This Post Helpful? 0
  • +
  • -

#3 GHBLoos  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 09-January 09

Re: Convert Image to Icon

Posted 09 January 2009 - 01:28 PM

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.
Was This Post Helpful? 0
  • +
  • -

#4 Stippadippa  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 9
  • Joined: 10-July 09

Re: Convert Image to Icon

Posted 17 July 2009 - 01:35 PM

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?
Was This Post Helpful? 0
  • +
  • -

#5 norz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 08-January 10

Re: Convert Image to Icon

Posted 07 January 2010 - 11:55 PM

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#.
Was This Post Helpful? 0
  • +
  • -

#6 kuthu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 27-September 10

Re: Convert Image to Icon

Posted 27 September 2010 - 12:29 PM

da echi kala nayae ethu oru code a?…. pannada inimale forum la post pannuna serupala adipa….those who post comment as “nice, working good,” will sucks forever
Was This Post Helpful? 0
  • +
  • -

#7 T_xy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 26-March 11

Re: Convert Image to Icon

Posted 26 March 2011 - 11:22 AM

Good work, just beware of proper disposing http://msdn.microsof...fromhandle.aspx
Was This Post Helpful? 0
  • +
  • -

#8 PaddiM8  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 15-February 16

Re: Convert Image to Icon

Posted 06 March 2016 - 08:53 AM

I can't believe it! You saved me :bananaman:
Thank's dude!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1