How to convert a bitmap to icon in C# and VB.NET

To convert a bitmap to icon in C# and VB.NET you can use the following extension method.

Sample C#

public static Icon ToIcon(this Bitmap img, bool makeTransparent, Color colorToMakeTransparent)
{
	if (makeTransparent)
	{
		img.MakeTransparent(colorToMakeTransparent);
	}
	var iconHandle = img.GetHicon();
	return Icon.FromHandle(iconHandle);
}

Sample VB.NET

<System.Runtime.CompilerServices.Extension> _
Public Shared Function ToIcon(img As Bitmap, makeTransparent As Boolean, colorToMakeTransparent As Color) As Icon
	If makeTransparent Then
		img.MakeTransparent(colorToMakeTransparent)
	End If
	Dim iconHandle = img.GetHicon()
	Return Icon.FromHandle(iconHandle)
End Function

One thought on “How to convert a bitmap to icon in C# and VB.NET

Leave a Reply