如果我定义了这样的结构:
struct rgb
{
double r;
double g;
double b;
};
每个元素都是一个百分比。如何从 C 或 C++ 中的 .bmp 文件中获取这些值?谢谢!
我不确定,但您可以参考以下链接以了解如何获取 rgb 值。
如果他们提到了下面提到的示例,还请检查以下链接:-
//Pass the handle to the window on which you may want to draw
struct vRGB
{
vRGB():R(0), G(0), B(0){}
vRGB(BYTE r, BYTE g, BYTE b):R(r), G(b), B(b){}
BYTE R, G, B;
};
void GetBitmapPixel(HWND hwnd, std::vector<std::vector<vRGB>> &pixel){
BITMAP bi;
//Load Bitmap from resource file
HBITMAP hBmp = LoadBitmap(GetModuleHandle(0),MAKEINTRESOURCE(IDB_BITMAP1));
//Get the Height and Width
::GetObject(hBmp, sizeof(bi), &bi);
int Width = bi.bmWidth; int Height = bi.bmHeight;
//Allocate and Initialize enough memory for external (Y-dimension) array
pixel.resize(Height);
//Create a memory device context and place your bitmap
HDC hDC = GetDC(hwnd);
HDC hMemDC = ::CreateCompatibleDC(hDC);
SelectObject(hMemDC, hBmp);
DWORD pixelData = 0;
for (int y = 0; y < Height; ++y)
{
//Reserve memory for each internel (X-dimension) array
pixel[y].reserve(Width);
for (int x = 0; x < Width; ++x)
{
//Add the RGB pixel information to array.
pixelData = GetPixel(hMemDC, x, y);
pixel[y].push_back(vRGB(GetRValue(pixelData), GetGValue(pixelData), GetBValue(pixelData)));
}
}
//Cleanup device contexts
DeleteDC(hMemDC);
ReleaseDC(hwnd, hDC);
}