C ++ Gdiplus单色像素值

时间:2015-04-01 18:14:25

标签: c++ bitmap gdi+

我有一个单色位图。我用它来进行碰撞检测。

// creates the monochrome bitmap
bmpTest = new Bitmap(200, 200, PixelFormat1bppIndexed);

// color and get the pixel color at point (x, y)
Color color;
bmpTest->GetPixel(110,110,&color);

// the only method I know of that I can get a 0 or 1 from.
int b = color.GetB();   

// b is 0 when the color is black and 1 when it is not black as desired

有更快的方法吗?我只能在Get A R G B()值上使用它。我正在使用GetB(),因为任何ARGB值都是0或1,正确,但对我来说似乎很乱。

有没有办法可以从单色位图中读取一个返回0或1的字节? (是问题)

1 个答案:

答案 0 :(得分:1)

您应该使用LockBits()方法来加快访问速度:

BitmapData bitmapData;
pBitmap->LockBits(&Rect(0,0,pBitmap->GetWidth(), pBitmap->GetHeight()), ImageLockModeWrite, PixelFormat32bppARGB, &bitmapData);

unsigned int *pRawBitmapOrig = (unsigned int*)bitmapData.Scan0;   // for easy access and indexing

unsigned int curColor = pRawBitmapCopy[curY * bitmapData.Stride / 4 + curX];
int b = curColor & 0xff;
int g = (curColor & 0xff00) >> 8;
int r = (curColor & 0xff0000) >> 16;
int a = (curColor & 0xff000000) >> 24;