Bitmap.GetHicon Bitmap.GetHicon Bitmap.GetHicon Bitmap.GetHicon Method

Definizione

Restituisce l'handle per un'icona.Returns the handle to an icon.

public:
 IntPtr GetHicon();
public IntPtr GetHicon ();
member this.GetHicon : unit -> nativeint
Public Function GetHicon () As IntPtr

Restituisce

Handle Windows per un'icona con la stessa immagine dell'oggetto Bitmap.A Windows handle to an icon with the same image as the Bitmap.

Eccezioni

Operazione non riuscita.The operation failed.

Esempi

L'esempio di codice seguente è progettato per l'uso con Windows Forms e richiede PaintEventArgs @ no__t-1, che è un parametro del gestore eventi Paint.The following code example is designed for use with Windows Forms, and it requires PaintEventArgse, which is a parameter of the Paint event handler. Nel codice vengono eseguite le seguenti azioni:The code performs the following actions:

  • Crea un oggetto Bitmap.Creates a Bitmap.

  • Disegna l'oggetto sullo schermo.Draws that object to the screen.

  • Ottiene un handle di icona per il Bitmap.Gets an icon handle for the Bitmap.

  • Imposta l'attributo Form.Icon del form su un'icona creata dall'handle.Sets the Form.Icon attribute of the form to an icon created from the 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 );
   }

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

}
<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

Commenti

Inizialmente, un'icona creata dall'handle avrà le stesse dimensioni della bitmap originale.Initially, an icon created from the handle will have the same size as the original bitmap.

Sicurezza

SecurityPermission
per chiamare codice non gestito.for calling into unmanaged code. Enumerazione correlata: UnmanagedCodeRelated enumeration: UnmanagedCode

Si applica a