The author has 4 posts.
Font size: Small - 100% (Default)  Content converter: No conversion
 
Clicks Replies
1234 3
【程序】由位图裸数据得到位图句柄HBITMAP——CreateDIBitmap
巨大八爪鱼
武林盟主 二十一级
Reply
Floor 1 Posted at: 6/24/16 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;
}
巨大八爪鱼
武林盟主 二十一级
Reply
Floor 2 Posted at: 6/24/16 16:50
注意:CreateDIBitmap函数实际上是:Create DIB-generated Bitmap
最终创建的是设备有关位图(DDB)!


参考资料:http://www.cnblogs.com/staring-hxs/archive/2013/08/17/3264896.html
巨大八爪鱼
武林盟主 二十一级
Reply
Floor 3 Posted at: 6/24/16 18:24
CreateDIBitmap的主要功能:由DIB创建DDB。
调用该函数等价于先调用CreateCompatibleBitmap创建一个空的DDB,然后调用SetDIBits将DIB数据转换为DDB。
巨大八爪鱼
武林盟主 二十一级
Reply
Floor 4 Posted at: 6/24/16 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);
Reply the post
Content:
User: You are currently anonymous.
Captcha:
Unclear? Try another one.
(Shortcut key: Ctrl+Enter)
Post Information
Clicks: 1234 Replies: 3
Author: 巨大八爪鱼
Last reply: 巨大八爪鱼
Last reply time: 6/24/16 18:36
Bar Good Posts
Announcements