語言

Bitmap.MakeTransparent 方法

定義

會讓預設的透明色變透明 Bitmap。

多載

名稱 Description
MakeTransparent()

會讓預設的透明色變透明 Bitmap。

MakeTransparent(Color)

使指定 Bitmap顏色透明。

MakeTransparent()

來源:
Bitmap.cs
來源:
Bitmap.cs
來源:
Bitmap.cs
來源:
Bitmap.cs
來源:
Bitmap.cs
來源:
Bitmap.cs
來源:
Bitmap.cs
來源:
Bitmap.cs
來源:
Bitmap.cs

會讓預設的透明色變透明 Bitmap。

public:
 void MakeTransparent();
public void MakeTransparent();
member this.MakeTransparent : unit -> unit
Public Sub MakeTransparent ()

例外狀況

該 Bitmap 影像格式為圖示格式。

作業失敗。

範例

以下程式碼範例是為 Windows Forms 設計的,並需要 PaintEventArgse,這是 Paint 事件處理程序的參數。 程式碼會讓系統預設透明色,然後 myBitmap將 Bitmap 繪製到螢幕上。

private:
   void MakeTransparent_Example1( PaintEventArgs^ e )
   {
      // Create a Bitmap object from an image file.
      Bitmap^ myBitmap = gcnew Bitmap( "Grapes.gif" );

      // Draw myBitmap to the screen.
      e->Graphics->DrawImage( myBitmap, 0, 0, myBitmap->Width, myBitmap->Height );

      // Make the default transparent color transparent for myBitmap.
      myBitmap->MakeTransparent();

      // Draw the transparent bitmap to the screen.
      e->Graphics->DrawImage( myBitmap, myBitmap->Width, 0, myBitmap->Width, myBitmap->Height );
   }
private void MakeTransparent_Example1(PaintEventArgs e)
{

    // Create a Bitmap object from an image file.
    Bitmap myBitmap = new Bitmap("Grapes.gif");

    // Draw myBitmap to the screen.
    e.Graphics.DrawImage(myBitmap, 0, 0, myBitmap.Width,
        myBitmap.Height);

    // Make the default transparent color transparent for myBitmap.
    myBitmap.MakeTransparent();

    // Draw the transparent bitmap to the screen.
    e.Graphics.DrawImage(myBitmap, myBitmap.Width, 0,
        myBitmap.Width, myBitmap.Height);
}
Private Sub MakeTransparent_Example1(ByVal e As PaintEventArgs)

    ' Create a Bitmap object from an image file.
    Dim myBitmap As New Bitmap("Grapes.gif")

    ' Draw myBitmap to the screen.
    e.Graphics.DrawImage(myBitmap, 0, 0, myBitmap.Width, _
    myBitmap.Height)

    ' Make the default transparent color transparent for myBitmap.
    myBitmap.MakeTransparent()

    ' Draw the transparent bitmap to the screen.
    e.Graphics.DrawImage(myBitmap, myBitmap.Width, 0, myBitmap.Width, _
    myBitmap.Height)
End Sub

備註

系統調色盤定義一種顏色為預設的透明色(或稱 alpha 色)。 此方法使預設 Bitmap透明色變透明。 若系統未指定透明色,則 LightGray 為透明色。

當你呼叫 MakeTransparent時,點陣圖會被轉換成該 Format32bppArgb 格式,因為此格式支援 alpha 通道。

適用於

MakeTransparent(Color)

來源:
Bitmap.cs
來源:
Bitmap.cs
來源:
Bitmap.cs
來源:
Bitmap.cs
來源:
Bitmap.cs
來源:
Bitmap.cs
來源:
Bitmap.cs
來源:
Bitmap.cs
來源:
Bitmap.cs

使指定 Bitmap顏色透明。

public:
 void MakeTransparent(System::Drawing::Color transparentColor);
public void MakeTransparent(System.Drawing.Color transparentColor);
member this.MakeTransparent : System.Drawing.Color -> unit
Public Sub MakeTransparent (transparentColor As Color)

參數

transparentColor
Color

Color代表顏色的結構,讓它變得透明。

例外狀況

該 Bitmap 影像格式為圖示格式。

作業失敗。

範例

以下程式碼範例是為 Windows Forms 設計的,並需要 PaintEventArgse,這是 Paint 事件處理程序的參數。 此程式碼會執行下列動作:

  • 會得到像素 Bitmap的顏色。

  • 這樣可以讓那種顏色對位圖來說透明。

  • 把它 Bitmap 吸引到螢幕上。

private:
   void MakeTransparent_Example2( PaintEventArgs^ e )
   {
      // Create a Bitmap object from an image file.
      Bitmap^ myBitmap = gcnew Bitmap( "Grapes.gif" );

      // Draw myBitmap to the screen.
      e->Graphics->DrawImage( myBitmap, 0, 0, myBitmap->Width, myBitmap->Height );

      // Get the color of a background pixel.
      Color backColor = myBitmap->GetPixel( 1, 1 );

      // Make backColor transparent for myBitmap.
      myBitmap->MakeTransparent( backColor );

      // Draw the transparent bitmap to the screen.
      e->Graphics->DrawImage( myBitmap, myBitmap->Width, 0, myBitmap->Width, myBitmap->Height );
   }
private void MakeTransparent_Example2(PaintEventArgs e)
{

    // Create a Bitmap object from an image file.
    Bitmap myBitmap = new Bitmap("Grapes.gif");

    // Draw myBitmap to the screen.
    e.Graphics.DrawImage(
        myBitmap, 0, 0, myBitmap.Width, myBitmap.Height);

    // Get the color of a background pixel.
    Color backColor = myBitmap.GetPixel(1, 1);

    // Make backColor transparent for myBitmap.
    myBitmap.MakeTransparent(backColor);

    // Draw the transparent bitmap to the screen.
    e.Graphics.DrawImage(
        myBitmap, myBitmap.Width, 0, myBitmap.Width, myBitmap.Height);
}
Private Sub MakeTransparent_Example2(ByVal e As PaintEventArgs)

    ' Create a Bitmap object from an image file.
    Dim myBitmap As New Bitmap("Grapes.gif")

    ' Draw myBitmap to the screen.
    e.Graphics.DrawImage(myBitmap, 0, 0, myBitmap.Width, _
        myBitmap.Height)

    ' Get the color of a background pixel.
    Dim backColor As Color = myBitmap.GetPixel(1, 1)

    ' Make backColor transparent for myBitmap.
    myBitmap.MakeTransparent(backColor)

    ' Draw the transparent bitmap to the screen.
    e.Graphics.DrawImage(myBitmap, myBitmap.Width, 0, myBitmap.Width, _
        myBitmap.Height)
End Sub

備註

當你呼叫 MakeTransparent時,點陣圖會被轉換成該 Format32bppArgb 格式,因為此格式支援 alpha 通道。

適用於