当前位置: 移动技术网 > IT编程>开发语言>c# > C#无损转换Image为Icon的方法

C#无损转换Image为Icon的方法

2019年07月18日  | 移动技术网IT编程  | 我要评论

如题,市面上常见的方法是:

var handle = bmp.gethicon();  //得到图标句柄
return icon.fromhandle(handle); //通过句柄得到图标

此法的问题是,如果图像是透明背景,那么得到的icon的边缘就是毛糙的,像是先垫了一层背景色然后再去色的效果,很不如人意,用过的朋友都知道。尚未研究是bmp.gethicon出的问题,还是icon.fromhandle有问题,日后有闲心再捣鼓下。

下面给出完美转换方法:

/// <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);
    }
  }
}

如码所示,方法的原理是:

1、先将image编码为png
2、再将png原样包装成一个icon

第1步虽然是重编码,但png是无损格式,图像质量不会有丝毫损失。然后在二进制层面原封不动的把转换得到的png塞入图标。所以整个方法担得起【无损】的说法,介意失真的朋友请放心使用。注意:方法中并未对原图size做检查、处理,所以请先确保原图的尺寸符合图标规格再传入;另外,不负责销毁原图,请调用者在外部负责。

下面是闲扯:

为了解决这个问题还真费了番功夫,stackoverflow、codeproject等神迹多现的地方逛了几圈都没找到如意的法子,思索一番后感觉可以从图标格式上尝试,然后在万能的msdn果然找到一篇讲icon格式的文档:,还好不算很难理解,一番尝试之下,方法出炉。

-文毕-

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网