Bitmap Bitmap Bitmap Bitmap Class

定义

封装 GDI+ 位图,此位图由图形图像及其属性的像素数据组成。Encapsulates a GDI+ bitmap, which consists of the pixel data for a graphics image and its attributes. Bitmap 是用于处理由像素数据定义的图像的对象。A Bitmap is an object used to work with images defined by pixel data.

public ref class Bitmap sealed : System::Drawing::Image
[System.Runtime.InteropServices.ComVisible(true)]
[System.Serializable]
public sealed class Bitmap : System.Drawing.Image
type Bitmap = class
    inherit Image
Public NotInheritable Class Bitmap
Inherits Image
继承
属性

示例

下面的代码示例演示如何使用 GetPixel 和 @no__t 2 方法对图像重新着色,从而构造文件中的新 BitmapThe following code example demonstrates how to construct a new Bitmap from a file, using the GetPixel and SetPixel methods to recolor the image. 它还使用 PixelFormatWidthHeight 属性。It also uses the PixelFormat, Width, and Height properties.

此示例设计为与包含 @no__t 为 @no__t、@no__t 和 @no__t 2 的 Windows 窗体一起使用,分别为 @no__t、@no__t、和。This example is designed to be used with a Windows Forms that contains a Label, PictureBox, and Button named Label1, PictureBox1, and Button1, respectively. 将代码粘贴到窗体中,并将 Button1_Click 方法与按钮的 @no__t 1 事件关联。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

注解

位图由图形图像及其属性的像素数据组成。A bitmap consists of the pixel data for a graphics image and its attributes. 有许多标准格式可用于将位图保存到文件。There are many standard formats for saving a bitmap to a file. GDI+GDI+ 支持以下文件格式:BMP、GIF、EXIF、JPG、PNG 和 TIFF。supports the following file formats: BMP, GIF, EXIF, JPG, PNG and TIFF. 有关支持的格式的详细信息,请参阅位图类型For more information about supported formats, see Types of Bitmaps.

您可以使用 @no__t 0 构造函数之一从文件、流和其他源创建图像,然后使用 Save 方法将它们保存到流或文件系统。You can create images from files, streams, and other sources by using one of the Bitmap constructors and save them to a stream or to the file system with the Save method. 使用 @no__t 对象的 @no__t 的方法,将图像绘制到屏幕或内存。Images are drawn to the screen or to memory by using the DrawImage method of the Graphics object. 有关使用图像文件的主题的列表,请参阅使用图像、位图、图标和图元文件。For a list of topics about working with image files, see Working with Images, Bitmaps, Icons, and Metafiles.

备注

不能跨应用程序域访问 Bitmap 类。The Bitmap class is not accessible across application domains. 例如,如果创建一个动态 @no__t 0,并在该域中创建多个画笔、笔和位图,然后将这些对象传递回主应用程序域,则可以成功使用笔和画笔。For example, if you create a dynamic AppDomain and create several brushes, pens, and bitmaps in that domain, then pass these objects back to the main application domain, you can successfully use the pens and brushes. 但是,如果调用 @no__t 0 方法来绘制封送 Bitmap,则会收到以下异常。However, if you call the DrawImage method to draw the marshaled Bitmap, you receive the following exception.

远程处理无法在类型 "System.web" 上找到字段 "本机映像"。Remoting cannot find field "native image" on type "System.Drawing.Image".

构造函数

Bitmap(Image) Bitmap(Image) Bitmap(Image) Bitmap(Image)

从指定的现有图像初始化 Bitmap 类的新实例。Initializes a new instance of the Bitmap class from the specified existing image.

Bitmap(Image, Int32, Int32) Bitmap(Image, Int32, Int32) Bitmap(Image, Int32, Int32) Bitmap(Image, Int32, Int32)

从指定的现有图像(缩放到指定大小)初始化 Bitmap 类的新实例。Initializes a new instance of the Bitmap class from the specified existing image, scaled to the specified size.

Bitmap(Image, Size) Bitmap(Image, Size) Bitmap(Image, Size) 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(Int32, Int32) Bitmap(Int32, Int32) Bitmap(Int32, Int32)

用指定的大小初始化 Bitmap 类的新实例。Initializes a new instance of the Bitmap class with the specified size.

Bitmap(Int32, Int32, Graphics) Bitmap(Int32, Int32, Graphics) Bitmap(Int32, Int32, Graphics) Bitmap(Int32, Int32, Graphics)

用指定的大小和指定的 Graphics 对象的分辨率初始化 Bitmap 类的新实例。Initializes a new instance of the Bitmap class with the specified size and with the resolution of the specified Graphics object.

Bitmap(Int32, Int32, Int32, PixelFormat, IntPtr) Bitmap(Int32, Int32, Int32, PixelFormat, IntPtr) Bitmap(Int32, Int32, Int32, PixelFormat, IntPtr) 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(Int32, Int32, PixelFormat) Bitmap(Int32, Int32, PixelFormat) Bitmap(Int32, Int32, PixelFormat) Bitmap(Int32, Int32, PixelFormat)

用指定的大小和格式初始化 Bitmap 类的新实例。Initializes a new instance of the Bitmap class with the specified size and format.

Bitmap(Stream) Bitmap(Stream) Bitmap(Stream) Bitmap(Stream)

从指定的数据流初始化 Bitmap 类的新实例。Initializes a new instance of the Bitmap class from the specified data stream.

Bitmap(Stream, Boolean) Bitmap(Stream, Boolean) Bitmap(Stream, Boolean) Bitmap(Stream, Boolean)

从指定的数据流初始化 Bitmap 类的新实例。Initializes a new instance of the Bitmap class from the specified data stream.

Bitmap(String) Bitmap(String) Bitmap(String) Bitmap(String)

从指定的文件初始化 Bitmap 类的新实例。Initializes a new instance of the Bitmap class from the specified file.

Bitmap(String, Boolean) Bitmap(String, Boolean) Bitmap(String, Boolean) Bitmap(String, Boolean)

从指定的文件初始化 Bitmap 类的新实例。Initializes a new instance of the Bitmap class from the specified file.

Bitmap(Type, String) Bitmap(Type, String) Bitmap(Type, String) Bitmap(Type, String)

从指定的资源初始化 Bitmap 类的新实例。Initializes a new instance of the Bitmap class from a specified resource.

方法

Clone() Clone() Clone() Clone()

创建此 Image 的一个精确副本。Creates an exact copy of this Image.

(Inherited from Image)
Clone(Rectangle, PixelFormat) Clone(Rectangle, PixelFormat) Clone(Rectangle, PixelFormat) Clone(Rectangle, PixelFormat)

创建此 Bitmap(它由 Rectangle 结构并使用指定的 PixelFormat 枚举定义)部分的副本。Creates a copy of the section of this Bitmap defined by Rectangle structure and with a specified PixelFormat enumeration.

Clone(RectangleF, PixelFormat) Clone(RectangleF, PixelFormat) Clone(RectangleF, PixelFormat) Clone(RectangleF, PixelFormat)

创建此 Bitmap(用指定的 PixelFormat 枚举定义)部分的副本。Creates a copy of the section of this Bitmap defined with a specified PixelFormat enumeration.

CreateObjRef(Type) CreateObjRef(Type) CreateObjRef(Type) CreateObjRef(Type)

创建一个对象,该对象包含生成用于与远程对象进行通信的代理所需的全部相关信息。Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.

(Inherited from MarshalByRefObject)
Dispose() Dispose() Dispose() Dispose()

释放此 Image 使用的所有资源。Releases all resources used by this Image.

(Inherited from Image)
Dispose(Boolean) Dispose(Boolean) Dispose(Boolean) Dispose(Boolean)

释放由 Image 占用的非托管资源,还可以另外再释放托管资源。Releases the unmanaged resources used by the Image and optionally releases the managed resources.

(Inherited from Image)
Equals(Object) Equals(Object) Equals(Object) Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(Inherited from Object)
FromHicon(IntPtr) FromHicon(IntPtr) FromHicon(IntPtr) FromHicon(IntPtr)

从图标的 Windows 图柄创建 BitmapCreates a Bitmap from a Windows handle to an icon.

FromResource(IntPtr, String) FromResource(IntPtr, String) FromResource(IntPtr, String) FromResource(IntPtr, String)

从指定的 Windows 资源创建 BitmapCreates a Bitmap from the specified Windows resource.

GetBounds(GraphicsUnit) GetBounds(GraphicsUnit) GetBounds(GraphicsUnit) GetBounds(GraphicsUnit)

以指定的单位获取图像的界限。Gets the bounds of the image in the specified unit.

(Inherited from Image)
GetEncoderParameterList(Guid) GetEncoderParameterList(Guid) GetEncoderParameterList(Guid) GetEncoderParameterList(Guid)

返回有关指定的图像编码器所支持的参数的信息。Returns information about the parameters supported by the specified image encoder.

(Inherited from Image)
GetFrameCount(FrameDimension) GetFrameCount(FrameDimension) GetFrameCount(FrameDimension) GetFrameCount(FrameDimension)

返回指定维度的帧数。Returns the number of frames of the specified dimension.

(Inherited from Image)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(Inherited from Object)
GetHbitmap() GetHbitmap() GetHbitmap() GetHbitmap()

从此 Bitmap 创建 GDIGDI 位图对象。Creates a GDIGDI bitmap object from this Bitmap.

GetHbitmap(Color) GetHbitmap(Color) GetHbitmap(Color) GetHbitmap(Color)

从此 Bitmap 创建 GDIGDI 位图对象。Creates a GDIGDI bitmap object from this Bitmap.

GetHicon() GetHicon() GetHicon() GetHicon()

返回图标的图柄。Returns the handle to an icon.

GetLifetimeService() GetLifetimeService() GetLifetimeService() GetLifetimeService()

检索控制此实例的生存期策略的当前生存期服务对象。Retrieves the current lifetime service object that controls the lifetime policy for this instance.

(Inherited from MarshalByRefObject)
GetPixel(Int32, Int32) GetPixel(Int32, Int32) GetPixel(Int32, Int32) GetPixel(Int32, Int32)

获取此 Bitmap 中指定像素的颜色。Gets the color of the specified pixel in this Bitmap.

GetPropertyItem(Int32) GetPropertyItem(Int32) GetPropertyItem(Int32) GetPropertyItem(Int32)

从该 Image 获取指定的属性项。Gets the specified property item from this Image.

(Inherited from Image)
GetThumbnailImage(Int32, Int32, Image+GetThumbnailImageAbort, IntPtr) GetThumbnailImage(Int32, Int32, Image+GetThumbnailImageAbort, IntPtr) GetThumbnailImage(Int32, Int32, Image+GetThumbnailImageAbort, IntPtr) GetThumbnailImage(Int32, Int32, Image+GetThumbnailImageAbort, IntPtr)

返回此 Image 的缩略图。Returns a thumbnail for this Image.

(Inherited from Image)
GetType() GetType() GetType() GetType()

获取当前实例的 TypeGets the Type of the current instance.

(Inherited from Object)
InitializeLifetimeService() InitializeLifetimeService() InitializeLifetimeService() InitializeLifetimeService()

获取生存期服务对象来控制此实例的生存期策略。Obtains a lifetime service object to control the lifetime policy for this instance.

(Inherited from MarshalByRefObject)
LockBits(Rectangle, ImageLockMode, PixelFormat) LockBits(Rectangle, ImageLockMode, PixelFormat) LockBits(Rectangle, ImageLockMode, PixelFormat) LockBits(Rectangle, ImageLockMode, PixelFormat)

Bitmap 锁定到系统内存中。Locks a Bitmap into system memory.

LockBits(Rectangle, ImageLockMode, PixelFormat, BitmapData) LockBits(Rectangle, ImageLockMode, PixelFormat, BitmapData) LockBits(Rectangle, ImageLockMode, PixelFormat, BitmapData) LockBits(Rectangle, ImageLockMode, PixelFormat, BitmapData)

Bitmap 锁定到系统内存中。Locks a Bitmap into system memory.

MakeTransparent() MakeTransparent() MakeTransparent() MakeTransparent()

Bitmap 的默认透明颜色为透明。Makes the default transparent color transparent for this Bitmap.

MakeTransparent(Color) MakeTransparent(Color) MakeTransparent(Color) MakeTransparent(Color)

使指定的颜色对此 Bitmap 透明。Makes the specified color transparent for this Bitmap.

MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(Inherited from Object)
MemberwiseClone(Boolean) MemberwiseClone(Boolean) MemberwiseClone(Boolean) MemberwiseClone(Boolean)

创建当前 MarshalByRefObject 对象的浅表副本。Creates a shallow copy of the current MarshalByRefObject object.

(Inherited from MarshalByRefObject)
RemovePropertyItem(Int32) RemovePropertyItem(Int32) RemovePropertyItem(Int32) RemovePropertyItem(Int32)

从该 Image 移除指定的属性项。Removes the specified property item from this Image.

(Inherited from Image)
RotateFlip(RotateFlipType) RotateFlip(RotateFlipType) RotateFlip(RotateFlipType) RotateFlip(RotateFlipType)

旋转、翻转或者同时旋转和翻转 ImageRotates, flips, or rotates and flips the Image.

(Inherited from Image)
Save(Stream, ImageCodecInfo, EncoderParameters) Save(Stream, ImageCodecInfo, EncoderParameters) Save(Stream, ImageCodecInfo, EncoderParameters) Save(Stream, ImageCodecInfo, EncoderParameters)

使用指定的编码器和图像编码器参数,将该图像保存到指定的流。Saves this image to the specified stream, with the specified encoder and image encoder parameters.

(Inherited from Image)
Save(Stream, ImageFormat) Save(Stream, ImageFormat) Save(Stream, ImageFormat) Save(Stream, ImageFormat)

将此图像以指定的格式保存到指定的流中。Saves this image to the specified stream in the specified format.

(Inherited from Image)
Save(String) Save(String) Save(String) Save(String)

将该 Image 保存到指定的文件或流。Saves this Image to the specified file or stream.

(Inherited from Image)
Save(String, ImageCodecInfo, EncoderParameters) Save(String, ImageCodecInfo, EncoderParameters) Save(String, ImageCodecInfo, EncoderParameters) Save(String, ImageCodecInfo, EncoderParameters)

使用指定的编码器和图像编码器参数,将该 Image 保存到指定的文件。Saves this Image to the specified file, with the specified encoder and image-encoder parameters.

(Inherited from Image)
Save(String, ImageFormat) Save(String, ImageFormat) Save(String, ImageFormat) Save(String, ImageFormat)

将此 Image 以指定格式保存到指定文件。Saves this Image to the specified file in the specified format.

(Inherited from Image)
SaveAdd(EncoderParameters) SaveAdd(EncoderParameters) SaveAdd(EncoderParameters) SaveAdd(EncoderParameters)

将帧添加到上次对 Save 方法调用时所指定的文件或流中。Adds a frame to the file or stream specified in a previous call to the Save method. 使用此方法将多帧图像中的选定帧保存到另一个多帧图像。Use this method to save selected frames from a multiple-frame image to another multiple-frame image.

(Inherited from Image)
SaveAdd(Image, EncoderParameters) SaveAdd(Image, EncoderParameters) SaveAdd(Image, EncoderParameters) SaveAdd(Image, EncoderParameters)

将帧添加到上次对 Save 方法调用时所指定的文件或流中。Adds a frame to the file or stream specified in a previous call to the Save method.

(Inherited from Image)
SelectActiveFrame(FrameDimension, Int32) SelectActiveFrame(FrameDimension, Int32) SelectActiveFrame(FrameDimension, Int32) SelectActiveFrame(FrameDimension, Int32)

选择由维度和索引指定的帧。Selects the frame specified by the dimension and index.

(Inherited from Image)
SetPixel(Int32, Int32, Color) SetPixel(Int32, Int32, Color) SetPixel(Int32, Int32, Color) SetPixel(Int32, Int32, Color)

在此 Bitmap 中设置指定像素的颜色。Sets the color of the specified pixel in this Bitmap.

SetPropertyItem(PropertyItem) SetPropertyItem(PropertyItem) SetPropertyItem(PropertyItem) SetPropertyItem(PropertyItem)

在此 Image 中存储一个属性项(元数据片)。Stores a property item (piece of metadata) in this Image.

(Inherited from Image)
SetResolution(Single, Single) SetResolution(Single, Single) SetResolution(Single, Single) SetResolution(Single, Single)

设置此 Bitmap 的分辨率。Sets the resolution for this Bitmap.

ToString() ToString() ToString() ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(Inherited from Object)
UnlockBits(BitmapData) UnlockBits(BitmapData) UnlockBits(BitmapData) UnlockBits(BitmapData)

从系统内存解锁此 BitmapUnlocks this Bitmap from system memory.

属性

Flags Flags Flags Flags

获取该 Image 的像素数据的特性标志。Gets attribute flags for the pixel data of this Image.

(Inherited from Image)
FrameDimensionsList FrameDimensionsList FrameDimensionsList FrameDimensionsList

获取 GUID 的数组,这些 GUID 表示此 Image 中帧的维数。Gets an array of GUIDs that represent the dimensions of frames within this Image.

(Inherited from Image)
Height Height Height Height

获取此 Image 的高度(以像素为单位)。Gets the height, in pixels, of this Image.

(Inherited from Image)
HorizontalResolution HorizontalResolution HorizontalResolution HorizontalResolution

获取此 Image 的水平分辨率(以“像素/英寸”为单位)。Gets the horizontal resolution, in pixels per inch, of this Image.

(Inherited from Image)
Palette Palette Palette Palette

获取或设置用于此 Image 的调色板。Gets or sets the color palette used for this Image.

(Inherited from Image)
PhysicalDimension PhysicalDimension PhysicalDimension PhysicalDimension

获取此图像的宽度和高度。Gets the width and height of this image.

(Inherited from Image)
PixelFormat PixelFormat PixelFormat PixelFormat

获取此 Image 的像素格式。Gets the pixel format for this Image.

(Inherited from Image)
PropertyIdList PropertyIdList PropertyIdList PropertyIdList

获取存储于该 Image 中的属性项的 ID。Gets IDs of the property items stored in this Image.

(Inherited from Image)
PropertyItems PropertyItems PropertyItems PropertyItems

获取存储于该 Image 中的所有属性项(元数据片)。Gets all the property items (pieces of metadata) stored in this Image.

(Inherited from Image)
RawFormat RawFormat RawFormat RawFormat

获取此 Image 的文件格式。Gets the file format of this Image.

(Inherited from Image)
Size Size Size Size

获取此图像的宽度和高度(以像素为单位)。Gets the width and height, in pixels, of this image.

(Inherited from Image)
Tag Tag Tag Tag

获取或设置提供有关图像的附加数据的对象。Gets or sets an object that provides additional data about the image.

(Inherited from Image)
VerticalResolution VerticalResolution VerticalResolution VerticalResolution

获取此 Image 的垂直分辨率(以“像素/英寸”为单位)。Gets the vertical resolution, in pixels per inch, of this Image.

(Inherited from Image)
Width Width Width Width

获取此 Image 的宽度(以像素为单位)。Gets the width, in pixels, of this Image.

(Inherited from Image)

显式界面实现

ISerializable.GetObjectData(SerializationInfo, StreamingContext) ISerializable.GetObjectData(SerializationInfo, StreamingContext) ISerializable.GetObjectData(SerializationInfo, StreamingContext) ISerializable.GetObjectData(SerializationInfo, StreamingContext)

使用将目标对象序列化所需的数据填充 SerializationInfoPopulates a SerializationInfo with the data needed to serialize the target object.

(Inherited from Image)

适用于

另请参阅