Skip to content

Instantly share code, notes, and snippets.

@nbxx
Created September 27, 2016 00:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nbxx/2800bdfef65aaa30d83fb03678233abe to your computer and use it in GitHub Desktop.
Save nbxx/2800bdfef65aaa30d83fb03678233abe to your computer and use it in GitHub Desktop.
/// <summary>
/// Convert Image to Icon.
/// </summary>
/// <param name="image">The source image.</param>
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);
bin.Write((short)1);
bin.Write((short)1);
bin.Write((byte)image.Width);
bin.Write((byte)image.Height);
bin.Write((byte)0);
bin.Write((byte)0);
bin.Write((short)0);
bin.Write((short)32);
bin.Write((int)msImg.Length);
bin.Write(22);
bin.Write(msImg.ToArray());
bin.Flush();
bin.Seek(0, SeekOrigin.Begin);
return new Icon(msIco);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment