I've loaded a CBitmap object from a resource ID, and I'm now wanting to scale it to 50% its size in each dimension. How might I go about this?
❓ How can I resize a BMP image? First, you need to add a BMP image file: drag & drop your BMP image file or click inside the white area to choose a file. Then adjust resize settings, and click the "Resize" button. After the process completes, you can download your result file.
You can resize your bitmap via the options by specifying the desired width and height (in pixels) of the new bitmap or drag the bitmap by its corners and adjust its size interactively. You can also preserve the aspect ratio of bitmap sides and change the dimensions proportionally.
Bitmap images are organised as a grid of coloured squares called pixels (short for 'picture elements'). When zooming in or enlarging a bitmap image, the pixels are stretched and made into larger blocks. This is why bitmap images appear as poor quality when enlarged too much.
Here's a worked out implementation of @Smashery's answer.
I use this to scale based on DPI, but it should be easy to adapt to arbitrary scales.
std::shared_ptr<CBitmap> AppHiDpiScaleBitmap (CBitmap &bmp)
{
BITMAP bm = { 0 };
bmp.GetBitmap (&bm);
auto size = CSize (bm.bmWidth, bm.bmHeight);
CWindowDC screenCDC (NULL);
auto dpiX = screenCDC.GetDeviceCaps (LOGPIXELSX);
auto dpiY = screenCDC.GetDeviceCaps (LOGPIXELSY);
auto hiSize = CSize ((dpiX * size.cx) / 96, (dpiY * size.cy) / 96);
std::shared_ptr<CBitmap> res (new CBitmap ());
res->CreateCompatibleBitmap (&screenCDC, hiSize.cx, hiSize.cy);
CDC srcCompatCDC;
srcCompatCDC.CreateCompatibleDC (&screenCDC);
CDC destCompatCDC;
destCompatCDC.CreateCompatibleDC (&screenCDC);
CMemDC srcDC (srcCompatCDC, CRect (CPoint (), size));
auto oldSrcBmp = srcDC.GetDC ().SelectObject (&bmp);
CMemDC destDC (destCompatCDC, CRect(CPoint(), hiSize));
auto oldDestBmp = destDC.GetDC ().SelectObject (res.get());
destDC.GetDC ().StretchBlt (0, 0, hiSize.cx, hiSize.cy, &srcDC.GetDC(), 0, 0, size.cx, size.cy, SRCCOPY);
srcDC.GetDC ().SelectObject (oldSrcBmp);
destDC.GetDC ().SelectObject (oldDestBmp);
return res;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With