目前共有4篇帖子。 內容轉換:不轉換▼
 
點擊 回復
264 3
【程序】由位图裸数据得到位图句柄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);

回復帖子

內容:
用戶名: 您目前是匿名發表
驗證碼:
(快捷鍵:Ctrl+Enter)
 

本帖信息

點擊數:264 回複數:3
評論數: ?
作者:巨大八爪鱼
最後回復:巨大八爪鱼
最後回復時間:2016-6-24 18:36
 
©2010-2024 Arslanbar Ver2.0
除非另有聲明,本站採用創用CC姓名標示-相同方式分享 3.0 Unported許可協議進行許可。