Export (0) Print
Expand All

How to: Use LockBits

[This documentation is for preview only, and is subject to change in later releases. Blank topics are included as placeholders.]

The .NET Compact Framework provides support for the LockBits method, which enables you to manipulate a bitmap's array of pixels in an unmanaged memory buffer and then replace the pixels in the bitmap with the pixels from the buffer.

The .NET Compact Framework does not support the LockBits overload that takes a BitmapData as a parameter. You can use the ReadWrite, ReadOnly, and WriteOnly values for the ImageLockMode. The .NET Compact Framework supports only the following PixelFormat values:

Note that because the .NET Compact Framework does not support the PixelFormat property of an Image, you must explicitly specify its pixel format value by using the format parameter in LockBits.

The following example creates a bitmap and uses LockBits to change the intensity of a series of blue pixels.

private Bitmap CreateBitmap(int width, int height, string s)
    Bitmap bmp = new Bitmap(width, height);

    Graphics g = Graphics.FromImage(bmp);
    g.FillRectangle(new SolidBrush(Color.LightCoral), 0, 0, bmp.Width, bmp.Height);
    g.DrawRectangle(new Pen(Color.Green, 10), 5, 5, bmp.Width - 10, bmp.Height - 10);
    g.DrawLine(new Pen(Color.Yellow, 15), 0, 0, bmp.Width, bmp.Height);
    g.DrawLine(new Pen(Color.Yellow, 15), bmp.Width, 0, 0, bmp.Height);

    SizeF size = g.MeasureString(s, this.Font);
    g.DrawString(s, this.Font, new SolidBrush(Color.Black),
                 (bmp.Width - size.Width) / 2,
                 (bmp.Height - size.Height) / 2);

    return bmp;

protected override void OnPaint(PaintEventArgs e)
    Bitmap bmp = CreateBitmap(100, 100, "Hi Mom!");
    e.Graphics.DrawImage(bmp, 0, 0);


    // Draw the modified image to the right of the original one
    e.Graphics.DrawImage(bmp, 120, 0);


private void MakeMoreBlue(Bitmap bmp)
    // Specify a pixel format.
    PixelFormat pxf = PixelFormat.Format24bppRgb;

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

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

    // Declare an array to hold the bytes of the bitmap.
    // int numBytes = bmp.Width * bmp.Height * 3;
    int numBytes = bmpData.Stride * bmp.Height;
    byte[] rgbValues = new byte[numBytes];

    // Copy the RGB values into the array.
    Marshal.Copy(ptr, rgbValues, 0, numBytes);

    // Manipulate the bitmap, such as changing the
    // blue value for every other pixel in the the bitmap.
    for(int counter = 0; counter < rgbValues.Length; counter+=6)
        rgbValues[counter] = 255;

    // Copy the RGB values back to the bitmap
    Marshal.Copy(rgbValues, 0, ptr, numBytes);

    // Unlock the bits.

© 2014 Microsoft