/* class Image { public: Image() = delete; Image(const uint32_t& _w, const uint32_t& _h) :w(_w), h(_h) { data = new byte[w*h * 4]; }; ~Image() { if (data) { delete[] data; } } uint32_t w; uint32_t h; byte* data; }; */ BITMAPINFO dib; memset(&dib, 0, sizeof(BITMAPINFO)); dib.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); dib.bmiHeader.biWidth = image->w; dib.bmiHeader.biHeight =-(int) image->h; dib.bmiHeader.biPlanes = 1; dib.bmiHeader.biBitCount = 32; dib.bmiHeader.biCompression = BI_RGB; dib.bmiHeader.biSizeImage = image->w * image->h * 4; BITMAPFILEHEADER fileHeader; fileHeader.bfType = 0x4d42; // BM fileHeader.bfReserved1 = 0; fileHeader.bfReserved2 = 0; fileHeader.bfSize = image->w * image->h * 4 + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER); fileHeader.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER); CFile file; //保存到文件 if (file.Open("E:\temp\1.bmp", CFile::modeCreate | CFile::modeWrite)) { file.Write(&fileHeader, sizeof(BITMAPFILEHEADER)); file.Write(&dib.bmiHeader, sizeof(BITMAPINFOHEADER)); file.Write(image->data, image->w * image->h * 4); file.Close(); } //渲染到设备 SetDIBitsToDevice(this->GetDC()->GetSafeHdc(), 0, 0, image->w, image->h, 0, 0, 0, image->h, image->data, &dib, DIB_RGB_COLORS);
测试可行