SoFunction
Updated on 2025-03-04

Method for converting Image to Icon without lossless

As the question is, the common methods on the market are:

var handle = ();  //Get the icon handlereturn (handle); //Get the icon through the handle

The problem with this method is that if the image is a transparent background, the edges of the obtained Icon will be rough, such as the effect of first applying a layer of background color and then removing the color is very unsatisfactory, and friends who have used it will know it. I haven't studied whether it's a problem or something, and I'll try it out if I have the time to have some leisure time in the future.

The perfect conversion method is given below:

/// <summary>
/// Convert Image to Icon/// </summary>
/// <param name="image">Image object to convert to icon</param>/// <param name="nullTonull">Whether null is returned when image is null.  false will throw empty reference exception</param>/// &lt;exception cref="ArgumentNullException" /&gt;
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())
  {
    (msImg, );

    using (var bin = new BinaryWriter(msIco))
    {
      //Write the icon header      ((short)0);      //0-1 Reserved      ((short)1);      //2-3 File type.  1=Icon, 2=Cursor      ((short)1);      //4-5 Number of images (the icon can contain multiple images)
      ((byte)); //6 icon width      ((byte)); //7 icon height      ((byte)0);      //8 color number (if the pixel bit depth >=8, fill in 0. This is obvious. The minimum number of colors that reach 8bpp is 256, which is not enough to indicate)      ((byte)0);      //9 Reserved.  Must be 0      ((short)0);      //10-11 color palette      ((short)32);     //12-13 Degree      ((int)); //14-17 bitmap data size      (22);         //18-21 bitmap data start byte
      //Write image data      (());

      ();
      (0, );
      return new Icon(msIco);
    }
  }
}

As shown in the code, the principle of the method is:

1. Encode the image as png first
2. Package png as is an icon

Although the first step is recoding, png is a lossless format and there will be no loss in image quality. Then, the converted png is stuffed into the icon intact at the binary level. So the whole method can be afforded by the statement of [destructive]. Friends who are concerned about distortion, please feel free to use it. Note: The original image size is not checked or processed in the method, so please make sure that the size of the original image meets the icon specifications before passing it in; in addition, you are not responsible for destroying the original image, please be responsible externally.

Here is a talk:

It took a lot of effort to solve this problem. I went around for a few times in places where miracles such as * and codeproject, but couldn't find a way to do it. After thinking about it, I felt that I could try it out on the icon format, and then I found a document on the universal msdn./en-us/library/Fortunately, it is not difficult to understand. After some attempts, the method is released.

-Wenbi-

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.