cool hit counter [C#] Lossless Conversion of Image to Icon_Intefrankly

[C#] Lossless Conversion of Image to Icon

As the title suggests, a common method on the market is.

var handle = bmp. GetHicon();     //Get the icon handle
 return Icon.FromHandle(handle);  // get the icon via the handle

The problem with this method is that if the image has a transparent background, then the edges of the resulting Icon are hairy, like the effect of first matting a background color and then de-coloring it, which is very unsatisfactory, as those who have used it know. Not yet studied is bmp. The problem with GetHicon is still a problem with Icon.FromHandle, I'll tinker with it later when I have some free time.

The perfect conversion method is given below.

/// <summary>
 /// Convert Image to Icon
/// </summary>
/// <param name="image">To convert to an icon ofImage targets</param>
/// <param name="nullTonull"> properimage because ofnull Does it return whennull。false then throw null reference exception</param>
/// <exception cref="ArgumentNullException" />
public static Icon ConvertToIcon(Image image, bool nullTonull = false)
    if (image == null)
        if (nullTonull) { return null; }
        throw new ArgumentNullException("image");

    using (MemoryStream msImg = new MemoryStream()
                      , msIco = new MemoryStream())
        image.Save(msImg, ImageFormat.Png);

        using (var bin = new BinaryWriter(msIco))
             //Write icon header
             bin. Write((short)0);            //0-1 reserved
             bin. Write((short)1);            /2-3 document type. 1=Icon, 2=Cursor
             bin. Write((short)1);            /4-5 number of images (icons can contain more than one image)

             bin. Write((byte)image. Width);   /6 icon width
             bin. Write((byte)image. Height);  /7 icon height
            bin.Write((byte)0);            //8 Number of colors( If the pixel bit depth>=8, fill or stuff0。 It's obvious., attain8bpp The minimum number of colors is256,byte not sufficient to indicate)
             bin. Write((byte)0);             /9 Reserved. Must be 0
             bin. Write((short)0);            /10-11 Palette
             bin. Write((short)32);           /12-13 bit deep
             bin. Write((int)msImg.Length);   //14-17 bitmap data size
             bin. Write(22);                  //18-21 bitmap data start byte

             /write image data

            bin.Seek(0, SeekOrigin.Begin);
            return new Icon(msIco);

The principle of the method, as shown in the code, is that

  1. First encode the image as a png
  2. Then wrap the png as-is into an icon

Step 1 is re-encoding, but the png is a lossless format and the image quality is not lost in the slightest. The converted png is then stuffed into the icon as is at the binary level. So the whole method can afford to be [non-destructive], so please feel free to use it if you mind the distortion. Note: The method does not check or process the original image size, so please make sure the original image size meets the icon specification before passing it in; in addition, it is not responsible for destroying the original image, so please ask the caller to be responsible for it externally.

Here is the rambling.

In order to solve this problem really took some effort, stackoverflow, codeproject and other miracle places to browse a few rounds but did not find a satisfactory method, after thinking about it, I feel that we can try from the icon format, and then in the almighty msdn really found a document about the icon format: the , thankfully not too hard to understand, and with some trial and error, the method came out.


1、Four Challenges in Kubernetes Log Transfer
2、What is machine learning in one article
3、Peking University Medical School Partners with Meridian Health to Create the Peking Medical Meridian Institute of Public Health
4、2018 no one will return until the building is broken To the miracle worker in us
5、Artificial intelligence conference held a very artistic visual feast

    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送