Bitmap.FromHicon(IntPtr) 方法

定义

从图标的 Windows 图柄创建 Bitmap

public:
 static System::Drawing::Bitmap ^ FromHicon(IntPtr hicon);
public static System.Drawing.Bitmap FromHicon (IntPtr hicon);
static member FromHicon : nativeint -> System.Drawing.Bitmap
Public Shared Function FromHicon (hicon As IntPtr) As Bitmap

参数

hicon
IntPtr

nativeint

图标的图柄。

返回

此方法创建的 Bitmap

示例

下面的代码示例旨在与 Windows 窗体 一起使用,它需要 PaintEventArgse,它是 事件处理程序的Paint一个参数。 此代码执行以下操作:

  • 获取现有图标图像的句柄。

  • Bitmap从句柄创建 。

  • Bitmap 绘制到屏幕。

private:
    [System::Runtime::InteropServices::DllImportAttribute("user32.dll", CharSet = CharSet::Unicode)]
   static IntPtr LoadImage( int Hinstance, String^ name, int type, int width, int height, int load );

private:
   void Hicon_Example( PaintEventArgs^ e )
   {
      
      // Get a handle to an icon.
      IntPtr Hicon = LoadImage( 0, "smile.ico", 1, 0, 0, 16 );
      
      // Create a Bitmap object from the icon handle.
      Bitmap^ iconBitmap = Bitmap::FromHicon( Hicon );
      
      // Draw the Bitmap object to the screen.
      e->Graphics->DrawImage( iconBitmap, 0, 0 );
   }
[System.Runtime.InteropServices.DllImportAttribute("user32.dll", CharSet = CharSet.Unicode)]
private static extern IntPtr LoadImage(int Hinstance,
    string name, int type, int width, int height, int load);

private void Hicon_Example(PaintEventArgs e)
{

    // Get a handle to an icon.
    IntPtr Hicon = LoadImage(0, "smile.ico", 1, 0, 0, 16);

    // Create a Bitmap object from the icon handle.
    Bitmap iconBitmap = Bitmap.FromHicon(Hicon);

    // Draw the Bitmap object to the screen.
    e.Graphics.DrawImage(iconBitmap, 0, 0);
}
<System.Runtime.InteropServices.DllImportAttribute("user32.dll", CharSet:=CharSet.Unicode)> _
Private Shared Function LoadImage(ByVal Hinstance As Integer, ByVal name As String, ByVal type As Integer, ByVal width As Integer, ByVal height As Integer, ByVal load As Integer) As IntPtr
End Function

Private Sub HICON_Example(ByVal e As PaintEventArgs)

    ' Get a handle to an icon.
    Dim Hicon As IntPtr = LoadImage(0, "smile.ico", 1, 0, 0, 16)

    ' Create a Bitmap object from the icon handle.
    Dim iconBitmap As Bitmap = Bitmap.FromHicon(Hicon)

    ' Draw the Bitmap object to the screen.
    e.Graphics.DrawImage(iconBitmap, 0, 0)
End Sub

适用于