Export (0) Print
Expand All

Bitmap.FromHicon Method

Creates a Bitmap from a Windows handle to an icon.

[Visual Basic]
Public Shared Function FromHicon( _
   ByVal hicon As IntPtr _
) As Bitmap
[C#]
public static Bitmap FromHicon(
 IntPtr hicon
);
[C++]
public: static Bitmap* FromHicon(
 IntPtr hicon
);
[JScript]
public static function FromHicon(
   hicon : IntPtr
) : Bitmap;

Parameters

hicon
A handle to an icon.

Return Value

The Bitmap object that this method creates.

Example

[Visual Basic, C#] The following example is designed for use with Windows Forms, and it requires PaintEventArgs e, which is a parameter of the Paint event handler. The code performs the following actions:

  • Gets the handle to an existing icon image.
  • Creates a Bitmap object from the handle.
  • Draws the Bitmap object to the screen.
[Visual Basic] 
Private Shared Function <System.Runtime.InteropServices.DllImportAttribute("user32.dll")> LoadImage(Hinstance As Integer, name As String, type As Integer, width As Integer, height As Integer, load As Integer) As IntPtr
Public Sub HICON_Example(e As PaintEventArgs)
' Get a handle to an icon.
Dim Hicon As IntPtr = LoadImage(0, "smile.ico", 1, 0, 0, 16)
' Create a Bitmap object from the icon handle.
Dim iconBitmap As Bitmap = Bitmap.FromHicon(Hicon)
' Draw the Bitmap object to the screen.
e.Graphics.DrawImage(iconBitmap, 0, 0)
End Sub
        
[C#] 
[System.Runtime.InteropServices.DllImportAttribute("user32.dll")]
private static extern IntPtr LoadImage(
int Hinstance,
string name,
int type,
int width,
int height,
int load);
public void Hicon_Example(PaintEventArgs e)
{
// Get a handle to an icon.
IntPtr Hicon = LoadImage(
0,
"smile.ico",
1,
0,
0,
16);
// Create a Bitmap object from the icon handle.
Bitmap iconBitmap = Bitmap.FromHicon(Hicon);
// Draw the Bitmap object to the screen.
e.Graphics.DrawImage(iconBitmap, 0, 0);
}
        

[C++, JScript] No example is available for C++ or JScript. To view a Visual Basic or C# example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

Bitmap Class | Bitmap Members | System.Drawing Namespace

Show:
© 2014 Microsoft