Bitmap 類別

定義

封裝 GDI+ 點陣圖,這個點陣圖是由圖形影像的像素資料及其屬性所組成。Encapsulates a GDI+ bitmap, which consists of the pixel data for a graphics image and its attributes. Bitmap 是用來處理像素資料所定義影像的物件。A Bitmap is an object used to work with images defined by pixel data.

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

範例

下列程式碼範例示範如何使用 GetPixel 和 @no__t 2 方法,從檔案中建立新的 Bitmap,以重新著色影像。The following code example demonstrates how to construct a new Bitmap from a file, using the GetPixel and SetPixel methods to recolor the image. 它也會使用 PixelFormatWidth 和 @no__t 2 屬性。It also uses the PixelFormat, Width, and Height properties.

這個範例的設計目的是搭配包含 LabelPictureBox,以及分別名為 Label1PictureBox1Button1Button 使用的 Windows Forms。This example is designed to be used with a Windows Forms that contains a Label, PictureBox, and Button named Label1, PictureBox1, and Button1, respectively. 將程式碼貼入表單,並將 Button1_Click 方法與按鈕的 Click 事件產生關聯。Paste the code into the form and associate the Button1_Click method with the button's Click event.

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

備註

點陣圖是由圖形影像及其屬性的圖元資料所組成。A bitmap consists of the pixel data for a graphics image and its attributes. 有許多標準格式可以將點陣圖儲存至檔案。There are many standard formats for saving a bitmap to a file. GDI+GDI+ 支援下列檔案格式:BMP、GIF、EXIF、JPG、PNG 和 TIFF。supports the following file formats: BMP, GIF, EXIF, JPG, PNG and TIFF. 如需支援格式的詳細資訊,請參閱點陣圖類型For more information about supported formats, see Types of Bitmaps.

您可以使用其中一個 @no__t 0 的函式,並將其儲存至資料流程或具有 Save 方法的檔案系統,以從檔案、資料流程和其他來源建立影像。You can create images from files, streams, and other sources by using one of the Bitmap constructors and save them to a stream or to the file system with the Save method. 影像會使用 Graphics 物件的 DrawImage 方法繪製到螢幕或記憶體中。Images are drawn to the screen or to memory by using the DrawImage method of the Graphics object. 如需有關處理影像檔案的主題清單,請參閱使用影像、點陣圖、圖示和元檔。For a list of topics about working with image files, see Working with Images, Bitmaps, Icons, and Metafiles.

注意

不能在應用程式域之間存取 @no__t 0 類別。The Bitmap class is not accessible across application domains. 例如,如果您建立動態 AppDomain,並在該網域中建立數筆刷、畫筆和點陣圖,然後將這些物件傳遞回主應用程式域,則可以成功使用畫筆和筆刷。For example, if you create a dynamic AppDomain and create several brushes, pens, and bitmaps in that domain, then pass these objects back to the main application domain, you can successfully use the pens and brushes. 不過,如果您呼叫 DrawImage 方法來繪製封送處理的 Bitmap,您會收到下列例外狀況。However, if you call the DrawImage method to draw the marshaled Bitmap, you receive the following exception.

遠端處理在類型「System.web. 影像」上找不到欄位「原生映射」。Remoting cannot find field "native image" on type "System.Drawing.Image".

建構函式

Bitmap(Image)

從指定的現有影像初始化 Bitmap 類別的新執行個體。Initializes a new instance of the Bitmap class from the specified existing image.

Bitmap(Image, Int32, Int32)

從指定的現有影像 (已縮放至指定之大小),初始化 Bitmap 類別的新執行個體。Initializes a new instance of the Bitmap class from the specified existing image, scaled to the specified size.

Bitmap(Image, Size)

從指定的現有影像 (已縮放至指定之大小),初始化 Bitmap 類別的新執行個體。Initializes a new instance of the Bitmap class from the specified existing image, scaled to the specified size.

Bitmap(Int32, Int32)

使用指定的大小,初始化 Bitmap 類別的新執行個體。Initializes a new instance of the Bitmap class with the specified size.

Bitmap(Int32, Int32, Graphics)

使用指定的大小,並使用指定 Bitmap 物件的解析度,初始化 Graphics 類別的新執行個體。Initializes a new instance of the Bitmap class with the specified size and with the resolution of the specified Graphics object.

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

使用指定的大小、像素格式和像素資料,初始化 Bitmap 類別的新執行個體。Initializes a new instance of the Bitmap class with the specified size, pixel format, and pixel data.

Bitmap(Int32, Int32, PixelFormat)

使用指定的大小和格式,初始化 Bitmap 類別的新執行個體。Initializes a new instance of the Bitmap class with the specified size and format.

Bitmap(Stream)

從指定的資料流,初始化 Bitmap 類別的新執行個體。Initializes a new instance of the Bitmap class from the specified data stream.

Bitmap(Stream, Boolean)

從指定的資料流,初始化 Bitmap 類別的新執行個體。Initializes a new instance of the Bitmap class from the specified data stream.

Bitmap(String)

從指定的檔案,初始化 Bitmap 類別的新執行個體。Initializes a new instance of the Bitmap class from the specified file.

Bitmap(String, Boolean)

從指定的檔案,初始化 Bitmap 類別的新執行個體。Initializes a new instance of the Bitmap class from the specified file.

Bitmap(Type, String)

從指定的資源,初始化 Bitmap 類別的新執行個體。Initializes a new instance of the Bitmap class from a specified resource.

屬性

Flags

取得這個 Image 之像素資料的屬性旗標。Gets attribute flags for the pixel data of this Image.

(繼承來源 Image)
FrameDimensionsList

取得 GUID 的陣列,表示這個 Image 中的框架 (Frame) 維度 (Dimension)。Gets an array of GUIDs that represent the dimensions of frames within this Image.

(繼承來源 Image)
Height

取得這個 Image 的高度 (單位為像素)。Gets the height, in pixels, of this Image.

(繼承來源 Image)
HorizontalResolution

取得這個 Image 的水平解析度 (單位為每英吋的像素)。Gets the horizontal resolution, in pixels per inch, of this Image.

(繼承來源 Image)
Palette

取得或設定用於這個 Image 的調色盤。Gets or sets the color palette used for this Image.

(繼承來源 Image)
PhysicalDimension

取得這個影像的寬度和高度。Gets the width and height of this image.

(繼承來源 Image)
PixelFormat

取得這個 Image 的像素格式。Gets the pixel format for this Image.

(繼承來源 Image)
PropertyIdList

取得儲存在這個 Image 中的屬性項目 ID。Gets IDs of the property items stored in this Image.

(繼承來源 Image)
PropertyItems

取得儲存在這個 Image 中的所有屬性項目 (中繼資料的各個項目)。Gets all the property items (pieces of metadata) stored in this Image.

(繼承來源 Image)
RawFormat

取得這個 Image 的檔案格式。Gets the file format of this Image.

(繼承來源 Image)
Size

取得影像的寬度和高度,單位為像素。Gets the width and height, in pixels, of this image.

(繼承來源 Image)
Tag

取得或設定物件,該物件提供影像的其他相關資料。Gets or sets an object that provides additional data about the image.

(繼承來源 Image)
VerticalResolution

取得這個 Image 的垂直解析度 (單位為每英吋的像素)。Gets the vertical resolution, in pixels per inch, of this Image.

(繼承來源 Image)
Width

取得這個 Image 的寬度 (單位為像素)。Gets the width, in pixels, of this Image.

(繼承來源 Image)

方法

Clone()

建立這個 Image 的完全相同複本。Creates an exact copy of this Image.

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

建立藉由 Bitmap 結構和使用指定 Rectangle 列舉型別定義的 PixelFormat 之區段複本。Creates a copy of the section of this Bitmap defined by Rectangle structure and with a specified PixelFormat enumeration.

Clone(RectangleF, PixelFormat)

建立使用指定 Bitmap 定義的這個 PixelFormat 之區段複本。Creates a copy of the section of this Bitmap defined with a specified PixelFormat enumeration.

CreateObjRef(Type)

建立包含所有相關資訊的物件,這些資訊是產生用來與遠端物件通訊的所需 Proxy。Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.

(繼承來源 MarshalByRefObject)
Dispose()

釋放這個 Image 所使用的所有資源。Releases all resources used by this Image.

(繼承來源 Image)
Dispose(Boolean)

釋放 Image 所使用的 Unmanaged 資源,並選擇性地釋放 Managed 資源。Releases the unmanaged resources used by the Image and optionally releases the managed resources.

(繼承來源 Image)
Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(繼承來源 Object)
FromHicon(IntPtr)

從圖示的 Windows 控制代碼建立 BitmapCreates a Bitmap from a Windows handle to an icon.

FromResource(IntPtr, String)

從指定的 Windows 資源建立 BitmapCreates a Bitmap from the specified Windows resource.

GetBounds(GraphicsUnit)

取得使用指定單位之影像的界限。Gets the bounds of the image in the specified unit.

(繼承來源 Image)
GetEncoderParameterList(Guid)

傳回指定的影像編碼器支援的參數資訊。Returns information about the parameters supported by the specified image encoder.

(繼承來源 Image)
GetFrameCount(FrameDimension)

傳回指定維度的框架數。Returns the number of frames of the specified dimension.

(繼承來源 Image)
GetHashCode()

作為預設雜湊函數。Serves as the default hash function.

(繼承來源 Object)
GetHbitmap()

從這個 Bitmap 建立 GDIGDI 點陣圖物件。Creates a GDIGDI bitmap object from this Bitmap.

GetHbitmap(Color)

從這個 Bitmap 建立 GDIGDI 點陣圖物件。Creates a GDIGDI bitmap object from this Bitmap.

GetHicon()

傳回圖示的控制代碼。Returns the handle to an icon.

GetLifetimeService()

擷取控制這個執行個體存留期 (Lifetime) 原則的目前存留期服務物件。Retrieves the current lifetime service object that controls the lifetime policy for this instance.

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

取得這個 Bitmap 中指定像素的色彩。Gets the color of the specified pixel in this Bitmap.

GetPropertyItem(Int32)

從這個 Image 取得指定的屬性項目。Gets the specified property item from this Image.

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

傳回這個 Image 的縮圖。Returns a thumbnail for this Image.

(繼承來源 Image)
GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(繼承來源 Object)
InitializeLifetimeService()

取得存留期服務物件,以控制這個執行個體的存留期原則。Obtains a lifetime service object to control the lifetime policy for this instance.

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

Bitmap 鎖定在系統記憶體內。Locks a Bitmap into system memory.

LockBits(Rectangle, ImageLockMode, PixelFormat, BitmapData)

Bitmap 鎖定在系統記憶體內。Locks a Bitmap into system memory.

MakeTransparent()

可讓這個 Bitmap的預設透明色彩為透明。Makes the default transparent color transparent for this Bitmap.

MakeTransparent(Color)

將此 Bitmap 的指定色彩變為透明。Makes the specified color transparent for this Bitmap.

MemberwiseClone()

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

(繼承來源 Object)
MemberwiseClone(Boolean)

建立目前 MarshalByRefObject 物件的淺層複本。Creates a shallow copy of the current MarshalByRefObject object.

(繼承來源 MarshalByRefObject)
RemovePropertyItem(Int32)

將指定的屬性項目從這個 Image 中移除。Removes the specified property item from this Image.

(繼承來源 Image)
RotateFlip(RotateFlipType)

旋轉、翻轉或者同時旋轉和翻轉 ImageRotates, flips, or rotates and flips the Image.

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

使用指定的編碼器和影像編碼器參數,將此影像儲存至指定的資料流。Saves this image to the specified stream, with the specified encoder and image encoder parameters.

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

將這個影像以指定的格式儲存至指定的資料流。Saves this image to the specified stream in the specified format.

(繼承來源 Image)
Save(String)

儲存這個 Image 至指定的檔案或資料流。Saves this Image to the specified file or stream.

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

使用指定的編碼器和影像編碼器參數,將此 Image 儲存至指定的檔案。Saves this Image to the specified file, with the specified encoder and image-encoder parameters.

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

將這個 Image 以指定的格式儲存至指定的檔案。Saves this Image to the specified file in the specified format.

(繼承來源 Image)
SaveAdd(EncoderParameters)

將框架加入先前的 Save 方法呼叫所指定的檔案或資料流。Adds a frame to the file or stream specified in a previous call to the Save method. 使用這個方法以從多重框架影像儲存選取框架至另一個多重框架影像。Use this method to save selected frames from a multiple-frame image to another multiple-frame image.

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

將框架加入先前的 Save 方法呼叫所指定的檔案或資料流。Adds a frame to the file or stream specified in a previous call to the Save method.

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

選取由維度和索引所指定的框架。Selects the frame specified by the dimension and index.

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

設定此 Bitmap 中指定之像素的色彩。Sets the color of the specified pixel in this Bitmap.

SetPropertyItem(PropertyItem)

在這個 Image 中儲存屬性項目 (中繼資料的項目)。Stores a property item (piece of metadata) in this Image.

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

設定這個 Bitmap 的解析度。Sets the resolution for this Bitmap.

ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(繼承來源 Object)
UnlockBits(BitmapData)

從系統記憶體解除鎖定這個 BitmapUnlocks this Bitmap from system memory.

明確介面實作

ISerializable.GetObjectData(SerializationInfo, StreamingContext)

將序列化目標物件所需的資料填入 SerializationInfoPopulates a SerializationInfo with the data needed to serialize the target object.

(繼承來源 Image)

適用於

另請參閱