通过


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)

使用指定大小和指定Graphics对象的分辨率初始化类的新实例Bitmap

Bitmap(Int32, Int32, PixelFormat)

使用指定的大小和格式初始化类的新实例 Bitmap

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

使用指定的大小、像素格式和像素数据初始化类的新实例 Bitmap

Bitmap(Image)

Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
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)

Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
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 包含单个尺寸大于 65,535 像素的 PNG 图像文件。

示例

下面的代码示例演示如何从流加载位图。

此示例旨在与 Windows 窗体一起使用。 创建包含命名的PictureBoxPictureBox1窗体。 将代码粘贴到窗体中,并从窗体的构造函数或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)

Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
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)

Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
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

表示 SizeBitmap大小的结构。

例外

操作失败。

适用于

Bitmap(Int32, Int32)

Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
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的高度(以像素为单位)。

例外

操作失败。

注解

此构造函数创建一个BitmapPixelFormat枚举值为 Format32bppArgb.

适用于

Bitmap(Stream, Boolean)

Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
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 包含单个尺寸大于 65,535 像素的 PNG 图像文件。

注解

必须在流的生存期内保持打开状态 Bitmap

由于 GDI+ 解码器的限制, System.ArgumentException 如果从 .png 图像文件中构造位图,其维度大于 65,535 像素,则会引发位图。

另请参阅

适用于

Bitmap(String, Boolean)

Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
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 属性。

此示例设计为与包含一LabelPictureBox个和命名Label1PictureBox1以及ButtonButton1分别包含的 Windows 窗体一起使用。 将代码粘贴到窗体中,并将 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

注解

使用此构造函数可打开具有以下文件格式的图像:BMP、GIF、EXIF、JPG、PNG 和 TIFF。 有关支持的格式的详细信息,请参阅 位图的类型。 文件保持锁定状态, Bitmap 直到释放。

另请参阅

适用于

Bitmap(Type, String)

Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
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)

Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
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)

Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs

使用指定大小和指定Graphics对象的分辨率初始化类的新实例Bitmap

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

指定 GraphicsBitmap解析的对象。

例外

gnull

注解

此方法创建的新 Bitmap 方法分别采用 DpiX 其水平分辨率和 DpiY 垂直分辨率以及 g其属性。

适用于

Bitmap(Int32, Int32, PixelFormat)

Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
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项的像素格式。 这必须指定以 开头 的值。

例外

指定 PixelFormat 名称不以 Format 开头的值。 例如,指定 Gdi 将导致一个 ArgumentException,但不 Format48bppRgb 会导致。

适用于

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

Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
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

一个扫描行的开头和下一个扫描行的开头之间指定字节偏移量的整数。 这通常是(但不一定)像素格式(例如,每个像素 2(16 位)的字节数乘以位图的宽度。 传递给此参数的值必须是 4 的倍数。

format
PixelFormat

Bitmap项的像素格式。 这必须指定以 开头 的值。

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 内容之前,不应释放内存。

适用于