How to properly recreate BITMAP, that was previously shared by CreateFileMapping()?
Dear friends, I need your help.
I need to send .bmp file to another process (dialog box) and display it there, using MMF(Memory Mapped File) But the problem is that image displays in reversed colors and upside down.
Here's source code:
In first application I open picture from HDD and link it to the named 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");
In second application I open mapped bmp file and at the end I display m_HBitmap on the static component, using SendMessage function.
HANDLE hMappedFile = OpenFileMapping(FILE_MAP_READ, FALSE, "Gigabyte_picture");
PBYTE pbData = (PBYTE开发者_Go百科) 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);
pbData points to begin of bitmap data, which points to bitmap header. Give SetBitmapBits pointer to raw data: pbData + header size + optional pallete.
I edited code, and it works OK now(picture colors are correct, image is NOT upside down)
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);
精彩评论