Warning: file_get_contents(https://whois.pconline.com.cn/jsLabel.jsp?ip=127.0.0.1) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.1 503 Service Temporarily Unavailable in D:\wwwroot\huidong\wwwroot\function.inc.php on line 884
GDI加载图片(位图)到HBITMAP,并显示到屏幕上 - huidong

huidong

首页 | 会员登录 | 关于争取 2022 寒假做出汇东网 Ver3.0.0 !
搜索文章


http://blog.chinaunix.net/uid-31439230-id-5763532.html


主要代码

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    int wmId, wmEvent;
    PAINTSTRUCT ps;
    HDC hdc;

    switch (message)
    {
    case WM_PAINT:
        {
            hdc = BeginPaint(hWnd, &ps);
            // TODO: 在此添加任意绘图代码...
            HDC hDCMem = CreateCompatibleDC(hdc);//创建兼容DC
            HBITMAP hOldBmp = (HBITMAP)SelectObject(hDCMem,hbmpBack);//把hbmBack的位图选择到兼容DC HDCMem,之后这个兼容DC就拥有和hbmpBack同样大小的绘图区域
            BITMAP bmp;
            GetObject(hbmpBack,sizeof(BITMAP),&bmp);//获取位图的大小信息,事实上也是兼容DC绘图输出的范围
#if 0
            BitBlt(hdc,0,0,bmp.bmWidth,bmp.bmHeight,hDCMem,0,0,SRCCOPY);//原样拷贝,不支持拉伸
#else
            RECT rcClient;
            GetClientRect(hWnd,&rcClient);//获得客户区的大小
            int nWidth = rcClient.right - rcClient.left;//客户区的宽度
            int nHeight = rcClient.bottom - rcClient.top;//客户区的高度
            StretchBlt(hdc,0,0,nWidth,nHeight,hDCMem,0,0,bmp.bmWidth,bmp.bmHeight,SRCCOPY);//拉伸拷贝
#endif
            SelectObject(hDCMem,hOldBmp);//复原兼容DC数据
            DeleteDC(hDCMem);//删除兼容DC,避免内存泄漏
            EndPaint(hWnd, &ps);
            break;
        }
    }
}



提炼之后:

// 图像
struct BITMAP_IMAGE
{
    HBITMAP hBmp;
    HDC hDC;

}typedef GXImage;


// 加载图像
GXImage LoadImage(LPCTSTR src)
{
    GXImage img;

    HBITMAP hBmp = (HBITMAP)LoadImage(NULL, src, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
    HDC hDCMem = CreateCompatibleDC(GetHDC());                // 创建DC
    HBITMAP hOldBmp = (HBITMAP)SelectObject(hDCMem, hBmp);    // 把位图画入到DC

    img.hBmp = hBmp;
    img.hDC = hDCMem;

    return img;
}

// 得到图像大小
// hBmp 如果需要,可以得到此图像的BITMAP
POINT GetImageSize(GXImage img, BITMAP* hBmp = NULL)
{
    BITMAP bmp;
    GetObject(img.hBmp, sizeof(BITMAP), &bmp);// 获取位图的大小信息,事实上也是DC绘图输出的范围

    if (hBmp != NULL)
    {
        *hBmp = bmp;
    }

    POINT size = { bmp.bmWidth,bmp.bmHeight };
    return size;
}

// 删除图像
void DeleteImage(GXImage img)
{
    DeleteObject(img.hBmp);    // 删除HBITMAP
    DeleteDC(img.hDC);        // 删除DC
}

/*
    显示图像
    img 图像
    x,y 图像显示位置
    isCut 是否裁剪图像
    rctCut 裁剪的图像区域
    isReSize 是否调整图像大小
    width,height 调整后的图像大小
*/
void PutImage(
    GXImage img,
    int x = 0, int y = 0,
    bool isCut = false, RECT* rctCut = NULL,
    bool isReSize = false, int width = 0, int height = 0
)
{
    BITMAP bmp;
    GetImageSize(img, &bmp);

    if (isReSize)
    {
        StretchBlt(
            GetHDC(), x, y, width, height,
            img.hDC, isCut == true ? rctCut->left : 0, isCut == true ? rctCut->top : 0,
            isCut == true ? rctCut->right : bmp.bmWidth, isCut == true ? rctCut->bottom : bmp.bmHeight,
            SRCCOPY
        );// 拉伸拷贝
    }
    else
    {
        BitBlt(
            GetHDC(), x, y,
            isCut == true ? rctCut->right : bmp.bmWidth, isCut == true ? rctCut->bottom : bmp.bmHeight,
            img.hDC, isCut == true ? rctCut->left : 0, isCut == true ? rctCut->top : 0,
            SRCCOPY
        );// 原样拷贝,不支持拉伸
    }
}




返回首页


Copyright (C) 2018-2024 huidong