C ++ WIN32:重新缩放位图/提供位图HDC [英] C++ WIN32: Rescaling Bitmaps/Giving Bitmaps HDC's

查看:145
本文介绍了C ++ WIN32:重新缩放位图/提供位图HDC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我一直试图在不打印原始图并重新打印重新缩放的图像的情况下重新调整位图的大小.我正在尝试使用基于MSDN Microsoft重缩放图像功能的StretchBlt():

So i've been trying to rescale a bitmap without it printing the original and reprinting the rescaled image. I'm trying to use StretchBlt(), based on the MSDN Microsoft rescaling images function:

https://msdn.microsoft.com/en-us/library/windows/desktop/dd162950(v = vs.85).aspx

但这需要与Source绑定的辅助hdc,如果不先打印HBITMAP,则无法进行拉伸.有没有办法将HBITMAP转换为HDC?我已经能够将HANDLE排除在HBITMAP之外,这可能会提供更直接的路线.我可以做的另一件事是在标准位图之外的已分配内存(未保存)中创建一个调整大小的位图,然后打印出来.

but that requires a secondary hdc tied to the Source, which stretching can't be done without printing the HBITMAP first. Is there a way to convert HBITMAP's into HDC's? I have been able to get HANDLE's out of HBITMAP's, which might provide a more direct route. The other thing i could do is create a resized bitmap in allocated memory (not saved) out of the standard bitmap and print that.

我打印位图的标准方法是:

The standard way i print bitmaps is:

HBITMAP hBitmap;
static HANDLE hDIB = NULL;
CHAR szFileName[MAX_PATH] = "fileName.bmp";

hDIB = OpenDIB((LPSTR)szFileName);

hBitmap = BitmapFromDIB(hDIB, NULL);

DrawBitmap(hdc, x, y, hBitmap, SRCCOPY);

我可以尝试的另一种选择是研究显示bmp的另一种方法.我对win32还是很陌生,所以我不知道其他任何方法可以完成此任务.关于如何无需首先打印即可重新缩放BITMAP的任何见解.

Another option i could try is to look into another means of displaying the bmp. I'm pretty new to win32, so I don't know any other means of accomplishing this task. Any insight into how i can rescale the BITMAP without printing it in the first place.

推荐答案

您发布的链接( BitBlt 的调用替换为 StretchBlt :

The link you posted (Scaling an Image) already contains code, that renders a bitmap. All you need to do is replace the call to BitBlt with StretchBlt:

BOOL DrawBitmap (HDC hDC, INT x, INT y, INT width, INT height, HBITMAP hBitmap, DWORD dwROP)
{
    HDC       hDCBits;
    BITMAP    Bitmap;
    BOOL      bResult;

    if (!hDC || !hBitmap)
        return FALSE;

    hDCBits = CreateCompatibleDC(hDC);
    GetObject(hBitmap, sizeof(BITMAP), (LPSTR)&Bitmap);
    SelectObject(hDCBits, hBitmap);
    // Replace with StretchBlt call
    //bResult = BitBlt(hDC, x, y, Bitmap.bmWidth, Bitmap.bmHeight, hDCBits, 0, 0, dwROP);
    bResult = StretchBlt(hDC, x, y, width, height,
                         hDCBits, 0, 0, Bitmap.bmWidth, Bitmap.bmHeight, dwROP);
    DeleteDC(hDCBits);

    return bResult;
}

例如,您可以从 WM_PAINT 消息处理程序中调用此方法:

You can call this from your WM_PAINT message handler, for example:

case WM_PAINT:
{
    PAINTSTRUCT ps = { 0 };
    HDC hDC = ::BeginPaint( hWnd, &ps );
    RECT rc = { 0 };
    ::GetClientRect( hWnd, &rc );
    DrawBitmap( hDC, 0, 0, rc.right, rc.bottom, hBitmap, SRCCOPY );
    ::EndPaint( hWnd, &ps );
}
break;

这篇关于C ++ WIN32:重新缩放位图/提供位图HDC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆