Icon::FromHandle Method (IntPtr)
Creates a GDI+ Icon from the specified Windows handle to an icon (HICON).
Assembly: System.Drawing (in System.Drawing.dll)
Parameters
- handle
-
Type:
System::IntPtr
A Windows handle to an icon.
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.
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.
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 ); }
for access to unmanaged code. Related enumeration UnmanagedCode.
Available since 1.1