亲爱的朋友们,我需要你们的帮助。
我需要将.bmp文件发送到另一个进程(对话框)并使用MMF(内存映射文件)将其显示在那里 但问题是图像以反色和颠倒的方式显示。
这是源代码:
在第一个应用程序中,我从HDD打开图片并将其链接到命名的MMF“Gigabyte_picture”
HANDLE hFile = CreateFile("123.bmp", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 0, "Gigabyte_picture");
在第二个应用程序中,我打开映射的bmp文件,最后我使用SendMessage函数在静态组件上显示m_HBitmap。
HANDLE hMappedFile = OpenFileMapping(FILE_MAP_READ, FALSE, "Gigabyte_picture");
PBYTE pbData = (PBYTE) MapViewOfFile(hMappedFile, FILE_MAP_READ, 0, 0, 0);
BITMAPINFO bmpInfo = { 0 };
LONG lBmpSize = 60608; // size of the bmp file in bytes
bmpInfo.bmiHeader.biBitCount = 32;
bmpInfo.bmiHeader.biHeight = 174;
bmpInfo.bmiHeader.biWidth = 87;
bmpInfo.bmiHeader.biPlanes = 1;
bmpInfo.bmiHeader.biSizeImage = lBmpSize;
bmpInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
UINT * pPixels = 0;
HDC hDC = CreateCompatibleDC(NULL);
HBITMAP m_HBitmap = CreateDIBSection(hDC, &bmpInfo, DIB_RGB_COLORS, (void **)& pPixels, NULL, 0);
SetBitmapBits(m_HBitmap, lBmpSize, pbData);
SendMessage(gStaticBox, STM_SETIMAGE, (WPARAM)IMAGE_BITMAP,(LPARAM)m_HBitmap);
/////////////
HWND gStaticBox = CreateWindowEx(0, "STATIC","",
SS_CENTERIMAGE | SS_REALSIZEIMAGE | SS_BITMAP | WS_CHILD | WS_VISIBLE,
10,10,380, 380, myDialog, (HMENU)-1,NULL,NULL);
答案 0 :(得分:0)
pbData指向位图数据的开头,指向位图标题。 将SetBitmapBits指针指向原始数据:pbData + header size + optional pallete。
答案 1 :(得分:0)
我编辑了代码,现在工作正常(图片颜色正确,图片不是颠倒的)
SetBitmapBits(m_HBitmap, lBmpSize, pbFile + 54);
BITMAP bm;
GetObject(m_HBitmap, sizeof(BITMAP), (LPSTR)&bm);
// this code rotate picture on 180 degrees on Y axis
HDC TempHDC = ::CreateCompatibleDC(NULL);
HBITMAP hOldBitmap2 = (HBITMAP)SelectObject(TempHDC, m_HBitmap);
::StretchBlt(TempHDC,0,0, bm.bmWidth, bm.bmHeight, TempHDC, 0, bm.bmHeight-1, bm.bmWidth,-bm.bmHeight, SRCCOPY );
SelectObject(TempHDC,hOldBitmap2);
DeleteDC(TempHDC);
// as before
SendMessage(gStaticBox, STM_SETIMAGE, (WPARAM)IMAGE_BITMAP,(LPARAM)m_HBitmap);