Home > Backend Development > C#.Net Tutorial > Detailed explanation of examples of lossless conversion of Image to Icon in C#

Detailed explanation of examples of lossless conversion of Image to Icon in C#

Y2J
Release: 2017-04-22 10:17:39
Original
2423 people have browsed it

This article mainly introduces in detail the method of lossless conversion of Image to Icon in C#, which has certain reference value. Interested friends can refer to it

As the title says, the common methods on the market are :

var handle = bmp.GetHicon();  //得到图标句柄
return Icon.FromHandle(handle); //通过句柄得到图标
Copy after login

The problem with this method is that if the image has a transparent background, the edges of the resulting Icon will be rough, as if a layer of background color is applied first and then the color is removed, which is very unsatisfactory. Friends who have used it all know it. I haven't studied whether it is a problem with bmp.GetHicon or Icon.FromHandle. I will try to tinker with it when I have time in the future.

The perfect conversion method is given below:

/// <summary>
/// 转换Image为Icon
/// </summary>
/// <param name="image">要转换为图标的Image对象</param>
/// <param name="nullTonull">当image为null时是否返回null。false则抛空引用异常</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))
    {
      //写图标头部
      bin.Write((short)0);      //0-1保留
      bin.Write((short)1);      //2-3文件类型。1=图标, 2=光标
      bin.Write((short)1);      //4-5图像数量(图标可以包含多个图像)

      bin.Write((byte)image.Width); //6图标宽度
      bin.Write((byte)image.Height); //7图标高度
      bin.Write((byte)0);      //8颜色数(若像素位深>=8,填0。这是显然的,达到8bpp的颜色数最少是256,byte不够表示)
      bin.Write((byte)0);      //9保留。必须为0
      bin.Write((short)0);      //10-11调色板
      bin.Write((short)32);     //12-13位深
      bin.Write((int)msImg.Length); //14-17位图数据大小
      bin.Write(22);         //18-21位图数据起始字节

      //写图像数据
      bin.Write(msImg.ToArray());

      bin.Flush();
      bin.Seek(0, SeekOrigin.Begin);
      return new Icon(msIco);
    }
  }
}
Copy after login

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

1. First encode the image into png
2, and then Pack the png into an icon as it is

Although step 1 is re-encoding, png is a lossless format and the image quality will not be lost at all. Then insert the converted PNG into the icon intact at the binary level. Therefore, the whole method can be said to be "lossless". Friends who mind distortion can feel free to use it. Note: The size of the original image is not checked or processed in the method, so please ensure that the size of the original image meets the icon specifications before passing it in; in addition, the method is not responsible for destroying the original image, and the caller should be responsible externally.

The above is the detailed content of Detailed explanation of examples of lossless conversion of Image to Icon in C#. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template