Bitmap.LockBits Method
.NET Framework 4
Locks a Bitmap into system memory.
This member is overloaded. For complete information about this member, including syntax, usage, and examples, click a name in the overload list.
| Name | Description | |
|---|---|---|
|
LockBits(Rectangle, ImageLockMode, PixelFormat) | Locks a Bitmap into system memory. |
|
LockBits(Rectangle, ImageLockMode, PixelFormat, BitmapData) | Locks a Bitmap into system memory |
Simple 32-bit bitmap write using marshal.
private: Bitmap^ bmp;
private: Rectangle rect;
private: array<Byte>^rgbValues;
private: int bytes;
private: Byte shade;
private: IntPtr ptr;
private: System::Drawing::Imaging::BitmapData^ bmpData;
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
// Write pixel to array. Stride * pixel y, gives us the position of next scanline.
for ( int counter = 0; counter < rgbValues->Length; counter += 4 )
{
rgbValues[ counter ] = 0; // Blue channel.
rgbValues[ counter + 1 ] = 0; // Green channel.
rgbValues[ counter + 2 ] = 255; // Red channel.
rgbValues[ counter + 3 ] = shade; // Alpha channel ( 255 = solid color, 0 = background colour ).
}
// Lock the bitmap's bits.
bmpData = bmp->LockBits( rect, System::Drawing::Imaging::ImageLockMode::ReadWrite, bmp->PixelFormat );
// Get the address of the first line.
// Pointer to the start of bitmap memory. It is FLAT and can be treated as a one-dimensional array.
ptr = bmpData->Scan0;
// Copy the ARGB values to the bitmap. Remember it will be BGRA.
// DWORDS or QWORDS written to the bitmap are alot more efficient. This is from windows MSDN so i will stick with this.
System::Runtime::InteropServices::Marshal::Copy( rgbValues, 0, ptr, bytes );
// Unlock the bits.
bmp->UnlockBits( bmpData );
// Set the PictureBox to display the image.
// Not sure this is legal but it allows me to use alpha channel to create volumetric fog / smoke, haze, light for free!
pictureBox1->Image = bmp;
// Increase alpha channel by 16 everytime button is pressed.
shade += 16;
}
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) {
shade = 0;
// Create a new bitmap.
// Bitmap("filename", true) will load a bitmap file into the memory bitmap.
bmp = gcnew Bitmap( pictureBox1->Width,pictureBox1->Height,System::Drawing::Imaging::PixelFormat::Format32bppArgb );
// Lock the bitmap's bits.
rect = Rectangle(0,0,bmp->Width,bmp->Height);
bmpData = bmp->LockBits( rect, System::Drawing::Imaging::ImageLockMode::ReadWrite, bmp->PixelFormat );
// Declare an array to hold the bytes of the bitmap.
// This code is specific to a bitmap with 32 bits per pixels.
// Always use stride * height, not width * height.
bytes = bmpData->Stride * bmp->Height;
rgbValues = gcnew array<Byte>(bytes);
// Unlock the bits.
bmp->UnlockBits( bmpData );
// Display the pixel format in Label1.
label1->Text = String::Format( "Pixel format: {0}", bmp->PixelFormat );
//See if stride equals 640 * 4 = 2560, can also equal -2560.
numericUpDown1->Value = bmpData->Stride;
//Compare to Width * 4
numericUpDown2->Value = bmp->Width * 4;
//If bmp->Width * 4 is equal to bmpData->Stride then we can write to one-dimensional array.
}
- 11/3/2010
- voxel32
LockBits Example
See here for example of using LockBits:
http://msdn.microsoft.com/en-us/library/5ey6h79d.aspx
http://msdn.microsoft.com/en-us/library/5ey6h79d.aspx
- 8/27/2010
- Ade Miller