我在Microsoft Visual C ++ 6中。我有一个bmp图片。我想将图像像素分别以红色,绿色和蓝色值保存在数组中。
我使用“ BitBlt”功能在屏幕上显示图像,然后使用以下代码从屏幕上读取图像值:
它可以正常工作,但是需要很长时间才能完成。我在Stackoverflow中找到了另一种方法,并根据需要尝试了以下方法:
但是不幸的是,它没有用。 您能指导我如何有效地完成这项任务吗? 预先感谢
for (Y = 0; Y < Siz; Y++) {
for (X = 0; X < Siz; X++) {
COLORREF c = dc.GetPixel(70 + X, 110 + Y);
B[c1] = GetRValue(c);
B[c1 + 1] = GetGValue(c);
B[c1 + 2] = GetBValue(c);
}
}
其他代码
FILE *f = fopen(s, "rb");
unsigned char info[54];
fread(info, sizeof(unsigned char), 54, f); // read the 54-byte header
// extract image height and width from header
int width, height;
memcpy(&width, info + 18, sizeof(int));
memcpy(&height, info + 22, sizeof(int));
int heightSign = 1;
if (height < 0) {
heightSign = -1;
}
int size = 3 * width * abs(height);
unsigned char *data = new unsigned char[size]; // allocate 3 bytes per
pixel
fread(data, sizeof(unsigned char), size, f); // read the rest of the
data at once
fclose(f);
if (heightSign == 1) {
for (int i = 0; i < size; i += 3) {
unsigned char tmp = data[i];
data[i] = data[i + 2];
data[i + 2] = tmp;
int red = int(data[i] & 0xff);
int green = int(data[i+1] & 0xff);
int blue = int(data[i+2] & 0xff);
}
}