Bitmap 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
封装 GDI+ 位图,此位图由图形图像及其属性的像素数据组成。 Bitmap 是用于处理由像素数据定义的图像的对象。
public ref class Bitmap sealed : System::Drawing::Imagepublic sealed class Bitmap : System.Drawing.Image[System.Serializable]
public sealed class Bitmap : System.Drawing.Image[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class Bitmap : System.Drawing.Imagetype Bitmap = class
    inherit Image[<System.Serializable>]
type Bitmap = class
    inherit Image[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type Bitmap = class
    inherit ImagePublic NotInheritable Class Bitmap
Inherits Image- 继承
- 属性
示例
下面的代码示例演示如何使用 GetPixel 和 SetPixel 方法来重新着色图像,从 文件中构造新的 Bitmap 。 它还使用 PixelFormat、 Width和 Height 属性。
此示例旨在与分别包含 Label、PictureBox、 和 Button 、PictureBox1Label1、 和 Button1的 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
注解
位图由图形图像的像素数据及其属性组成。 有许多标准格式可用于将位图保存到文件中。 GDI+ 支持以下文件格式:BMP、GIF、EXIF、JPG、PNG 和 TIFF。 有关支持的格式的详细信息,请参阅 位图的类型。
可以使用其中一个 Bitmap 构造函数从文件、流和其他源创建图像,并使用 方法将它们保存到流或文件系统 Save 。 使用 DrawImage 对象的 方法 Graphics 将图像绘制到屏幕或内存中。 有关使用图像文件的主题列表,请参阅 使用图像、位图、图标和图元文件。
注意
在 .NET 6 及更高版本中, System.Drawing.Common 包(包括此类型)仅在 Windows 操作系统上受支持。 在跨平台应用中使用此类型会导致编译时警告和运行时异常。 有关详细信息,请参阅 System.Drawing.Common 仅在 Windows 上受支持。
注意
无法 Bitmap 跨应用程序域访问 类。 例如,如果创建动态AppDomain并在该域中创建多个画笔、笔和位图,然后将这些对象传递回main应用程序域,则可以成功使用笔和画笔。 但是,如果调用 DrawImage 方法来绘制封 Bitmap送的 ,则会收到以下异常:
远程处理在类型“System.Drawing.Image”上找不到字段“本机图像”。
构造函数
| Bitmap(Image) | 从指定的现有图像初始化 Bitmap 类的新实例。 | 
| Bitmap(Image, Int32, Int32) | 从指定的现有图像(缩放到指定大小)初始化 Bitmap 类的新实例。 | 
| Bitmap(Image, Size) | 从指定的现有图像(缩放到指定大小)初始化 Bitmap 类的新实例。 | 
| Bitmap(Int32, Int32) | 用指定的大小初始化 Bitmap 类的新实例。 | 
| Bitmap(Int32, Int32, Graphics) | |
| Bitmap(Int32, Int32, Int32, PixelFormat, IntPtr) | 用指定的大小、像素格式和像素数据初始化 Bitmap 类的新实例。 | 
| Bitmap(Int32, Int32, PixelFormat) | 用指定的大小和格式初始化 Bitmap 类的新实例。 | 
| Bitmap(Stream) | 从指定的数据流初始化 Bitmap 类的新实例。 | 
| Bitmap(Stream, Boolean) | 从指定的数据流初始化 Bitmap 类的新实例。 | 
| Bitmap(String) | 从指定的文件初始化 Bitmap 类的新实例。 | 
| Bitmap(String, Boolean) | 从指定的文件初始化 Bitmap 类的新实例。 | 
| Bitmap(Type, String) | 从指定的资源初始化 Bitmap 类的新实例。 | 
属性
| Flags | 获取该 Image 的像素数据的特性标志。(继承自 Image) | 
| FrameDimensionsList | 获取 GUID 的数组,这些 GUID 表示此 Image 中帧的维数。(继承自 Image) | 
| Height | 获取此 Image 的高度(以像素为单位)。(继承自 Image) | 
| HorizontalResolution | 获取此 Image 的水平分辨率(以“像素/英寸”为单位)。(继承自 Image) | 
| Palette | 获取或设置用于此 Image 的调色板。(继承自 Image) | 
| PhysicalDimension | 获取此图像的宽度和高度。(继承自 Image) | 
| PixelFormat | 获取此 Image 的像素格式。(继承自 Image) | 
| PropertyIdList | 获取存储于该 Image 中的属性项的 ID。(继承自 Image) | 
| PropertyItems | 获取存储于该 Image 中的所有属性项(元数据片)。(继承自 Image) | 
| RawFormat | 获取此 Image 的文件格式。(继承自 Image) | 
| Size | 获取此图像的宽度和高度(以像素为单位)。(继承自 Image) | 
| Tag | 获取或设置提供有关图像的附加数据的对象。(继承自 Image) | 
| VerticalResolution | 获取此 Image 的垂直分辨率(以“像素/英寸”为单位)。(继承自 Image) | 
| Width | 获取此 Image 的宽度(以像素为单位)。(继承自 Image) | 
方法
显式接口实现
| ISerializable.GetObjectData(SerializationInfo, StreamingContext) | 使用将目标对象序列化所需的数据填充 SerializationInfo。(继承自 Image) |