Bitmap 建構函式

定義

初始化 Bitmap 類別的新執行個體。

多載

Bitmap(Image)

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

Bitmap(Stream)

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

Bitmap(String)

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

Bitmap(Image, Size)

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

Bitmap(Int32, Int32)

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

Bitmap(Stream, Boolean)

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

Bitmap(String, Boolean)

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

Bitmap(Type, String)

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

Bitmap(Image, Int32, Int32)

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

Bitmap(Int32, Int32, Graphics)

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

Bitmap(Int32, Int32, PixelFormat)

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

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

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

Bitmap(Image)

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

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

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,用於建立新的 Bitmap

適用於

Bitmap(Stream)

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

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

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

參數

stream
Stream

用來載入影像的資料流。

例外狀況

stream 未含有影像資料或為 null

-或-

stream 含有一個 PNG 影像檔案,其中有一個維度超過 65,535 個像素。

範例

下列程式代碼範例示範如何從數據流載入點陣圖。

此範例的設計目的是要與 Windows Forms 搭配使用。 Create 包含具名 PictureBox1PictureBox表單。 將程式代碼貼到表單中,並從表單的建構函式或Load事件處理方法呼叫 InitializeStreamBitmap 方法。

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存留期內保持開啟。

由於 GDI+ 譯碼器的限制, System.ArgumentException 如果您 .png 從單一維度大於 65,535 像素的單一維度建構位圖,則會擲回 。

另請參閱

適用於

Bitmap(String)

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

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

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

點陣圖檔名稱和路徑。

例外狀況

找不到指定的檔案。

備註

檔名和路徑可以相對於應用程式或絕對路徑。 使用此建構函式以下列檔格式開啟影像:BMP、GIF、EXIF、JPG、PNG 和 TIFF。 如需支援格式的詳細資訊,請參閱 位圖的類型。 檔案會保持鎖定狀態, Bitmap 直到處置 為止。

另請參閱

適用於

Bitmap(Image, Size)

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

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

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,用於建立新的 Bitmap

newSize
Size

Size 結構,表示新 Bitmap 的大小。

例外狀況

作業失敗。

適用於

Bitmap(Int32, Int32)

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

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

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 的寬度 (以像素為單位)。

height
Int32

Bitmap 的高度 (以像素為單位)。

例外狀況

作業失敗。

備註

這個建構函式會 Bitmap 建立 具有 PixelFormat 列舉值的 Format32bppArgb

適用於

Bitmap(Stream, Boolean)

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

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

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
Public Sub New (stream As Stream, useIcm As Boolean)

參數

stream
Stream

用來載入影像的資料流。

useIcm
Boolean

true 表示要對此 Bitmap 進行色彩修正,否則為 false

例外狀況

stream 未含有影像資料或為 null

-或-

stream 含有一個 PNG 影像檔案,其中有一個維度超過 65,535 個像素。

備註

您必須讓數據流在 的 Bitmap存留期內保持開啟。

由於 GDI+ 譯碼器的限制, System.ArgumentException 如果您 .png 從單一維度大於 65,535 像素的單一維度建構位圖,則會擲回 。

另請參閱

適用於

Bitmap(String, Boolean)

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

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

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

點陣圖檔的名稱。

useIcm
Boolean

true 表示要對此 Bitmap 進行色彩修正,否則為 false

範例

下列程式代碼範例示範如何從檔案建構新的點陣圖。 此範例會使用 GetPixelSetPixel 方法來重新著色影像。 它也會使用 PixelFormat 屬性。

此範例的設計目的是要分別與包含 LabelPictureBoxPictureBox1ButtonButton1Label1Windows Form 搭配使用。 將程式代碼貼到表單中,並將方法與按鈕的事件Click產生關聯Button1_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

備註

使用此建構函式以下列檔格式開啟影像:BMP、GIF、EXIF、JPG、PNG 和 TIFF。 如需支援格式的詳細資訊,請參閱 位圖的類型。 檔案會保持鎖定狀態, Bitmap 直到處置 為止。

另請參閱

適用於

Bitmap(Type, String)

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

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

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

參數

type
Type

用來擷取資源的類別。

resource
String

資源名稱。

範例

下列程式代碼範例示範如何從類型建構位圖,以及如何使用 Save 方法。 若要執行此範例,請將程式代碼貼到 Windows Form 中。 處理表單的事件 Paint 並呼叫 ConstructFromResourceSaveAsGif 方法,並傳遞 ePaintEventArgs

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

備註

這個建構函式會將指定型別的命名空間與資源的字串名稱結合,並在元件指令清單中尋找相符專案。 例如,您可以傳入 Button 型別和 Button.bmp 至這個建構函式,它會尋找名為 的資源 System.Windows.Forms.Button.bmp

另請參閱

適用於

Bitmap(Image, Int32, Int32)

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

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

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,用於建立新的 Bitmap

width
Int32

Bitmap 的寬度 (以像素為單位)。

height
Int32

Bitmap 的高度 (以像素為單位)。

例外狀況

作業失敗。

適用於

Bitmap(Int32, Int32, Graphics)

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

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

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 的寬度 (以像素為單位)。

height
Int32

Bitmap 的高度 (以像素為單位)。

g
Graphics

設定新 Graphics 解析度的 Bitmap 物件。

例外狀況

gnull

備註

這個方法所建立的新 Bitmap 會分別從 DpiX 的 和 DpiY 屬性 g取得其水準和垂直解析度。

適用於

Bitmap(Int32, Int32, PixelFormat)

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

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

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 的寬度 (以像素為單位)。

height
Int32

Bitmap 的高度 (以像素為單位)。

format
PixelFormat

Bitmap 的像素格式。 這必須指定以 開頭 Format的值。

例外狀況

指定了 PixelFormat 值,其名稱不是以「格式」開頭。 例如,指定 Gdi 會導致 ArgumentException,但 Format48bppRgb 不會。

適用於

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

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

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

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 的寬度 (以像素為單位)。

height
Int32

Bitmap 的高度 (以像素為單位)。

stride
Int32

整數,指定一條掃描線 (Scan Line) 的開頭和下一條之間的位元組位移 (Offset)。 這通常 (但不一定) 是像素格式的位元組數目 (例如,16 位元/像素為 2) 乘以點陣圖寬度。 傳遞給此參數之值必須為四的倍數。

format
PixelFormat

Bitmap 的像素格式。 這必須指定以 開頭 Format的值。

scan0
IntPtr

nativeint

包含像素資料之位元組陣列的指標。

例外狀況

指定了 PixelFormat 值,其名稱不是以「格式」開頭。 例如,指定 Gdi 會導致 ArgumentException,但 Format48bppRgb 不會。

範例

下列程式代碼範例示範如何使用 建 Bitmap(Int32, Int32, Int32, PixelFormat, IntPtr) 構函式。 此範例的設計目的是要與 Windows Forms 搭配使用,而且需要 PaintEventArgs 參數,這是 事件的參數Paint

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 指定的記憶體區塊。 不過,在釋放相關 Bitmap 之前,不應該釋放記憶體。

適用於