win32显示图片

一直对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;
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值