Icon.ToBitmap 方法

定义

将此 Icon 转换为 GDI+ Bitmap

public:
 System::Drawing::Bitmap ^ ToBitmap();
public System.Drawing.Bitmap ToBitmap ();
member this.ToBitmap : unit -> System.Drawing.Bitmap
Public Function ToBitmap () As Bitmap

返回

Bitmap,它表示已转换的 Icon

示例

下面的代码示例演示如何使用 ToBitmap 方法。 此示例旨在与 Windows 窗体 一起使用。 创建窗体并将以下代码粘贴到其中。 IconToBitmap在窗体的 .Paint事件处理程序中调用 方法,作为 ePaintEventArgs 传递。

private:
   void IconToBitmap( PaintEventArgs^ e )
   {
      // Construct an Icon.
      System::Drawing::Icon^ icon1 = gcnew System::Drawing::Icon( SystemIcons::Exclamation,40,40 );

      // Call ToBitmap to convert it.
      Bitmap^ bmp = icon1->ToBitmap();

      // Draw the bitmap.
      e->Graphics->DrawImage( bmp, Point(30,30) );
   }
private void IconToBitmap(PaintEventArgs e)
{
    // Construct an Icon.
    Icon icon1 = new Icon(SystemIcons.Exclamation, 40, 40);

    // Call ToBitmap to convert it.
    Bitmap bmp = icon1.ToBitmap();

    // Draw the bitmap.
    e.Graphics.DrawImage(bmp, new Point(30, 30));
}
Private Sub IconToBitmap(ByVal e As PaintEventArgs)

    ' Construct an Icon.
    Dim icon1 As New Icon(SystemIcons.Exclamation, 40, 40)

    ' Call ToBitmap to convert it.
    Dim bmp As Bitmap = icon1.ToBitmap()

    ' Draw the bitmap.
    e.Graphics.DrawImage(bmp, New Point(30, 30))
End Sub

注解

图标的透明区域在转换为位图时丢失,生成的位图的透明颜色设置为 RGB(13,11,12)。 返回的位图的高度和宽度与原始图标相同。

注意

从框架版本 4.6 开始,在 .ico 文件中添加了对 PNG 帧的支持。 面向框架的早期版本但在 4.6 位上运行的应用程序可以通过向 app.config 文件的 部分添加以下行来 <runtime> 选择加入新行为:<AppContextSwitchOverrides value="Switch.System.Drawing.DontSupportPngFramesInIcons=false" />

适用于