Bitmap 建構函式
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
初始化 Bitmap 類別的新執行個體。
多載
| 名稱 | Description |
|---|---|
| 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(Int32, Int32, PixelFormat) |
初始化一個以指定大小與格式的新類別實例 Bitmap 。 |
| Bitmap(Int32, Int32, Int32, PixelFormat, IntPtr) |
初始化一個以指定大小、像素格式及像素資料的新類別實例 Bitmap 。 |
Bitmap(Image)
- 來源:
- Bitmap.cs
- 來源:
- Bitmap.cs
- 來源:
- Bitmap.cs
- 來源:
- Bitmap.cs
- 來源:
- Bitmap.cs
- 來源:
- 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)
參數
適用於
Bitmap(Stream)
- 來源:
- Bitmap.cs
- 來源:
- Bitmap.cs
- 來源:
- Bitmap.cs
- 來源:
- Bitmap.cs
- 來源:
- Bitmap.cs
- 來源:
- 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
用於載入影像的資料流。
例外狀況
範例
以下程式碼範例示範如何從串流載入位圖。
此範例設計用於 Windows 表單。 建立一個包含 PictureBox 名稱 PictureBox1的表單。 將程式碼貼到表單中,並從表單的建構子或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+ 解碼器的限制,若從單維大於 65,535 像素的 .png 影像檔案構建點陣圖,會拋出 。System.ArgumentException
另請參閱
適用於
Bitmap(String)
- 來源:
- Bitmap.cs
- 來源:
- Bitmap.cs
- 來源:
- Bitmap.cs
- 來源:
- Bitmap.cs
- 來源:
- Bitmap.cs
- 來源:
- 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.cs
- 來源:
- Bitmap.cs
- 來源:
- 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)
參數
例外狀況
作業失敗。
適用於
Bitmap(Int32, Int32)
- 來源:
- Bitmap.cs
- 來源:
- Bitmap.cs
- 來源:
- Bitmap.cs
- 來源:
- Bitmap.cs
- 來源:
- Bitmap.cs
- 來源:
- 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)
參數
例外狀況
作業失敗。
備註
此構造子創造 Bitmap 一個枚 PixelFormat 舉值為 Format32bppArgb。
適用於
Bitmap(Stream, Boolean)
- 來源:
- Bitmap.cs
- 來源:
- Bitmap.cs
- 來源:
- Bitmap.cs
- 來源:
- Bitmap.cs
- 來源:
- Bitmap.cs
- 來源:
- 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
用於載入影像的資料流。
例外狀況
備註
你必須在 的整個壽命 Bitmap期間保持串流暢通。
由於 GDI+ 解碼器的限制,若從單維大於 65,535 像素的 .png 影像檔案構建點陣圖,會拋出 。System.ArgumentException
另請參閱
適用於
Bitmap(String, Boolean)
- 來源:
- Bitmap.cs
- 來源:
- Bitmap.cs
- 來源:
- Bitmap.cs
- 來源:
- Bitmap.cs
- 來源:
- Bitmap.cs
- 來源:
- 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
點陣圖檔案的名稱。
範例
以下程式碼範例示範如何從檔案建構新的位圖。 範例中使用 和 GetPixelSetPixel 方法來重新上色影像。 它也使用該 PixelFormat 物業。
此範例設計用於包含 Label、 、 PictureBoxLabel1Button 和 分別為 的 PictureBox1Button1Windows 表單。 將程式碼貼到表單中,並將方法與按鈕事件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.cs
- 來源:
- Bitmap.cs
- 來源:
- 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 表單中。 處理表單事件 Paint 並呼叫該 ConstructFromResourceSaveAsGif 方法,傳遞 e 為 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
備註
此建構子結合給定型態的命名空間與資源的字串名稱,並在組合語言清單中尋找匹配。 例如,你可以將 Button 型別 和 Button.bmp 傳遞給這個建構子,它會尋找一個名為 System.Windows.Forms.Button.bmp的資源。
另請參閱
適用於
Bitmap(Image, Int32, Int32)
- 來源:
- Bitmap.cs
- 來源:
- Bitmap.cs
- 來源:
- Bitmap.cs
- 來源:
- Bitmap.cs
- 來源:
- Bitmap.cs
- 來源:
- 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)
參數
例外狀況
作業失敗。
適用於
Bitmap(Int32, Int32, Graphics)
- 來源:
- Bitmap.cs
- 來源:
- Bitmap.cs
- 來源:
- Bitmap.cs
- 來源:
- Bitmap.cs
- 來源:
- Bitmap.cs
- 來源:
- Bitmap.cs
- 來源:
- Bitmap.cs
- 來源:
- Bitmap.cs
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)
參數
例外狀況
g 為 null。
備註
此方法所產生的新解析度 Bitmap 分別來自 DpiX 與 DpiYg的性質。
適用於
Bitmap(Int32, Int32, PixelFormat)
- 來源:
- Bitmap.cs
- 來源:
- Bitmap.cs
- 來源:
- Bitmap.cs
- 來源:
- Bitmap.cs
- 來源:
- Bitmap.cs
- 來源:
- 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)
參數
- format
- PixelFormat
新的 Bitmap. 這必須指定一個以 Format為開頭的值。
例外狀況
指定一個 PixelFormat 名稱不以 Format 開頭的值。 例如,指定 Gdi 會產生 ArgumentException,但 Format48bppRgb 不會。
適用於
Bitmap(Int32, Int32, Int32, PixelFormat, IntPtr)
- 來源:
- Bitmap.cs
- 來源:
- Bitmap.cs
- 來源:
- Bitmap.cs
- 來源:
- Bitmap.cs
- 來源:
- Bitmap.cs
- 來源:
- 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)
參數
- stride
- Int32
整數,指定一條掃描線起點與下一條掃描線起點之間的位元組偏移量。 這通常是(但不一定)像素格式中的位元組數(例如每像素 16 位元為 2 位元)乘以位圖寬度。 傳遞給此參數的值必須是四的倍數。
- format
- PixelFormat
新的 Bitmap. 這必須指定一個以 Format為開頭的值。
- scan0
-
IntPtr
nativeint
指標指向包含像素資料的位元組陣列。
例外狀況
指定一個 PixelFormat 名稱不以 Format 開頭的值。 例如,指定 Gdi 會產生 ArgumentException,但 Format48bppRgb 不會。
範例
以下程式碼範例展示了如何使用 Bitmap(Int32, Int32, Int32, PixelFormat, IntPtr) 建構子。 此範例設計用於 Windows 表單,並需要一個 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 ,不應釋放該記憶。