Skip to main content
.NET Framework Class Library
Icon..::.FromHandle Method

Creates a GDI+ Icon from the specified Windows handle to an icon (HICON).

Namespace: System.Drawing
Assembly: System.Drawing (in System.Drawing.dll)
Syntax
Public Shared Function FromHandle ( _
	handle As IntPtr _
) As Icon
public static Icon FromHandle(
	IntPtr handle
)
public:
static Icon^ FromHandle(
	IntPtr handle
)
static member FromHandle : 
        handle:IntPtr -> Icon 

Parameters

handle
Type: System..::.IntPtr
A Windows handle to an icon.

Return Value

Type: System.Drawing..::.Icon
The Icon this method creates.
Remarks

When using this method, you must dispose of the original icon by using the DestroyIcon method in the Win32 API to ensure that the resources are released.

Examples

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.

  • Calls the Win32 API function DestroyIcon to release resources.


<System.Runtime.InteropServices.DllImportAttribute("user32.dll")> _
    Private Shared Function DestroyIcon(ByVal handle _ 
	As IntPtr) As Boolean 
    End Function

   Private Sub GetHicon_Example(ByVal e As PaintEventArgs)

        ' Create a Bitmap object from an image file.
        Dim myBitmap As New Bitmap("c:\FakePhoto.jpg")

        ' Draw myBitmap to the screen.
        e.Graphics.DrawImage(myBitmap, 0, 0)

        ' Get an Hicon for myBitmap.
        Dim HIcon As IntPtr = myBitmap.GetHicon()
	
        ' Create a new icon from the handle.
        Dim newIcon As Icon = System.Drawing.Icon.FromHandle(HIcon)

        ' Set the form Icon attribute to the new icon.
        Me.Icon = newIcon

        ' You can now destroy the icon, since the form creates its 
        ' own copy of the icon accessible through the Form.Icon property.
	DestroyIcon(newIcon.Handle)
    End Sub




[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;

    // You can now destroy the icon, since the form creates
    // its own copy of the icon accessible through the Form.Icon property.
    DestroyIcon(newIcon.Handle);

}


private:
   [System::Runtime::InteropServices::DllImportAttribute("user32.dll",CharSet=CharSet::Auto)]
   static bool DestroyIcon( IntPtr handle );

private:
   [SecurityPermission(SecurityAction::Demand, Flags=SecurityPermissionFlag::UnmanagedCode)]
   void GetHicon_Example( PaintEventArgs^ e )
   {

      // Create a Bitmap object from an image file.
      Bitmap^ myBitmap = gcnew 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. 
      System::Drawing::Icon^ newIcon = ::Icon::FromHandle( Hicon );

      // Set the form Icon attribute to the new icon.
      this->Icon = newIcon;

      // You can now destroy the Icon, since the form creates
      // its own copy of the icon accesible through the Form.Icon property.
      DestroyIcon( newIcon->Handle );
   }

Version Information

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
.NET Framework Security
Platforms

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.