Version

IconToBitmap Method

Converts the specified System.Drawing.Icon to a System.Drawing.Bitmap, preserving transparency.
Syntax
'Declaration
 
Public Shared Function IconToBitmap( _
   ByVal icon As Icon _
) As Bitmap
public static Bitmap IconToBitmap( 
   Icon icon
)

Parameters

icon
The System.Drawing.Icon to convert.

Return Value

A System.Drawing.Bitmap with the same image as the specified System.Drawing.Icon.
Remarks

There is a limitation in the System.Drawing.Icon.ToBitmap method (.NET framework v1.x only) whereby the opacity of the pixels which comprise the icon is not properly represented in the returned Bitmap. The IconToBitmap method overcomes this limitation by accessing the Icon's pixel data directly.

The IconToBitmap method will only function correctly on a computer which has the right to execute unmanaged code; this is determined by using the System.Security.Permissions.SecurityPermission class.

The IconToBitmap method catches any exceptions that are thrown during the process of accessing the Icon's pixel data; when an exception is thrown, the System.Drawing.Icon.ToBitmap method is used as a fallback. If the System.Drawing.Icon.ToBitmap method itself throws an exception, the IconToBitmap method will surface the exception.

The System.Drawing.Bitmap returned by the IconToBitmap method is a limited system resource and should be disposed of when it is no longer in use.

The limitation in the System.Drawing.Icon.ToBitmap method does not exist in v2.x of the .NET framework, so on that platform, this method calls directly into the System.Drawing.Icon.ToBitmap method.

Requirements

Target Platforms: Windows 10, Windows 8.1, Windows 8, Windows 7, Windows Server 2012, Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

See Also