This documentation is archived and is not being maintained.

Bitmap.GetHicon Method

Returns the handle to an icon.

Namespace: System.Drawing
Assembly: System.Drawing (in system.drawing.dll)

public IntPtr GetHicon ()
public IntPtr GetHicon ()
public function GetHicon () : IntPtr
Not applicable.

Return Value

A Windows handle to an icon with the same image as the Bitmap.

Exception typeCondition

Exception

The operation failed.

Initially, an icon created from the handle will have the same size as the original bitmap.

The following code example is designed for use with Windows Forms, and it requires PaintEventArgse, which is a parameter of the Paint event handler. The code performs the following actions:

  • Creates a Bitmap.

  • Draws that object to the screen.

  • Gets an icon handle for the Bitmap.

  • Sets the Form.Icon attribute of the form to an icon created from the handle.


 [System.Runtime.InteropServices.DllImport("user32.dll", CharSet=CharSet.Auto)]
    extern static bool DestroyIcon(IntPtr handle);
   
private void GetHicon_Example(PaintEventArgs e)
    {
                 
        // Create a Bitmap object from an image file.
        Bitmap myBitmap = new Bitmap(@"c:\FakePhoto.jpg");
                 
        // Draw myBitmap to the screen.
        e.Graphics.DrawImage(myBitmap, 0, 0);
                 
        // Get an Hicon for myBitmap.
        IntPtr Hicon = myBitmap.GetHicon();
    
    // Create a new icon from the handle. 
    Icon newIcon = Icon.FromHandle(Hicon);
              
        // Set the form Icon attribute to the new icon.
        this.Icon = newIcon;
        
        // Destroy the Icon, since the form creates
    // its own copy of the icon.
        DestroyIcon(newIcon.Handle);
    
    }

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0
Show: