Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Bitmap Class

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.

Namespace:  System.Drawing
Assembly:  System.Drawing (in System.Drawing.dll)

[SerializableAttribute]
[ComVisibleAttribute(true)]
public sealed class Bitmap : Image

The Bitmap type exposes the following members.

  NameDescription
Public methodBitmap(Image)Initializes a new instance of the Bitmap class from the specified existing image.
Public methodBitmap(Stream)Initializes a new instance of the Bitmap class from the specified data stream.
Public methodBitmap(String)Initializes a new instance of the Bitmap class from the specified file.
Public methodBitmap(Image, Size)Initializes a new instance of the Bitmap class from the specified existing image, scaled to the specified size.
Public methodBitmap(Int32, Int32)Initializes a new instance of the Bitmap class with the specified size.
Public methodBitmap(Stream, Boolean)Initializes a new instance of the Bitmap class from the specified data stream.
Public methodBitmap(String, Boolean)Initializes a new instance of the Bitmap class from the specified file.
Public methodBitmap(Type, String)Initializes a new instance of the Bitmap class from a specified resource.
Public methodBitmap(Image, Int32, Int32)Initializes a new instance of the Bitmap class from the specified existing image, scaled to the specified size.
Public methodBitmap(Int32, Int32, Graphics)Initializes a new instance of the Bitmap class with the specified size and with the resolution of the specified Graphics object.
Public methodBitmap(Int32, Int32, PixelFormat)Initializes a new instance of the Bitmap class with the specified size and format.
Public methodBitmap(Int32, Int32, Int32, PixelFormat, IntPtr)Initializes a new instance of the Bitmap class with the specified size, pixel format, and pixel data.
Top

  NameDescription
Public propertyFlagsGets attribute flags for the pixel data of this Image. (Inherited from Image.)
Public propertyFrameDimensionsListGets an array of GUIDs that represent the dimensions of frames within this Image. (Inherited from Image.)
Public propertyHeightGets the height, in pixels, of this Image. (Inherited from Image.)
Public propertyHorizontalResolutionGets the horizontal resolution, in pixels per inch, of this Image. (Inherited from Image.)
Public propertyPaletteGets or sets the color palette used for this Image. (Inherited from Image.)
Public propertyPhysicalDimensionGets the width and height of this image. (Inherited from Image.)
Public propertyPixelFormatGets the pixel format for this Image. (Inherited from Image.)
Public propertyPropertyIdListGets IDs of the property items stored in this Image. (Inherited from Image.)
Public propertyPropertyItemsGets all the property items (pieces of metadata) stored in this Image. (Inherited from Image.)
Public propertyRawFormatGets the file format of this Image. (Inherited from Image.)
Public propertySizeGets the width and height, in pixels, of this image. (Inherited from Image.)
Public propertyTagGets or sets an object that provides additional data about the image. (Inherited from Image.)
Public propertyVerticalResolutionGets the vertical resolution, in pixels per inch, of this Image. (Inherited from Image.)
Public propertyWidthGets the width, in pixels, of this Image. (Inherited from Image.)
Top

  NameDescription
Public methodClone()Creates an exact copy of this Image. (Inherited from Image.)
Public methodClone(Rectangle, PixelFormat)Creates a copy of the section of this Bitmap defined by Rectangle structure and with a specified PixelFormat enumeration.
Public methodClone(RectangleF, PixelFormat)Creates a copy of the section of this Bitmap defined with a specified PixelFormat enumeration.
Public methodCreateObjRefCreates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object. (Inherited from MarshalByRefObject.)
Public methodDispose()Releases all resources used by this Image. (Inherited from Image.)
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Public methodStatic memberFromHiconCreates a Bitmap from a Windows handle to an icon.
Public methodStatic memberFromResourceCreates a Bitmap from the specified Windows resource.
Public methodGetBoundsGets the bounds of the image in the specified unit. (Inherited from Image.)
Public methodGetEncoderParameterListReturns information about the parameters supported by the specified image encoder. (Inherited from Image.)
Public methodGetFrameCountReturns the number of frames of the specified dimension. (Inherited from Image.)
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetHbitmap()Creates a GDI bitmap object from this Bitmap.
Public methodGetHbitmap(Color)Creates a GDI bitmap object from this Bitmap.
Public methodGetHiconReturns the handle to an icon.
Public methodGetLifetimeServiceRetrieves the current lifetime service object that controls the lifetime policy for this instance. (Inherited from MarshalByRefObject.)
Public methodGetPixelGets the color of the specified pixel in this Bitmap.
Public methodGetPropertyItemGets the specified property item from this Image. (Inherited from Image.)
Public methodGetThumbnailImageReturns a thumbnail for this Image. (Inherited from Image.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodInitializeLifetimeServiceObtains a lifetime service object to control the lifetime policy for this instance. (Inherited from MarshalByRefObject.)
Public methodLockBits(Rectangle, ImageLockMode, PixelFormat)Locks a Bitmap into system memory.
Public methodLockBits(Rectangle, ImageLockMode, PixelFormat, BitmapData)Locks a Bitmap into system memory
Public methodMakeTransparent()Makes the default transparent color transparent for this Bitmap.
Public methodMakeTransparent(Color)Makes the specified color transparent for this Bitmap.
Public methodRemovePropertyItemRemoves the specified property item from this Image. (Inherited from Image.)
Public methodRotateFlipRotates, flips, or rotates and flips the Image. (Inherited from Image.)
Public methodSave(String)Saves this Image to the specified file or stream. (Inherited from Image.)
Public methodSave(Stream, ImageFormat)Saves this image to the specified stream in the specified format. (Inherited from Image.)
Public methodSave(String, ImageFormat)Saves this Image to the specified file in the specified format. (Inherited from Image.)
Public methodSave(Stream, ImageCodecInfo, EncoderParameters)Saves this image to the specified stream, with the specified encoder and image encoder parameters. (Inherited from Image.)
Public methodSave(String, ImageCodecInfo, EncoderParameters)Saves this Image to the specified file, with the specified encoder and image-encoder parameters. (Inherited from Image.)
Public methodSaveAdd(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.)
Public methodSaveAdd(Image, EncoderParameters)Adds a frame to the file or stream specified in a previous call to the Save method. (Inherited from Image.)
Public methodSelectActiveFrameSelects the frame specified by the dimension and index. (Inherited from Image.)
Public methodSetPixelSets the color of the specified pixel in this Bitmap.
Public methodSetPropertyItemStores a property item (piece of metadata) in this Image. (Inherited from Image.)
Public methodSetResolutionSets the resolution for this Bitmap.
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Public methodUnlockBitsUnlocks this Bitmap from system memory.
Top

  NameDescription
Explicit interface implemetationPrivate methodISerializable.GetObjectDataPopulates a SerializationInfo with the data needed to serialize the target object. (Inherited from Image.)
Top

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.

NoteNote

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

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.

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.");
    }
}

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Show:
© 2014 Microsoft