Click to See Complete Forum and Search --> : Creating a Bitmap using LockBits Method


guru_apprentice
April 29th, 2004, 04:37 AM
Hi all,

I'm hoping someone could point me in the right direction with the LockBits method of System::Drawing::Imaging.

Initially, I would like to create a bitmap image from scratch and display it using graphics->DrawImage().

I have tried numerous approaches to this but unfortunately without any luck. The following code should nominally create and display a 500*500 pixel aqua coloured image on my screen. The code compiles without error but the screen remains blank. Any assistance will be greatly appreciated.

private: System::Void btn1_Click(System::Object * sender, System::EventArgs * e)

{

//Create Grphics object
Graphics * gr = CreateGraphics();

//Create a bitmap
System::Drawing::Bitmap * B = new System::Drawing::Bitmap(500,500);

//Create a Clone
System::Drawing::Rectangle rect(0,0,500,500);

System::Drawing::Bitmap * bitmap = B-> System::Drawing::Bitmap::Clone(rect, System::Drawing::Imaging::PixelFormat::Format32bppArgb);

//Create a BitmapData object (bitmapdata), and then use LockBits/UnlockBits
System::Drawing::Imaging::BitmapData * bitmapdata = new System::Drawing::Imaging::BitmapData;

bitmap->System::Drawing::Bitmap::LockBits(rect, System::Drawing::Imaging::ImageLockMode::ReadWrite, System::Drawing::Imaging::PixelFormat::Format32bppArgb);

Int32 p[,]= new Int32[500,500];

for (int y=0; y<500; ++y) {
for(int x=0; x < 500; ++x) {

p[y,x] = 0xff00ffff;

}
}

bitmapdata->set_Width(500);
bitmapdata->set_Height(500);
bitmapdata->set_Stride(4*500);
bitmapdata„³set_PixelFormat(System::Drawing::Imaging::PixelFormat::Format32bppArgb);
bitmapdata->set_Scan0((System::IntPtr)&p);
bitmapdata->set_Reserved(NULL);

bitmap->UnlockBits(bitmapdata);

gr->DrawImage(bitmap,20,80);
gr->Dispose();
bitmap->Dispose();

}