Bitmap 建構函式

定義

初始化 Bitmap 類別的新執行個體。Initializes a new instance of the Bitmap class.

多載

Bitmap(Image)

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

Bitmap(Stream)

從指定的資料流,初始化 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(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(Stream, Boolean)

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

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.

Bitmap(Image, Int32, Int32)

從指定的現有影像 (已縮放至指定之大小),初始化 Bitmap 類別的新執行個體。Initializes a new instance of the Bitmap class from the specified existing image, scaled to 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, PixelFormat)

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

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(Image)

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

public:
 Bitmap(System::Drawing::Image ^ original);
public Bitmap (System.Drawing.Image original);
new System.Drawing.Bitmap : System.Drawing.Image -> System.Drawing.Bitmap
Public Sub New (original As Image)

參數

original
Image

Image,用於建立新的 BitmapThe Image from which to create the new Bitmap.

Bitmap(Stream)

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

public:
 Bitmap(System::IO::Stream ^ stream);
public Bitmap (System.IO.Stream stream);
new System.Drawing.Bitmap : System.IO.Stream -> System.Drawing.Bitmap

參數

stream
Stream

用來載入影像的資料流。The data stream used to load the image.

例外狀況

stream 未含有影像資料或為 nullstream does not contain image data or is null.

-或--or- stream 含有一個 PNG 影像檔案,其中有一個維度超過 65,535 個像素。stream contains a PNG image file with a single dimension greater than 65,535 pixels.

範例

下列程式碼範例示範如何從資料流程載入點陣圖。The following code example demonstrates how to load a bitmap from a stream.

這個範例是設計來與 Windows Forms 搭配使用。This example is designed to be used with Windows Forms. 建立一個表單,其中包含名為 PictureBox1 的 @no__t 0。Create a form that contains a PictureBox named PictureBox1. 將程式碼貼入表單,並從表單的「函式」或 Load 事件處理方法呼叫 InitializeStreamBitmap 方法。Paste the code into the form and call the InitializeStreamBitmap method from the form's constructor or Load event-handling method.

void InitializeStreamBitmap()
{
   try
   {
      System::Net::WebRequest^ request = System::Net::WebRequest::Create( "http://www.microsoft.com//h/en-us/r/ms_masthead_ltr.gif" );
      System::Net::WebResponse^ response = request->GetResponse();
      System::IO::Stream^ responseStream = response->GetResponseStream();
      Bitmap^ bitmap2 = gcnew Bitmap( responseStream );
      PictureBox1->Image = bitmap2;
   }
   catch ( System::Net::WebException^ ) 
   {
      MessageBox::Show( "There was an error opening the image file."
      "Check the URL" );
   }

}
private void InitializeStreamBitmap()
{
    try
    {
        System.Net.WebRequest request = 
            System.Net.WebRequest.Create(
            "http://www.microsoft.com//h/en-us/r/ms_masthead_ltr.gif");
        System.Net.WebResponse response = request.GetResponse();
        System.IO.Stream responseStream = 
            response.GetResponseStream();
        Bitmap bitmap2 = new Bitmap(responseStream);
        PictureBox1.Image = bitmap2;

    }
    catch(System.Net.WebException)
    {
        MessageBox.Show("There was an error opening the image file."
           + "Check the URL");
    }
}
Private Sub InitializeStreamBitmap()
    Try
        Dim request As System.Net.WebRequest = _
            System.Net.WebRequest.Create( _
            "http://www.microsoft.com//h/en-us/r/ms_masthead_ltr.gif")
        Dim response As System.Net.WebResponse = request.GetResponse()
        Dim responseStream As System.IO.Stream = response.GetResponseStream()
        Dim bitmap2 As New Bitmap(responseStream)
        PictureBox1.Image = bitmap2

    Catch ex As System.Net.WebException
        MessageBox.Show("There was an error opening the image file. Check the URL")
    End Try
End Sub

備註

您必須將資料流程保持開啟狀態,Bitmap 的存留期。You must keep the stream open for the lifetime of the Bitmap.

由於 GDI+GDI+ 解碼器的限制,如果您從單一維度大於65535圖元的 .png 影像檔來建立點陣圖,則會擲回 System.ArgumentExceptionDue to a limitation of the GDI+GDI+ decoder, an System.ArgumentException is thrown if you construct a bitmap from a .png image file with a single dimension greater than 65,535 pixels.

另請參閱

Bitmap(String)

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

public:
 Bitmap(System::String ^ filename);
public Bitmap (string filename);
new System.Drawing.Bitmap : string -> System.Drawing.Bitmap
Public Sub New (filename As String)

參數

filename
String

點陣圖檔名稱和路徑。The bitmap file name and path.

例外狀況

找不到指定的檔案。The specified file is not found.

備註

檔案名和路徑可以相對於應用程式或絕對路徑。The file name and path can be relative to the application or an absolute path. 使用此函式來開啟具有下列檔案格式的影像:BMP、GIF、EXIF、JPG、PNG 和 TIFF。Use this constructor to open images with the following file formats: BMP, GIF, EXIF, JPG, PNG and TIFF. 如需支援格式的詳細資訊,請參閱點陣圖類型For more information about supported formats, see Types of Bitmaps. 檔案會保持鎖定,直到處置 Bitmap 為止。The file remains locked until the Bitmap is disposed.

另請參閱

Bitmap(Image, Size)

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

public:
 Bitmap(System::Drawing::Image ^ original, System::Drawing::Size newSize);
public Bitmap (System.Drawing.Image original, System.Drawing.Size newSize);
new System.Drawing.Bitmap : System.Drawing.Image * System.Drawing.Size -> System.Drawing.Bitmap
Public Sub New (original As Image, newSize As Size)

參數

original
Image

Image,用於建立新的 BitmapThe Image from which to create the new Bitmap.

newSize
Size

Size 結構,表示新 Bitmap 的大小。The Size structure that represent the size of the new Bitmap.

例外狀況

作業失敗。The operation failed.

Bitmap(Int32, Int32)

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

public:
 Bitmap(int width, int height);
public Bitmap (int width, int height);
new System.Drawing.Bitmap : int * int -> System.Drawing.Bitmap
Public Sub New (width As Integer, height As Integer)

參數

width
Int32

Bitmap 的寬度 (以像素為單位)。The width, in pixels, of the new Bitmap.

height
Int32

Bitmap 的高度 (以像素為單位)。The height, in pixels, of the new Bitmap.

例外狀況

作業失敗。The operation failed.

備註

此函式會建立 Bitmap,具有 Format32bppArgb 的 @no__t 1 列舉值。This constructor creates a Bitmap with a PixelFormat enumeration value of Format32bppArgb.

Bitmap(Stream, Boolean)

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

public:
 Bitmap(System::IO::Stream ^ stream, bool useIcm);
public Bitmap (System.IO.Stream stream, bool useIcm);
new System.Drawing.Bitmap : System.IO.Stream * bool -> System.Drawing.Bitmap

參數

stream
Stream

用來載入影像的資料流。The data stream used to load the image.

useIcm
Boolean

true 表示要對此 Bitmap 進行色彩修正,否則為 falsetrue to use color correction for this Bitmap; otherwise, false.

例外狀況

stream 未含有影像資料或為 nullstream does not contain image data or is null.

-或--or- stream 含有一個 PNG 影像檔案,其中有一個維度超過 65,535 個像素。stream contains a PNG image file with a single dimension greater than 65,535 pixels.

備註

您必須將資料流程保持開啟狀態,Bitmap 的存留期。You must keep the stream open for the lifetime of the Bitmap.

由於 GDI+GDI+ 解碼器的限制,如果您從單一維度大於65535圖元的 .png 影像檔來建立點陣圖,則會擲回 System.ArgumentExceptionDue to a limitation of the GDI+GDI+ decoder, an System.ArgumentException is thrown if you construct a bitmap from a .png image file with a single dimension greater than 65,535 pixels.

另請參閱

Bitmap(String, Boolean)

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

public:
 Bitmap(System::String ^ filename, bool useIcm);
public Bitmap (string filename, bool useIcm);
new System.Drawing.Bitmap : string * bool -> System.Drawing.Bitmap
Public Sub New (filename As String, useIcm As Boolean)

參數

filename
String

點陣圖檔的名稱。The name of the bitmap file.

useIcm
Boolean

true 表示要對此 Bitmap 進行色彩修正,否則為 falsetrue to use color correction for this Bitmap; otherwise, false.

範例

下列程式碼範例示範如何從檔案中建立新的點陣圖。The following code example demonstrates how to construct a new bitmap from a file. 此範例使用 GetPixelSetPixel 方法來重新著色影像。The example uses the GetPixel and SetPixel methods to recolor the image. 它也會使用 PixelFormat 屬性。It also uses the PixelFormat property.

這個範例的設計目的是要與 Windows Form 搭配使用,其中包含 LabelPictureBox,分別名為 Label1PictureBox1Button1 的 @no__t 2。This example is designed to be used with a Windows Form 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

備註

使用此函式來開啟具有下列檔案格式的影像:BMP、GIF、EXIF、JPG、PNG 和 TIFF。Use this constructor to open images with the following file formats: BMP, GIF, EXIF, JPG, PNG and TIFF. 如需支援格式的詳細資訊,請參閱點陣圖類型For more information about supported formats, see Types of Bitmaps. 檔案會保持鎖定,直到處置 Bitmap 為止。The file remains locked until the Bitmap is disposed.

安全性

SecurityPermission
用於呼叫非受控碼。for calling into unmanaged code. 相關列舉: UnmanagedCodeRelated enumeration: UnmanagedCode

另請參閱

Bitmap(Type, String)

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

public:
 Bitmap(Type ^ type, System::String ^ resource);
public Bitmap (Type type, string resource);
new System.Drawing.Bitmap : Type * string -> System.Drawing.Bitmap

參數

type
Type

用來擷取資源的類別。The class used to extract the resource.

resource
String

資源名稱。The name of the resource.

範例

下列程式碼範例示範如何從類型中建立點陣圖,以及如何使用 Save 方法。The following code example demonstrates how to construct a bitmap from a type, and how to use the Save method. 若要執行此範例,請將程式碼貼入 Windows Form。To run this example, paste the code into a Windows Form. 處理表單的 Paint 事件並呼叫 @no__t 1 方法,傳遞 e 作為 PaintEventArgsHandle the form's Paint event and call the ConstructFromResourceSaveAsGif method, passing e as PaintEventArgs

private:
    void ConstructFromResourceSaveAsGif(PaintEventArgs^ e)
    {
        // Construct a bitmap from the button image resource.
        Bitmap^ bmp1 = gcnew Bitmap(Button::typeid, "Button.bmp");
        String^ savePath =  
            Environment::GetEnvironmentVariable("TEMP") + "\\Button.bmp";

        try
        {
            // Save the image as a GIF.
            bmp1->Save(savePath, System::Drawing::Imaging::ImageFormat::Gif);
        }
        catch (IOException^)
        {
            // Carry on regardless
        }

        // Construct a new image from the GIF file.
        Bitmap^ bmp2 = nullptr;
        if (File::Exists(savePath))
        {
            bmp2 = gcnew Bitmap(savePath);
        }

        // Draw the two images.
        e->Graphics->DrawImage(bmp1, Point(10, 10));

        // If bmp1 did not save to disk, bmp2 may be null
        if (bmp2 != nullptr)
        {
            e->Graphics->DrawImage(bmp2, Point(10, 40));
        }

        // Dispose of the image files.
        delete bmp1;
        if (bmp2 != nullptr)
        {
            delete bmp2;
        }
    }
private void ConstructFromResourceSaveAsGif(PaintEventArgs e)
{

    // Construct a bitmap from the button image resource.
    Bitmap bmp1 = new Bitmap(typeof(Button), "Button.bmp");

    // Save the image as a GIF.
    bmp1.Save("c:\\button.gif", System.Drawing.Imaging.ImageFormat.Gif);

    // Construct a new image from the GIF file.
    Bitmap bmp2 = new Bitmap("c:\\button.gif");

    // Draw the two images.
    e.Graphics.DrawImage(bmp1, new Point(10, 10));
    e.Graphics.DrawImage(bmp2, new Point(10, 40));

    // Dispose of the image files.
    bmp1.Dispose();
    bmp2.Dispose();
}
Private Sub ConstructFromResourceSaveAsGif(ByVal e As PaintEventArgs)

    ' Construct a bitmap from the button image resource.
    Dim bmp1 As New Bitmap(GetType(Button), "Button.bmp")

    ' Save the image as a GIF.
    bmp1.Save("c:\button.gif", System.Drawing.Imaging.ImageFormat.Gif)

    ' Construct a new image from the GIF file.
    Dim bmp2 As New Bitmap("c:\button.gif")

    ' Draw the two images.
    e.Graphics.DrawImage(bmp1, New Point(10, 10))
    e.Graphics.DrawImage(bmp2, New Point(10, 40))

    ' Dispose of the image files.
    bmp1.Dispose()
    bmp2.Dispose()
End Sub

備註

此函式會將指定類型的命名空間與資源的字串名稱結合,並在組件資訊清單中尋找相符的。This constructor combines the namespace of the given type with the string name of the resource and looks for a match in the assembly manifest. 例如,您可以傳入 Button 類型,並 Button.bmp 傳遞至此函式,它會尋找名為 System.Windows.Forms.Button.bmp 的資源。For example you can pass in the Button type and Button.bmp to this constructor and it will look for a resource named System.Windows.Forms.Button.bmp.

另請參閱

Bitmap(Image, Int32, Int32)

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

public:
 Bitmap(System::Drawing::Image ^ original, int width, int height);
public Bitmap (System.Drawing.Image original, int width, int height);
new System.Drawing.Bitmap : System.Drawing.Image * int * int -> System.Drawing.Bitmap
Public Sub New (original As Image, width As Integer, height As Integer)

參數

original
Image

Image,用於建立新的 BitmapThe Image from which to create the new Bitmap.

width
Int32

Bitmap 的寬度 (以像素為單位)。The width, in pixels, of the new Bitmap.

height
Int32

Bitmap 的高度 (以像素為單位)。The height, in pixels, of the new Bitmap.

例外狀況

作業失敗。The operation failed.

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.

public:
 Bitmap(int width, int height, System::Drawing::Graphics ^ g);
public Bitmap (int width, int height, System.Drawing.Graphics g);
new System.Drawing.Bitmap : int * int * System.Drawing.Graphics -> System.Drawing.Bitmap
Public Sub New (width As Integer, height As Integer, g As Graphics)

參數

width
Int32

Bitmap 的寬度 (以像素為單位)。The width, in pixels, of the new Bitmap.

height
Int32

Bitmap 的高度 (以像素為單位)。The height, in pixels, of the new Bitmap.

g
Graphics

設定新 Graphics 解析度的 Bitmap 物件。The Graphics object that specifies the resolution for the new Bitmap.

例外狀況

gnullg is null.

備註

這個方法所建立的新 Bitmap 會分別從 gDpiX 和 @no__t 2 屬性取得其水準和垂直解析度。The new Bitmap that this method creates takes its horizontal and vertical resolution from the DpiX and DpiY properties of g, respectively.

Bitmap(Int32, Int32, PixelFormat)

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

public:
 Bitmap(int width, int height, System::Drawing::Imaging::PixelFormat format);
public Bitmap (int width, int height, System.Drawing.Imaging.PixelFormat format);
new System.Drawing.Bitmap : int * int * System.Drawing.Imaging.PixelFormat -> System.Drawing.Bitmap
Public Sub New (width As Integer, height As Integer, format As PixelFormat)

參數

width
Int32

Bitmap 的寬度 (以像素為單位)。The width, in pixels, of the new Bitmap.

height
Int32

Bitmap 的高度 (以像素為單位)。The height, in pixels, of the new Bitmap.

format
PixelFormat

Bitmap 的像素格式。The pixel format for the new Bitmap. 這必須指定以「格式」 開頭的值。This must specify a value that begins with Format.

例外狀況

指定了 PixelFormat 值,其名稱不是以「格式」開頭。A PixelFormat value is specified whose name does not start with Format. 例如,指定 Gdi 會導致 ArgumentException,但 Format48bppRgb 不會。For example, specifying Gdi will cause an ArgumentException, but Format48bppRgb will not.

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

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

public:
 Bitmap(int width, int height, int stride, System::Drawing::Imaging::PixelFormat format, IntPtr scan0);
public Bitmap (int width, int height, int stride, System.Drawing.Imaging.PixelFormat format, IntPtr scan0);
new System.Drawing.Bitmap : int * int * int * System.Drawing.Imaging.PixelFormat * nativeint -> System.Drawing.Bitmap
Public Sub New (width As Integer, height As Integer, stride As Integer, format As PixelFormat, scan0 As IntPtr)

參數

width
Int32

Bitmap 的寬度 (以像素為單位)。The width, in pixels, of the new Bitmap.

height
Int32

Bitmap 的高度 (以像素為單位)。The height, in pixels, of the new Bitmap.

stride
Int32

整數,指定一條掃描線 (Scan Line) 的開頭和下一條之間的位元組位移 (Offset)。Integer that specifies the byte offset between the beginning of one scan line and the next. 這通常 (但不一定) 是像素格式的位元組數目 (例如,16 位元/像素為 2) 乘以點陣圖寬度。This is usually (but not necessarily) the number of bytes in the pixel format (for example, 2 for 16 bits per pixel) multiplied by the width of the bitmap. 傳遞給此參數之值必須為四的倍數。The value passed to this parameter must be a multiple of four.

format
PixelFormat

Bitmap 的像素格式。The pixel format for the new Bitmap. 這必須指定以「格式」 開頭的值。This must specify a value that begins with Format.

scan0
IntPtr

包含像素資料之位元組陣列的指標。Pointer to an array of bytes that contains the pixel data.

例外狀況

指定了 PixelFormat 值,其名稱不是以「格式」開頭。A PixelFormat value is specified whose name does not start with Format. 例如,指定 Gdi 會導致 ArgumentException,但 Format48bppRgb 不會。For example, specifying Gdi will cause an ArgumentException, but Format48bppRgb will not.

範例

下列程式碼範例示範如何使用 Bitmap(Int32, Int32, Int32, PixelFormat, IntPtr) 的函數。The following code example shows how to use the Bitmap(Int32, Int32, Int32, PixelFormat, IntPtr) constructor. 這個範例是設計來搭配 Windows Forms 使用,而且需要 PaintEventArgs 參數,這是 Paint 事件的參數。This example is designed to be used with Windows Forms and requires a PaintEventArgs parameter, which is a parameter of the Paint event.

private void BitmapConstructorEx(PaintEventArgs e)
{

    // Create a bitmap.
    Bitmap bmp = new Bitmap("c:\\fakePhoto.jpg");
    
   // Retrieve the bitmap data from the bitmap.
    System.Drawing.Imaging.BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), 
        ImageLockMode.ReadOnly, bmp.PixelFormat);

    //Create a new bitmap.
    Bitmap newBitmap = new Bitmap(200, 200, bmpData.Stride, bmp.PixelFormat, bmpData.Scan0);

    bmp.UnlockBits(bmpData);

    // Draw the new bitmap.
    e.Graphics.DrawImage(newBitmap, 10, 10);

}
Private Sub BitmapConstructorEx(ByVal e As PaintEventArgs)

    ' Create a bitmap.
    Dim bmp As New Bitmap("c:\fakePhoto.jpg")

    ' Retrieve the bitmap data from the bitmap.
    Dim bmpData As System.Drawing.Imaging.BitmapData = bmp.LockBits(New Rectangle(0, 0, bmp.Width, bmp.Height), _
        ImageLockMode.ReadOnly, bmp.PixelFormat)

    'Create a new bitmap.
    Dim newBitmap As New Bitmap(200, 200, bmpData.Stride, bmp.PixelFormat, bmpData.Scan0)

    bmp.UnlockBits(bmpData)

    ' Draw the new bitmap.
    e.Graphics.DrawImage(newBitmap, 10, 10)

End Sub

備註

呼叫端負責配置和釋放 scan0 參數所指定的記憶體區塊。The caller is responsible for allocating and freeing the block of memory specified by the scan0 parameter. 不過,在釋放相關的 Bitmap 之前,不應釋放記憶體。However, the memory should not be released until the related Bitmap is released.

安全性

SecurityPermission
用於呼叫非受控碼。for calling into unmanaged code. 相關列舉: UnmanagedCodeRelated enumeration: UnmanagedCode

適用於