# Reading bitmaps....24Bit is skewed and decolored

### Question

There is my exe file so you can test it for yourself.  32Bit bitmaps work fine, my code for 32 bit reads 4 bytes per pixel at a type starting from the image and sets the pixels of a bitmap and then loads that to a pictureBox.  For 24 bit, it reads 3 bytes per pixel and sets the pixels.  BUT, for the 24 bit the color is mainly grayish, but if you look close you can see some green/red/blue looking lines going across the image.  And it is also skewed like 30-50 degrees.  I think this is because of the padding of bytes at the end of lines, but I don't know how to calculate the amount of padding bytes there are going to be...

Thanks,

paoloTheCool

Saturday, June 21, 2008 3:26 AM

### All replies

• Saturday, June 21, 2008 5:28 AM
• Everybody has their hero's in these forums.

JohnWein is mine...

Saturday, June 21, 2008 2:57 PM
• Haha so that's what stride is...I will look into that =)

Thanks,

paoloTheCool

Sunday, June 22, 2008 7:44 PM
• Actually I just have one question, it would be this "stride" that would be the reason for the skewed image and the decoloration right?... Because it would mess up my iteration of reading the RGB values, right?

Thanks,

paoloTheCool

Sunday, June 22, 2008 7:50 PM
• Ok I found this link....

http://objectmix.com/dotnet/102076-bitmapdata-stride-explain.html

It says:

for 24 bit:
int stride = (width * 3 + 3) & ~3;

Can someone explain what this means?

Thanks,

paoloTheCool

Sunday, June 22, 2008 8:04 PM
• Hi palloTheCool,

Well, as I understand, the stride is a horizontal scan line size in a bitmap format picture, if you try to do some testing, you will find the following results.

int width = 105;

int stride = (width * 3 + 3) & ~3; //stride == 316 == 4(bytes) * 79

width = 234;

stride = (width * 3 + 3) & ~3; //stride == 704 == 4(bytes) * 176

That means the bitmaps picture data size will always be aligned to 4 bytes boundary, and I believe that is bitmap’s format or algorithm to store its data.

In addition to the stride of the bitmap in .Net, please refer to BitmapData.Stride Property

Regards,

Xun

Wednesday, June 25, 2008 5:51 AM
• Hey, thanks for the reply...

What I really want to know is what the "&" and "~" things mean...

Thanks,

paoloTheCool

Wednesday, June 25, 2008 1:39 PM
• You really need to learn to use the help files.  Type those symbols in the textbox on the Index tab.

Wednesday, June 25, 2008 1:50 PM
• Hi palloTheCool,

Well, the “&” and “~” in that expression above are bitwise operators.

“&” do the logical AND operation for every bits of the two operands.

“~” is used to perform a bitwise complement for only one operand. And 0 is the complement of 1.

In addition to C# operator tutorial, please refer to the following MSDN document.

http://msdn.microsoft.com/en-us/library/sbf85k1c(VS.71).aspx

http://msdn.microsoft.com/en-us/library/d2bd4x66(VS.71).aspx

http://msdn.microsoft.com/en-us/library/6a71f45d(vs.71).aspx

Regards,

Xun

Wednesday, June 25, 2008 2:58 PM