tagBITMAPFILEHEADER bh;
BITMAPINFOHEADER bih;
char buf[3];
unsigned char bmp[200][600];
int width ,height;
ifstream fin(L"D:\\xx\\3.bmp",ios::_Nocreate|ios::binary);
fin.read((char*)&bh,sizeof(bh));
fin.read((char*)&bih,sizeof(bih));
width=bih.biWidth;
height=bih.biHeight;
HWND myconsole=GetConsoleWindow();
HDC mydc=GetDC(myconsole);
for(int i=height;i>=1;i--)
for(int j=1;j<=width;j++)
{
fin.read(buf,sizeof(buf));
bmp[i][j*3]=buf[0];
bmp[i][j*3+1]=buf[1];
bmp[i][j*3+2]=buf[2];
}
for(int i=1;i<=height;i++)
{
for(int j=1;j<=width;j++)
{
COLORREF color;
color=RGB(bmp[i][j*3],bmp[i][j*3+1],bmp[i][j*3+2]);
SetPixel(mydc,j,i,color);
}
//cout<<width<<endl;
}
ReleaseDC(myconsole,mydc);
fin.close();
return 0;
我将像素读取到数组中,这个 bmp 的位数是 24,所以我使用 char[3] 来存储数据,但最终像这样显示在控制台上,我无法弄清楚。