Domanda

Voglio creare una funzione di ritaglio in un motore esistente. Questo è quello che ho già:

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;
}

voglio che basta copiare l'intera immagine in un nuovo HBITMAP e sostituire il vecchio con esso. Quindi so che funziona. Dopo di che si sta solo giocando con i parametri BitBlt.

m_Handle è un HBITMAP della bitmap di classe.

Il risultato di questo codice è solo uno schermo nero.

È stato utile?

Soluzione

Non creare una bitmap compatibile da una memoria 'fresco' DC. A meno che è che si desidera creare un 1bpp bitmap - bitmap di default selezionato in una nuova memoria DC è una bitmap 1x1 1bpp - in modo che qualsiasi bitmap compatibile si crea corrisponderà questo. Che non tende a tradursi in tutte le uscite nero.

Il bitmap di colore in in HSRC, quindi l'uso che dc per rendere il nuovo bitmap.

Altri suggerimenti

Grazie per avermi aiutato. La funzione funziona perfettamente ora.

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;
}

Due piccole modifiche:

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); 

Si potrebbe desiderare un po 'più di controllo per assicurarsi che l'area richiesta rientra nella dimensione del bitmap originale.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top