Demande d'explications pour GetDIBits...

DJBACON_17 Messages postés 15 Date d'inscription dimanche 23 mai 2004 Statut Membre Dernière intervention 24 juin 2004 - 27 mai 2004 à 16:46
DJBACON_17 Messages postés 15 Date d'inscription dimanche 23 mai 2004 Statut Membre Dernière intervention 24 juin 2004 - 28 mai 2004 à 08:36
Est-ce quelqu'un pourrait m'expliquer comment , sous VC++6, je peux recuperer bits a bit le contenu du buffer que GetDIBits remplit svp?
Merci d'avance.

2 réponses

ymca2003 Messages postés 2070 Date d'inscription mardi 22 avril 2003 Statut Membre Dernière intervention 3 juillet 2006 7
27 mai 2004 à 22:30
voici comment récupérer les pixels du bitmap (on le récupère en 32 bits/pixel pour faire simple)

//************************************************// TestGetDIBits :
//
//************************************************
#include <windows.h>

#include "Resource.h"

//************************************************// WinMain :
//************************************************int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine,   int nCmdShow)
{
// chargement bitmap
HBITMAP hBitmap = LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_SPRITE));

// tout d'abord on récupère des informations sur la taille du bitmap
BITMAP bmpInfo;
GetObject(hBitmap, sizeof(BITMAP), &bmpInfo);

// allocation mémoire. On va récupérer le bitmap en 32 bits/pixel pour simplifier
// comme cela, on sera sur que chaque ligne du bitmap sera codé sur un nombre
// d'octets multiple de 4. Si le bitmap n'est pas bottom-up (cas le plus fréquent)
// mais top-down, bmpInfo.bmHeight est négatif.
LPBYTE lpBits = new BYTE[4*bmpInfo.bmWidth*bmpInfo.bmHeight];

// initialisation structure BITMAPINFO
BITMAPINFO bi;
memset(&bi, 0, sizeof(BITMAPINFO));
bi.bmiHeader.biSize= sizeof(BITMAPINFOHEADER);
bi.bmiHeader.biWidth= bmpInfo.bmWidth;
bi.bmiHeader.biHeight= bmpInfo.bmHeight;
bi.bmiHeader.biBitCount= 32;
bi.bmiHeader.biPlanes	= 1;
bi.bmiHeader.biCompression= BI_RGB;

// récupération pixels
HDC hdc = GetDC(NULL);
GetDIBits(hdc, hBitmap, 0, bmpInfo.bmHeight, lpBits, &bi, DIB_RGB_COLORS);
ReleaseDC(NULL, hdc);

// création d'un fichier
char szFileName[MAX_PATH];
GetModuleFileName(NULL, szFileName, MAX_PATH);
char* pEnd = strrchr(szFileName, '\\');
strcpy(pEnd, "\\Dump.txt");
HANDLE hFile = CreateFile(szFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);

// écriture des pixels à partir du haut du bitmap
char szColor[16];
DWORD dwNbBytesWritten;
for(int y = 0; y < bmpInfo.bmHeight; y++)
{
LPBYTE lpLine = lpBits+(bmpInfo.bmHeight-y-1)*4*bmpInfo.bmWidth;
for(int x = 0; x < bmpInfo.bmWidth; x++)
{
COLORREF color = *((COLORREF*)(lpLine+4*x));
wsprintf(szColor, "%08X ", color);
WriteFile(hFile, szColor, strlen(szColor), &dwNbBytesWritten, NULL);
}
WriteFile(hFile, "\r\n", 2, &dwNbBytesWritten, NULL);
}

// fermeture fichier, libération mémoire
CloseHandle(hFile);
delete[] lpBits;
return 0;
}

0
DJBACON_17 Messages postés 15 Date d'inscription dimanche 23 mai 2004 Statut Membre Dernière intervention 24 juin 2004
28 mai 2004 à 08:36
Merci ymca2003 pour ton aide.
0
Rejoignez-nous