我想在Win32应用程序上加载图像(.bmp)文件,但我不想使用
Windows API中的标准LoadBitmap / LoadImage:我希望它从已经在内存中的缓冲区加载.我可以直接从文件中轻松加载位图并将其打印在屏幕上,但这个问题让我陷入困境.
我正在寻找的功能是这样的:
HBITMAP LoadBitmapFromBuffer(char* buffer,int width,int height);
没关系,我找到了解决办法!这是初始化代码:
std::ifstream is; is.open("Image.bmp",std::ios::binary); is.seekg (0,std::ios::end); length = is.tellg(); is.seekg (0,std::ios::beg); pBuffer = new char [length]; is.read (pBuffer,length); is.close(); tagBITMAPFILEHEADER bfh = *(tagBITMAPFILEHEADER*)pBuffer; tagBITMAPINFOHEADER bih = *(tagBITMAPINFOHEADER*)(pBuffer+sizeof(tagBITMAPFILEHEADER)); RGBQUAD rgb = *(RGBQUAD*)(pBuffer+sizeof(tagBITMAPFILEHEADER)+sizeof(tagBITMAPINFOHEADER)); BITMAPINFO bi; bi.bmiColors[0] = rgb; bi.bmiHeader = bih; char* pPixels = (pBuffer+bfh.bfOffBits); char* ppvBits; hBitmap = CreateDIBSection(NULL,&bi,DIB_RGB_COLORS,(void**) &ppvBits,NULL,0); SetDIBits(NULL,hBitmap,bih.biHeight,pPixels,DIB_RGB_COLORS); GetObject(hBitmap,sizeof(BITMAP),&cBitmap);