Created
September 27, 2016 00:51
-
-
Save nbxx/2800bdfef65aaa30d83fb03678233abe to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <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