Reputation: 2124
I'm trying to workout how to resize an image after I've converted it to a bitmap. From the MSDN
examples, I was able to figure out how to resize from a DC (before it's converted to a bitmap)
# Big snapshot of the desktop
hwnd = win32gui.GetDesktopWindow()
zhwndDevice = win32gui.GetWindowDC(hwnd)
zmfcDC = win32ui.CreateDCFromHandle(zhwndDevice)
zsaveDC = zmfcDC.CreateCompatibleDC()
zsaveBitMap = win32ui.CreateBitmap()
zsaveBitMap.CreateCompatibleBitmap(zmfcDC, width, height)
zsaveDC.SelectObject(zsaveBitMap)
zsaveDC.BitBlt((0, 0), (width, height), zmfcDC, (left, top), win32con.SRCCOPY)
# Creates a smaller bitmap and resizes the first image to fit it
hwnd = win32gui.GetDesktopWindow()
hwndDevice = win32gui.GetWindowDC(hwnd)
mfcDC = win32ui.CreateDCFromHandle(hwndDevice)
saveDC = mfcDC.CreateCompatibleDC()
saveBitMap = win32ui.CreateBitmap()
saveBitMap.CreateCompatibleBitmap(mfcDC, target_size[0], target_size[1])
saveDC.SelectObject(saveBitMap)
win32gui.StretchBlt(pywintypes.HANDLE(saveDC.GetHandleAttrib()), 0,0,target_size[0], target_size[1], zsaveDC.GetHandleAttrib(), 0,0,width, height, win32con.SRCCOPY)
My question is, if I already have a Bitmap object, how would I go about creating a DC from it so that I can pass it to the StretchBlt
method?
Upvotes: 0
Views: 629
Reputation: 366093
The whole point of a Device Context is that it's a context suitable for some kind of device—your monitor, a printer, whatever. So, creating a DC for a bitmap doesn't make any sense. Read Device Contexts for more detail.
If you want to create an in-memory device context that's compatible with the current screen, that's exactly what you get from calling CreateCompatibleDC
with NULL
(from Python, None
or 0
, depending on which wrapper you're using) as the hdc
parameter:
A handle to an existing DC. If this handle is NULL, the function creates a memory DC compatible with the application's current screen.
However, in your case, you're ultimately trying to blit this into a target window (or some other target) for which you already have a DC, right? So I think what you really want is to create a memory DC compatible with the target DC, then create a bitmap compatible with that target DC, then select that bitmap into the memory DC.
Upvotes: 3