一直对win32 程序没什么研究,今次需要从基础抓起,写了一个测试程序,显示bmp图片,首先还是windows 基本程序,然后处理win_paint消息,置入如下函数
void OnBnClickedBtnShowBmp(HWND hWnd)
{
static TCHAR szFileName[MAX_PATH];
HWND hwnd;
HDC hdc;
hwnd = hWnd;
hdc = ::GetDC(hwnd);
BITMAP bitmap;
HDC hdcMem;
BITMAPFILEHEADER bmfh;
BITMAPINFO * pbmi;
BYTE * pBits;
BOOL bSuccess;
DWORD dwInfoSize, dwBytesRead;
HANDLE hFile;
HBITMAP hBitmap;
memcpy(szFileName, _T(".\\qr_code.bmp"), 50);
hFile = CreateFile(szFileName, GENERIC_READ, FILE_SHARE_READ,
NULL, OPEN_EXISTING, 0, NULL);
if (hFile == INVALID_HANDLE_VALUE)
return;
// Read in the BITMAPFILEHEADER
bSuccess = ReadFile(hFile, &bmfh, sizeof (BITMAPFILEHEADER),
&dwBytesRead, NULL);
if (!bSuccess || (dwBytesRead != sizeof (BITMAPFILEHEADER))
|| (bmfh.bfType != *(WORD *) "BM"))
{
CloseHandle(hFile);
return;
}
// Allocate memory for the BITMAPINFO structure & read it in
dwInfoSize = bmfh.bfOffBits - sizeof (BITMAPFILEHEADER);
pbmi = (BITMAPINFO *)malloc(dwInfoSize);
bSuccess = ReadFile(hFile, pbmi, dwInfoSize, &dwBytesRead, NULL);
if (!bSuccess || (dwBytesRead != dwInfoSize))
{
free(pbmi);
CloseHandle(hFile);
return;
}
// Create the DIB Section
hBitmap = CreateDIBSection(NULL, pbmi, DIB_RGB_COLORS, (void **)&pBits, NULL, 0);
if (hBitmap == NULL)
{
free(pbmi);
CloseHandle(hFile);
return;
}
// Read in the bitmap bits
ReadFile(hFile, pBits, bmfh.bfSize - bmfh.bfOffBits, &dwBytesRead, NULL);
free(pbmi);
CloseHandle(hFile);
//return hBitmap ;
if (hBitmap)
{
GetObject(hBitmap, sizeof (BITMAP), &bitmap);
hdcMem = CreateCompatibleDC(hdc);
SelectObject(hdcMem, hBitmap);
BitBlt(hdc, 0, 0, bitmap.bmWidth, bitmap.bmHeight,
hdcMem, 0, 0, SRCCOPY);
DeleteDC(hdcMem);
}
if (hBitmap)
{
DeleteObject(hBitmap);
hBitmap = NULL;
}
}
如此就可以在起始处显示bmp图片了。
下面的方式是把bmp转化位png图片的windows 插件实现,通过gdiplus读取了图片,然后转化为png
BOOL GetEncoderClsid(WCHAR* pFormat, CLSID* pClsid)
{
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
UINT num = 0, size = 0;
ImageCodecInfo* pImageCodecInfo = NULL;
GetImageEncodersSize(&num, &size);
if (size == 0)
{
return FALSE;
}
pImageCodecInfo = (Gdiplus::ImageCodecInfo*)(malloc(size));
if (pImageCodecInfo == NULL)
{
return FALSE;
}
GetImageEncoders(num, size, pImageCodecInfo);
BOOL bfound = FALSE;
for (UINT i = 0; !bfound && i < num; i++)
{
if (_wcsicmp(pImageCodecInfo[i].MimeType, pFormat) == 0)
{
*pClsid = pImageCodecInfo[i].Clsid;
bfound = TRUE;
}
}
GdiplusShutdown(gdiplusToken);
free(pImageCodecInfo);
return bfound;
}
BOOL BMptoPNG(LPCWSTR StrBMp, LPCWSTR StrPNG)
{
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
Gdiplus::GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
CLSID encoderClsid;
Status stat;
Image* image = NULL;
image = Gdiplus::Bitmap::FromFile(StrBMp, TRUE);
if (!GetEncoderClsid(L"image/png", &encoderClsid))
{
return FALSE;
}
stat = image->Save(StrPNG, &encoderClsid, NULL);
if (stat != Gdiplus::Ok)
{
return FALSE;
}
//Gdiplus::GdiplusShutdown(gdiplusToken);
delete image;
return TRUE;
}