How to create and change values in a 24bpp bitmap?

J

James Dean

I create a bitmap like this.
The width and height are got from the compressed file i am reading. The
width and height are in pixels.....1bpp
bitmap = new Bitmap(Width,Height,PixelFormat.Format24bppRgb);

i have a bitmapdata class and then lock the bits like this.

bitmapData =
bitmap.LockBits(bounds, ImageLockMode.ReadWrite,
PixelFormat.Format24bppRgb);

pBase = (Byte*) bitmapData.Scan0.ToPointer();

My problem is i want to set the values of these bytes. I thought i was
setting the values but i am not doing it right for some reason.

I followed this example but here he reads in and changes the bitmap.....
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dncscol
/html/csharp11152001.asp

I create the bitmap myself and change the values...but its different...

What i want to know is if somebody could tell me how to create a 24bpp
bitmap and then set any values in it...just a small example...just some
bytes in the bitmap and actually display this.....at the moment all i
get is rubbish as a result. I get just a black bitmap all the time when
i display it......whats wrong i wonder?.

*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
 
G

Guest

Hi James

I create a bitmap like this.
The width and height are got from the compressed file i am reading. The
width and height are in pixels.....1bpp
bitmap = new Bitmap(Width,Height,PixelFormat.Format24bppRgb);

i have a bitmapdata class and then lock the bits like this.

bitmapData =
bitmap.LockBits(bounds, ImageLockMode.ReadWrite,
PixelFormat.Format24bppRgb);

pBase = (Byte*) bitmapData.Scan0.ToPointer();

My problem is i want to set the values of these bytes. I thought i was
setting the values but i am not doing it right for some reason.

I followed this example but here he reads in and changes the bitmap.....
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dncscol
/html/csharp11152001.asp

I create the bitmap myself and change the values...but its different...

What i want to know is if somebody could tell me how to create a 24bpp
bitmap and then set any values in it...just a small example...just some
bytes in the bitmap and actually display this.....at the moment all i
get is rubbish as a result. I get just a black bitmap all the time when
i display it......whats wrong i wonder?.

Here is a sample code for you:

public static byte[] GetRowData(Bitmap bmp, int rowNumber) {
if( rowNumber<0 || rowNumber>=bmp.Height ) {
throw new ArgumentOutOfRangeException("rowNumber"
, "Row number out of range!");
}
BitmapData bmpData=bmp.LockBits(GetRowExtent(bmp, rowNumber)
, ImageLockMode.ReadOnly
, bmp.PixelFormat);
byte[] rowData=GetRowDataUnsafe(bmpData);
bmp.UnlockBits(bmpData);
return rowData;
}

private static Rectangle GetRowExtent(Bitmap bmp, int rowNumber) {
return new Rectangle(0
, rowNumber
, bmp.Width
, 1);
}

private static unsafe byte[] GetRowDataUnsafe(BitmapData bmpData) {
int rowDataLength=bmpData.Width * 3; // 3 - for 24bpp
byte[] rowData=new byte[rowDataLength];
byte* bmpRowDataPtr=(byte*)bmpData.Scan0;
fixed( byte* rowDataPtr=rowData ) {
for(int i=0; i<rowDataLength; i++) {
(*(rowDataPtr+i))=(*(bmpRowDataPtr+i));
}
}
return rowData;
}

public static void SetRowData(Bitmap bmp
, int rowNumber
, byte[] rowData) {
if( rowNumber<0 || rowNumber>=bmp.Height ) {
throw new ArgumentOutOfRangeException("rowNumber"
, "Row number out of range!");
}
BitmapData bmpData=bmp.LockBits(GetRowExtent(bmp, rowNumber)
, ImageLockMode.WriteOnly
, bmp.PixelFormat);
SetRowDataUnsafe(bmpData, rowData);
bmp.UnlockBits(bmpData);
}

private unsafe void SetRowDataUnsafe(BitmapData bmpData
, byte[] rowData) {
int rowDataLength=rowData.Length;
byte* bmpRowDataPtr=(byte*)bmpData.Scan0;
fixed( byte* rowDataPtr=rowData ) {
for(int i=0; i<rowDataLength; i++) {
(*(bmpRowDataPtr+i))=(*(rowDataPtr+i));
}
}
}

If there is any error then get me know!

Regards

Marcin
 
J

James Dean

Thanks alot it seems to have worked......why did my previous method not
work i wonder?. It would only work if i unlocked the bits from an
already created bitmap?......
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top