2 out of 2 rated this helpful - Rate this topic

BitmapData Class

Specifies the attributes of a bitmap image. The BitmapData class is used by the LockBits and UnlockBits methods of the Bitmap class. Not inheritable.

System.Object
  System.Drawing.Imaging.BitmapData

Namespace:  System.Drawing.Imaging
Assembly:  System.Drawing (in System.Drawing.dll)
public sealed class BitmapData

The BitmapData type exposes the following members.

  Name Description
Public method BitmapData Initializes a new instance of the BitmapData class.
Top
  Name Description
Public property Height Gets or sets the pixel height of the Bitmap object. Also sometimes referred to as the number of scan lines.
Public property PixelFormat Gets or sets the format of the pixel information in the Bitmap object that returned this BitmapData object.
Public property Reserved Reserved. Do not use.
Public property Scan0 Gets or sets the address of the first pixel data in the bitmap. This can also be thought of as the first scan line in the bitmap.
Public property Stride Gets or sets the stride width (also called scan width) of the Bitmap object.
Public property Width Gets or sets the pixel width of the Bitmap object. This can also be thought of as the number of pixels in one scan line.
Top
  Name Description
Public method Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected method Finalize Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public method GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Protected method MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public method ToString Returns a string that represents the current object. (Inherited from Object.)
Top

The following code example demonstrates how to use the BitmapData class with the LockBits and UnlockBits methods. This example is designed to be used with Windows Forms. To run this example, paste it into a form and handle the form's Paint event by calling the LockUnlockBitsExample method, passing e as PaintEventArgs.


    private void LockUnlockBitsExample(PaintEventArgs e)
        {

            // Create a new bitmap.
            Bitmap bmp = new Bitmap("c:\\fakePhoto.jpg");

            // Lock the bitmap's bits.  
            Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
            System.Drawing.Imaging.BitmapData bmpData =
                bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite,
                bmp.PixelFormat);

            // Get the address of the first line.
            IntPtr ptr = bmpData.Scan0;

            // Declare an array to hold the bytes of the bitmap.
            int bytes  = Math.Abs(bmpData.Stride) * bmp.Height;
���         byte[] rgbValues = new byte[bytes];

            // Copy the RGB values into the array.
            System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);

            // Set every third value to 255. A 24bpp bitmap will look red.  
            for (int counter = 2; counter < rgbValues.Length; counter += 3)
                rgbValues[counter] = 255;

            // Copy the RGB values back to the bitmap
            System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes);

            // Unlock the bits.
            bmp.UnlockBits(bmpData);

            // Draw the modified image.
            e.Graphics.DrawImage(bmp, 0, 150);

        }
    


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

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.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Error in the example for images whose width is not divisible by four
I'm very new to Bitmap processing using the .NET package. Trying out the example which sets each 3rd byte to 255 on a sample image, I ended up with colored lines (blue, green, red) in my image. The reason seems to be that my image width is not divisible by four, so there are some hidden paddings inserted at the end of each scan line. Try comparing 3*bmp.Width to bmp.Stride for 24 bits per pixel images. A solution is to use two loops and thereby skip these padded bytes:
for (int iLine = 0; iLine < bmpData.Height; iLine++)
     for (int counter = 2; counter < bmpData.Stride; counter+=3)
          rgbValues[iLine*bmpData.Stride + counter] = 255;