我已经从一个资源ID加载了一个CBitmap对象,现在我想在每个维度上将其缩放到其大小的50%。我该怎么做呢?
发布于 2010-05-05 13:42:32
发布于 2016-06-24 04:08:36
这是@Smashery答案的一个精心设计的实现。
我使用它来基于DPI进行缩放,但它应该很容易适应任意的缩放。
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;
}发布于 2020-04-16 20:07:01
void ResizeBitmap (CBitmap &bmp_src, CBitmap& bmp_dst, int dstW, int dstH)
{
BITMAP bm = { 0 };
bmp_src.GetBitmap (&bm);
auto size = CSize (bm.bmWidth, bm.bmHeight);
CWindowDC wndDC(NULL);
CDC srcDC;
srcDC.CreateCompatibleDC(&wndDC);
auto oldSrcBmp = srcDC.SelectObject(&bmp_src);
CDC destDC;
destDC.CreateCompatibleDC(&wndDC);
bmp_dst.CreateCompatibleBitmap (&wndDC, dstW, dstH);
auto oldDestBmp = destDC.SelectObject (&bmp_dst);
destDC.StretchBlt(0, 0, dstW, dstH, &srcDC, 0, 0, size.cx, size.cy, SRCCOPY);
}https://stackoverflow.com/questions/2770855
复制相似问题