Bitmap.GetHicon Method
Returns the handle to an icon.
Namespace: System.Drawing
Assembly: System.Drawing (in System.Drawing.dll)
| Exception | Condition |
|---|---|
| Exception | The operation failed. |
The following code 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:
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; // 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); }
- SecurityPermission
for calling into unmanaged code. Related enumeration: UnmanagedCode
Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.