C++ Gdiplus 单色像素值

标签 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 的单色位图中读取一个字节? (是题)

最佳答案

您应该使用 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;

关于C++ Gdiplus 单色像素值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29397877/

相关文章:

c++ - 将窗口内容显示为位图

c++ - std::function 的问题

c++ - 美化变量模板化函数调用

c++ - 按索引更改字符串

java - 将图像加载为文件

c# - 如何用c#制作两个透明层?

c++ - 在g++ <7.4.0>上boost::archive::text_iarchive in_archive {is} boost <1.71>崩溃

c - 如何将 4 位字符数组转换为 Int?

image - 如何在vb6中将bmp转换为jpg

windows - GDIPlus Windows 用户界面的 future : which will be the replacement?