Bitmap Bitmap Bitmap Bitmap Class

Definition

Encapsulates a GDI+ bitmap, which consists of the pixel data for a graphics image and its attributes. 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)]
[Serializable]
public sealed class Bitmap : System.Drawing.Image
type Bitmap = class
    inherit Image
Public NotInheritable Class Bitmap
Inherits Image
Inheritance
Attributes
ComVisibleAttribute SerializableAttribute

Examples

The following code example demonstrates how to construct a new Bitmap from a file, using the GetPixel and SetPixel methods to recolor the image. It also uses the PixelFormat, Width, and Height properties.

This example is designed to be used with a Windows Forms that contains a Label, PictureBox, and Button named Label1, PictureBox1, and Button1, respectively. 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

Remarks

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+ supports the following file formats: BMP, GIF, EXIF, JPG, PNG and TIFF. For more information about supported formats, see Types of Bitmaps.

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. 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.

Note

The Bitmap class is not accessible across application domains. 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. However, if you call the DrawImage method to draw the marshaled Bitmap, you receive the following exception.

Remoting cannot find field "native image" on type "System.Drawing.Image".

Constructors

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

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)

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)

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)

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)

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)

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)

Initializes a new instance of the Bitmap class with the specified size and format.

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

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)

Initializes a new instance of the Bitmap class from the specified data stream.

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

Initializes a new instance of the Bitmap class from the specified file.

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

Initializes a new instance of the Bitmap class from the specified file.

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

Initializes a new instance of the Bitmap class from a specified resource.

Methods

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

Creates an exact copy of this Image.

(Inherited from Image)
Clone(Rectangle, PixelFormat) Clone(Rectangle, PixelFormat) Clone(Rectangle, PixelFormat) Clone(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)

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()

Releases all resources used by this Image.

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

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)

Creates a Bitmap from a Windows handle to an icon.

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

Creates 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()

Creates a GDI bitmap object from this Bitmap.

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

Creates a GDI 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)

Gets the color of the specified pixel in this Bitmap.

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

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)

Returns a thumbnail for this Image.

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

Gets 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)

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)

Locks a Bitmap into system memory

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

Makes the default transparent color transparent for this Bitmap.

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

Makes the specified color transparent for this Bitmap.

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

Creates a shallow copy of the current Object.

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

Creates a shallow copy of the current MarshalByRefObject object.

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

Removes the specified property item from this Image.

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

Rotates, 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)

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)

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)

Saves this Image to the specified file in the specified format.

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

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)

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)

Sets the color of the specified pixel in this Bitmap.

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

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)

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)

Unlocks this Bitmap from system memory.

Properties

Flags Flags Flags Flags

Gets attribute flags for the pixel data of this Image.

(Inherited from Image)
FrameDimensionsList FrameDimensionsList FrameDimensionsList FrameDimensionsList

Gets an array of GUIDs that represent the dimensions of frames within this Image.

(Inherited from Image)
Height Height Height Height

Gets the height, in pixels, of this Image.

(Inherited from Image)
HorizontalResolution HorizontalResolution HorizontalResolution HorizontalResolution

Gets the horizontal resolution, in pixels per inch, of this Image.

(Inherited from Image)
Palette Palette Palette Palette

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

Gets the pixel format for this Image.

(Inherited from Image)
PropertyIdList PropertyIdList PropertyIdList PropertyIdList

Gets IDs of the property items stored in this Image.

(Inherited from Image)
PropertyItems PropertyItems PropertyItems PropertyItems

Gets all the property items (pieces of metadata) stored in this Image.

(Inherited from Image)
RawFormat RawFormat RawFormat RawFormat

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

Gets the vertical resolution, in pixels per inch, of this Image.

(Inherited from Image)
Width Width Width Width

Gets the width, in pixels, of this Image.

(Inherited from Image)

Explicit Interface Implementations

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

Populates a SerializationInfo with the data needed to serialize the target object.

(Inherited from Image)

Applies to

See Also