設置 | 登錄 | 註冊

作者共發了4篇帖子。

【程序】由位图裸数据得到位图句柄HBITMAP——CreateDIBitmap

1樓 巨大八爪鱼 2016-6-24 16:49
#include <stdio.h>
#include <Windows.h>

LPBYTE AllocateBits(int nWidth, int nHeight, int nBitcCount, int *pSize)
{
    *pSize = ((nWidth * nBitcCount + 31) / 32) * 4 * nHeight;
    if (*pSize < 0)
        *pSize = -*pSize;
    return (LPBYTE)malloc(*pSize);
}

int main(void)
{
    BITMAPINFOHEADER bmh;
    HBITMAP hbmp;
    HDC hdc;
    int size;
    LPBYTE pBits;

    ZeroMemory(&bmh, sizeof(bmh));
    bmh.biSize = sizeof(bmh);
    bmh.biBitCount = 24;
    bmh.biPlanes = 1;
    bmh.biWidth = 200;
    bmh.biHeight = 100;

    pBits = AllocateBits(bmh.biWidth, bmh.biHeight, bmh.biBitCount, &size);
    memset(pBits, 0x99, size);

    hdc = GetDC(NULL);
    hbmp = CreateDIBitmap(hdc, &bmh, CBM_INIT, pBits, (BITMAPINFO *)&bmh, DIB_RGB_COLORS);
    ReleaseDC(NULL, hdc);
    
    OpenClipboard(NULL);
    EmptyClipboard();
    SetClipboardData(CF_BITMAP, hbmp);
    CloseClipboard();

    free(pBits);
    return 0;
}
2樓 巨大八爪鱼 2016-6-24 16:50
注意:CreateDIBitmap函数实际上是:Create DIB-generated Bitmap
最终创建的是设备有关位图(DDB)!


参考资料:http://www.cnblogs.com/staring-hxs/archive/2013/08/17/3264896.html
3樓 巨大八爪鱼 2016-6-24 18:24
CreateDIBitmap的主要功能:由DIB创建DDB。
调用该函数等价于先调用CreateCompatibleBitmap创建一个空的DDB,然后调用SetDIBits将DIB数据转换为DDB。
4樓 巨大八爪鱼 2016-6-24 18:36
hbmp = CreateDIBitmap(hdc, &bmh, CBM_INIT, pBits, (BITMAPINFO *)&bmh, DIB_RGB_COLORS);
等价于:
hbmp = CreateCompatibleBitmap(hdc, bmh.biWidth, bmh.biHeight);
SetDIBits(hdc, hbmp, 0, bmh.biHeight, pBits, (LPBITMAPINFO)&bmh, DIB_RGB_COLORS);

內容轉換:

回覆帖子
內容:
用戶名: 您目前是匿名發表。
驗證碼:
看不清?換一張
©2010-2025 Purasbar Ver3.0 [手機版] [桌面版]
除非另有聲明,本站採用知識共享署名-相同方式共享 3.0 Unported許可協議進行許可。