Question

Je l'ai chargé un objet CBitmap d'un ID de ressource, et je suis maintenant vouloir mettre à l'échelle à 50% de sa taille dans chaque dimension. Comment pourrais-je aller à ce sujet?

Était-ce utile?

La solution

  1. Sélectionnez votre CBitmap obj dans un memDC A (en utilisant CDC :: SelectObject ())
  2. Créer une nouvelle CBitmap avec taille désirée et sélectionnez dans une autre MemDC B
  3. Utilisation CDC :: StretchBlt (...) pour étirer BMP dans MemDC A dans MemDC B
  4. Désélectionnez vos objets CBitmap (en sélectionnant ce qui a été renvoyé par les précédents appels à SelectObject)
  5. Utilisez votre nouveau CBitmap

Autres conseils

Voici une mise en œuvre élaboré de @ réponse de Smashery.

J'utilise cette à l'échelle en fonction de DPI, mais il devrait être facile à adapter à des échelles arbitraires.

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;
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top