Question

Je veux créer une fonction de la culture dans un moteur existant. Voilà ce que je l'ai déjà:

bool Bitmap::Crop(RECT cropArea)
{
BITMAP bm;
GetObject(m_Handle, sizeof(bm), &bm);

HDC hSrc = CreateCompatibleDC(NULL);
SelectObject(hSrc, m_Handle);

HDC hNew = CreateCompatibleDC(NULL);
HBITMAP hBmp = CreateCompatibleBitmap(hNew, bm.bmWidth, bm.bmHeight);
HBITMAP hOld = (HBITMAP)SelectObject(hNew, hBmp);

BitBlt(hNew, 0, 0, bm.bmWidth, bm.bmHeight, hSrc, 0, 0, SRCCOPY);

SelectObject(hNew, hOld);

DeleteDC(hSrc);
DeleteDC(hNew);

DeleteObject(m_Handle);

m_Handle = hBmp;
}

Je veux qu'il suffit de copier l'image entière à un nouveau HBITMAP et remplacer l'ancien avec elle. Donc, je sais que cela fonctionne. Après cela, il est juste de jouer avec les paramètres de BitBlt.

m_Handle est un HBITMAP de la classe Bitmap.

Le résultat de ce code est juste un écran noir.

Était-ce utile?

La solution

Ne jamais créer un bitmap compatible à partir d'une mémoire « frais » DC. À moins que que vous voulez créer un bitmap 1bpp - le bitmap par défaut sélectionnée dans une nouvelle mémoire DC est un bitmap 1x1 1bpp - de sorte que toute bitmap compatible vous créez correspondra que. Ce qui tend à entraîner toutes les sorties noir.

Votre bitmap de couleur dans la CRSS, donc utiliser que dc faire le nouveau bitmap.

Autres conseils

Merci pour votre aide. La fonction fonctionne parfaitement maintenant.

bool Bitmap::Crop(RECT cropArea)
{
HDC hSrc = CreateCompatibleDC(NULL);
SelectObject(hSrc, m_Handle);

HDC hNew = CreateCompatibleDC(hSrc);
HBITMAP hBmp = CreateCompatibleBitmap(hSrc, cropArea.right - cropArea.left, cropArea.bottom - cropArea.top); 
HBITMAP hOld = (HBITMAP)SelectObject(hNew, hBmp);

bool retVal = (BitBlt(hNew, 0, 0, cropArea.right - cropArea.left, cropArea.bottom - cropArea.top, hSrc, cropArea.left, cropArea.top, SRCCOPY))?true:false;

SelectObject(hNew, hOld);

DeleteDC(hSrc);
DeleteDC(hNew);

DeleteObject(m_Handle);

m_Handle = hBmp;

return retVal;
}

Deux petits changements:

HBITMAP hBmp = CreateCompatibleBitmap(hNew, cropArea.right - cropArea.left, cropArea.bottom - cropArea.top); 

BitBlt(hNew, 0, 0, cropArea.right - cropArea.left, cropArea.bottom - cropArea.top, hSrc, cropArea.left, cropArea.top, SRCCOPY); 

Vous voudrez peut-être un peu plus de vérifier pour assurer que la zone demandée relève de la taille du bitmap d'origine.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top