Bitmap 類別

定義

封裝 GDI+ 點陣圖,這個點陣圖是由圖形影像的像素資料及其屬性所組成。 Bitmap 是用來處理像素資料所定義影像的物件。

public ref class Bitmap sealed : System::Drawing::Image
public sealed class Bitmap : System.Drawing.Image
[System.Serializable]
public sealed class Bitmap : System.Drawing.Image
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class Bitmap : System.Drawing.Image
type Bitmap = class
    inherit Image
[<System.Serializable>]
type Bitmap = class
    inherit Image
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type Bitmap = class
    inherit Image
Public NotInheritable Class Bitmap
Inherits Image
繼承
屬性

範例

下列程式碼範例示範如何使用 和 SetPixel 方法來從檔案建構新的 BitmapGetPixel 以重新著色影像。 它也會使用 PixelFormatWidthHeight 屬性。

此範例的設計目的是要分別與包含 Label 、 和 、 和 ButtonButton1PictureBox1Label1 的Windows Forms搭配使用。 PictureBox 將程式碼貼到表單中,並將 Button1_Click 方法與按鈕的事件 Click 產生關聯。

private:
   Bitmap^ image1;
   void Button1_Click( System::Object^ /*sender*/, System::EventArgs^ /*e*/ )
   {
      try
      {
         
         // Retrieve the image.
         image1 = gcnew Bitmap( "C:\\Documents and Settings\\All Users\\"
         "Documents\\My Music\\music.bmp",true );
         int x;
         int y;
         
         // Loop through the images pixels to reset color.
         for ( x = 0; x < image1->Width; x++ )
         {
            for ( y = 0; y < image1->Height; y++ )
            {
               Color pixelColor = image1->GetPixel( x, y );
               Color newColor = Color::FromArgb( pixelColor.R, 0, 0 );
               image1->SetPixel( x, y, newColor );

            }

         }
         
         // Set the PictureBox to display the image.
         PictureBox1->Image = image1;
         
         // Display the pixel format in Label1.
         Label1->Text = String::Format( "Pixel format: {0}", image1->PixelFormat );
      }
      catch ( ArgumentException^ ) 
      {
         MessageBox::Show( "There was an error."
         "Check the path to the image file." );
      }

   }
Bitmap image1;

private void Button1_Click(System.Object sender, System.EventArgs e)
{

    try
    {
        // Retrieve the image.
        image1 = new Bitmap(@"C:\Documents and Settings\All Users\" 
            + @"Documents\My Music\music.bmp", true);

        int x, y;

        // Loop through the images pixels to reset color.
        for(x=0; x<image1.Width; x++)
        {
            for(y=0; y<image1.Height; y++)
            {
                Color pixelColor = image1.GetPixel(x, y);
                Color newColor = Color.FromArgb(pixelColor.R, 0, 0);
                image1.SetPixel(x, y, newColor);
            }
        }

        // Set the PictureBox to display the image.
        PictureBox1.Image = image1;

        // Display the pixel format in Label1.
        Label1.Text = "Pixel format: "+image1.PixelFormat.ToString();
    }
    catch(ArgumentException)
    {
        MessageBox.Show("There was an error." +
            "Check the path to the image file.");
    }
}
Dim image1 As Bitmap

Private Sub Button1_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button1.Click

    Try
        ' Retrieve the image.
        image1 = New Bitmap( _
            "C:\Documents and Settings\All Users\Documents\My Music\music.bmp", _
            True)

        Dim x, y As Integer

        ' Loop through the images pixels to reset color.
        For x = 0 To image1.Width - 1
            For y = 0 To image1.Height - 1
                Dim pixelColor As Color = image1.GetPixel(x, y)
                Dim newColor As Color = _
                    Color.FromArgb(pixelColor.R, 0, 0)
                image1.SetPixel(x, y, newColor)
            Next
        Next

        ' Set the PictureBox to display the image.
        PictureBox1.Image = image1

        ' Display the pixel format in Label1.
        Label1.Text = "Pixel format: " + image1.PixelFormat.ToString()

    Catch ex As ArgumentException
        MessageBox.Show("There was an error." _
            & "Check the path to the image file.")
    End Try
End Sub

備註

點陣圖是由圖形影像及其屬性的圖元資料所組成。 有許多標準格式可將點陣圖儲存至檔案。 GDI+ 支援下列檔案格式:BMP、GIF、EXIF、JPG、PNG 和 TIFF。 如需支援格式的詳細資訊,請參閱 點陣圖的類型

您可以使用其中 Bitmap 一個建構函式,從檔案、資料流程和其他來源建立影像,並使用 方法將它們儲存至資料流程或檔案系統 Save 。 影像會使用 DrawImage 物件的 方法 Graphics 繪製到螢幕或記憶體。 如需使用影像檔的主題清單,請參閱 使用影像、點陣圖、圖示和中繼檔

注意

在 .NET 6 和更新版本中,只有 Windows 作業系統才支援包含此類型的 System.Drawing.Common 套件。 在跨平臺應用程式中使用此類型會導致編譯時期警告和執行時間例外狀況。 如需詳細資訊,請參閱 僅限 Windows 上支援的 System.Drawing.Common

注意

類別 Bitmap 無法跨應用程式域存取。 例如,如果您建立動態 AppDomain ,並在該網域中建立數個筆刷、畫筆和點陣圖,然後將這些物件傳回主要應用程式域,您可以成功使用畫筆和筆刷。 不過,如果您呼叫 DrawImage 方法來繪製封送處理 Bitmap 的方法,您會收到下列例外狀況:

遠端處理在類型 「System.Drawing.Image」 上找不到欄位「原生影像」。

建構函式

Bitmap(Image)

從指定的現有影像初始化 Bitmap 類別的新執行個體。

Bitmap(Image, Int32, Int32)

從指定的現有影像 (已縮放至指定之大小),初始化 Bitmap 類別的新執行個體。

Bitmap(Image, Size)

從指定的現有影像 (已縮放至指定之大小),初始化 Bitmap 類別的新執行個體。

Bitmap(Int32, Int32)

使用指定的大小,初始化 Bitmap 類別的新執行個體。

Bitmap(Int32, Int32, Graphics)

使用指定的大小,並使用指定 Bitmap 物件的解析度,初始化 Graphics 類別的新執行個體。

Bitmap(Int32, Int32, Int32, PixelFormat, IntPtr)

使用指定的大小、像素格式和像素資料,初始化 Bitmap 類別的新執行個體。

Bitmap(Int32, Int32, PixelFormat)

使用指定的大小和格式,初始化 Bitmap 類別的新執行個體。

Bitmap(Stream)

從指定的資料流,初始化 Bitmap 類別的新執行個體。

Bitmap(Stream, Boolean)

從指定的資料流,初始化 Bitmap 類別的新執行個體。

Bitmap(String)

從指定的檔案,初始化 Bitmap 類別的新執行個體。

Bitmap(String, Boolean)

從指定的檔案,初始化 Bitmap 類別的新執行個體。

Bitmap(Type, String)

從指定的資源,初始化 Bitmap 類別的新執行個體。

屬性

Flags

取得這個 Image 之像素資料的屬性旗標。

(繼承來源 Image)
FrameDimensionsList

取得 GUID 的陣列,表示這個 Image 中的框架 (Frame) 維度 (Dimension)。

(繼承來源 Image)
Height

取得這個 Image 的高度 (單位為像素)。

(繼承來源 Image)
HorizontalResolution

取得這個 Image 的水平解析度 (單位為每英吋的像素)。

(繼承來源 Image)
Palette

取得或設定用於這個 Image 的調色盤。

(繼承來源 Image)
PhysicalDimension

取得這個影像的寬度和高度。

(繼承來源 Image)
PixelFormat

取得這個 Image 的像素格式。

(繼承來源 Image)
PropertyIdList

取得儲存在這個 Image 中的屬性項目 ID。

(繼承來源 Image)
PropertyItems

取得儲存在這個 Image 中的所有屬性項目 (中繼資料的各個項目)。

(繼承來源 Image)
RawFormat

取得這個 Image 的檔案格式。

(繼承來源 Image)
Size

取得影像的寬度和高度,單位為像素。

(繼承來源 Image)
Tag

取得或設定物件,該物件提供影像的其他相關資料。

(繼承來源 Image)
VerticalResolution

取得這個 Image 的垂直解析度 (單位為每英吋的像素)。

(繼承來源 Image)
Width

取得這個 Image 的寬度 (單位為像素)。

(繼承來源 Image)

方法

ApplyEffect(Effect, Rectangle)

封裝 GDI+ 點陣圖,這個點陣圖是由圖形影像的像素資料及其屬性所組成。 Bitmap 是用來處理像素資料所定義影像的物件。

Clone()

建立這個 Image 的完全相同複本。

(繼承來源 Image)
Clone(Rectangle, PixelFormat)

建立藉由 Bitmap 結構和使用指定 Rectangle 列舉型別定義的 PixelFormat 之區段複本。

Clone(RectangleF, PixelFormat)

建立使用指定 Bitmap 定義的這個 PixelFormat 之區段複本。

ConvertFormat(PixelFormat)

封裝 GDI+ 點陣圖,這個點陣圖是由圖形影像的像素資料及其屬性所組成。 Bitmap 是用來處理像素資料所定義影像的物件。

ConvertFormat(PixelFormat, DitherType, PaletteType, ColorPalette, Single)

封裝 GDI+ 點陣圖,這個點陣圖是由圖形影像的像素資料及其屬性所組成。 Bitmap 是用來處理像素資料所定義影像的物件。

CreateObjRef(Type)

建立包含所有相關資訊的物件,這些資訊是產生用來與遠端物件通訊的所需 Proxy。

(繼承來源 MarshalByRefObject)
Dispose()

釋放這個 Image 所使用的所有資源。

(繼承來源 Image)
Dispose(Boolean)

釋放 Image 所使用的 Unmanaged 資源,並選擇性地釋放 Managed 資源。

(繼承來源 Image)
Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
FromHicon(IntPtr)

從圖示的 Windows 控制代碼建立 Bitmap

FromResource(IntPtr, String)

從指定的 Windows 資源建立 Bitmap

GetBounds(GraphicsUnit)

取得使用指定單位之影像的界限。

(繼承來源 Image)
GetEncoderParameterList(Guid)

傳回指定的影像編碼器支援的參數資訊。

(繼承來源 Image)
GetFrameCount(FrameDimension)

傳回指定維度的框架數。

(繼承來源 Image)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetHbitmap()

從這個 Bitmap 建立 GDI 點陣圖物件。

GetHbitmap(Color)

從這個 Bitmap 建立 GDI 點陣圖物件。

GetHicon()

傳回圖示的控制代碼。

GetLifetimeService()
已淘汰.

擷取控制這個執行個體存留期 (Lifetime) 原則的目前存留期服務物件。

(繼承來源 MarshalByRefObject)
GetPixel(Int32, Int32)

取得這個 Bitmap 中指定像素的色彩。

GetPropertyItem(Int32)

從這個 Image 取得指定的屬性項目。

(繼承來源 Image)
GetThumbnailImage(Int32, Int32, Image+GetThumbnailImageAbort, IntPtr)

傳回這個 Image 的縮圖。

(繼承來源 Image)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
InitializeLifetimeService()
已淘汰.

取得存留期服務物件,以控制這個執行個體的存留期原則。

(繼承來源 MarshalByRefObject)
LockBits(Rectangle, ImageLockMode, PixelFormat)

Bitmap 鎖定在系統記憶體內。

LockBits(Rectangle, ImageLockMode, PixelFormat, BitmapData)

Bitmap 鎖定在系統記憶體內。

MakeTransparent()

可讓這個 Bitmap的預設透明色彩為透明。

MakeTransparent(Color)

將此 Bitmap 的指定色彩變為透明。

MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
MemberwiseClone(Boolean)

建立目前 MarshalByRefObject 物件的淺層複本。

(繼承來源 MarshalByRefObject)
RemovePropertyItem(Int32)

將指定的屬性項目從這個 Image 中移除。

(繼承來源 Image)
RotateFlip(RotateFlipType)

旋轉、翻轉或者同時旋轉和翻轉 Image

(繼承來源 Image)
Save(Stream, ImageCodecInfo, EncoderParameters)

使用指定的編碼器和影像編碼器參數,將此影像儲存至指定的資料流。

(繼承來源 Image)
Save(Stream, ImageFormat)

將這個影像以指定的格式儲存至指定的資料流。

(繼承來源 Image)
Save(String)

儲存這個 Image 至指定的檔案或資料流。

(繼承來源 Image)
Save(String, ImageCodecInfo, EncoderParameters)

使用指定的編碼器和影像編碼器參數,將此 Image 儲存至指定的檔案。

(繼承來源 Image)
Save(String, ImageFormat)

將這個 Image 以指定的格式儲存至指定的檔案。

(繼承來源 Image)
SaveAdd(EncoderParameters)

將框架加入先前的 Save 方法呼叫所指定的檔案或資料流。 使用這個方法以從多重框架影像儲存選取框架至另一個多重框架影像。

(繼承來源 Image)
SaveAdd(Image, EncoderParameters)

將框架加入先前的 Save 方法呼叫所指定的檔案或資料流。

(繼承來源 Image)
SelectActiveFrame(FrameDimension, Int32)

選取由維度和索引所指定的框架。

(繼承來源 Image)
SetPixel(Int32, Int32, Color)

設定此 Bitmap 中指定之像素的色彩。

SetPropertyItem(PropertyItem)

在這個 Image 中儲存屬性項目 (中繼資料的項目)。

(繼承來源 Image)
SetResolution(Single, Single)

設定這個 Bitmap 的解析度。

ToString()

傳回代表目前物件的字串。

(繼承來源 Object)
UnlockBits(BitmapData)

從系統記憶體解除鎖定這個 Bitmap

明確介面實作

ISerializable.GetObjectData(SerializationInfo, StreamingContext)

將序列化目標物件所需的資料填入 SerializationInfo

(繼承來源 Image)

適用於

另請參閱