Recopie zone bitmap avec redimensionnement possible (stretchblt win32)

Description

Quelques fonctions permettant de créer un bitmap qui soit la copie d'une zone d'un autre bitmap avec un redimensionnement possible. En gros montre comment utiliser StretchBlt pour créer un nouveau bitmap.

Source / Exemple :


//*****************************************************************************
// DimBmp.cpp :
//
//*****************************************************************************

#include <windows.h>

#include "Resource.h"

//=============================================================================
// Variables globales.
//=============================================================================
HINSTANCE	g_hAppInstance	= NULL;	// instance de l'application
HWND		g_hWndMainFrame	= NULL;	// fenêtre principale

HBITMAP		g_hBmpSrc		= NULL;	// bitmap source
HBITMAP		g_hBmpDst		= NULL;	// bitmap destination

//=============================================================================
// Fonctions du module.
//=============================================================================
BOOL	InitInstance(HINSTANCE hInstance, int nCmdShow);
SIZE	GetBmpSize	(HBITMAP hBmp);
HBITMAP	RedimBmp	(HDC hdc, HBITMAP hBmpScr, RECT rcSrc, SIZE sizeDst);
void	DrawBmp		(HDC hdc, int x, int y, HBITMAP hBmp);

LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

//*****************************************************************************
// WinMain : point d'entrée du programme.
//*****************************************************************************
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
				   LPSTR lpCmdLine, int nCmdShow)
{
	// initialisation de l'application
	if(!InitInstance(hInstance, nCmdShow))
		return 0;

	// boucle de messages
	MSG msg;
	while(GetMessage(&msg, NULL, 0, 0))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
	return msg.wParam;
}

//*****************************************************************************
// InitInstance : initialisation de l'application, création de la fenêtre
//                principale.
//*****************************************************************************
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
	// sauvegarde de l'instance de l'application
	g_hAppInstance = hInstance;
	char szWndClass[] = "DimBmpWnd";
	char szWndTitle[] = "DimBmp";

	// classe de fenêtre
	WNDCLASS wc;
	ZeroMemory(&wc, sizeof(WNDCLASS));
	wc.hIcon			= LoadIcon(NULL, IDI_APPLICATION);
	wc.hCursor			= LoadCursor(NULL, IDC_ARROW);
	wc.hInstance		= g_hAppInstance;
	wc.lpfnWndProc		= WndProc;
	wc.style			= CS_HREDRAW|CS_VREDRAW;
	wc.lpszClassName	= szWndClass;
	wc.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
	if(!RegisterClass(&wc))
		return FALSE;

	// création de la fenêtre
	g_hWndMainFrame = CreateWindow(szWndClass, szWndTitle, WS_OVERLAPPEDWINDOW,
				CW_USEDEFAULT, 0, CW_USEDEFAULT, 0,
				NULL, NULL, g_hAppInstance, NULL);
	if(g_hWndMainFrame == NULL)
		return FALSE;

	// affichage
	ShowWindow(g_hWndMainFrame, nCmdShow);
	UpdateWindow(g_hWndMainFrame);
	return TRUE;
}

//*****************************************************************************
// GetBmpSize : récupère la taille d'un bitmap.
// entrée : hBmp : bitmap dont on souhaite récupérer la taille.
// retour : taille du bitmap spécifié.
//*****************************************************************************
SIZE GetBmpSize(HBITMAP hBmp)
{
	// récupération des informations sur le bitmap
	BITMAP bmpInfo;
	GetObject(hBmp, sizeof(bmpInfo), &bmpInfo);

	// taille
	SIZE size;
	size.cx = bmpInfo.bmWidth;
	size.cy = bmpInfo.bmHeight;
	return size;
}

//*****************************************************************************
// RedimBmp : crée un bitmap en recopiant une zone d'un autre bitmap avec un
//            redimensionnement éventuel.
// entrée : hdc     : DC à utiliser pour créer les DCs compatibles (DC de la
//                    fenêtre principale ou de l'écran par exemple).
//          hBmpSrc : bitmap source.
//          rcSrc   : zone du bitmap source à recopier ou {0,0,0,0} pour
//                    recopier l'intégralité.
//          sizeDst : taille du bitmap de destination ou {0,0} pour calculer
//                    la taille à partir de rcSrc (pas de redimensionnement).
// retour : bitmap créé.
//*****************************************************************************
HBITMAP RedimBmp(HDC hdc, HBITMAP hBmpSrc, RECT rcSrc, SIZE sizeDst)
{
	// taille du bitmap initial
	SIZE sizeSrc = GetBmpSize(hBmpSrc);

	// si rcSrc est à {0,0,0,0}, on prend toute l'image source
	if(rcSrc.left==0 && rcSrc.top==0 && rcSrc.right==0 && rcSrc.bottom==0)
	{
		rcSrc.right		= sizeSrc.cx;
		rcSrc.bottom	= sizeSrc.cy;
	}
	// vérification des bornes
	if(rcSrc.left < 0)
		rcSrc.left = 0;
	if(rcSrc.top < 0)
		rcSrc.top = 0;
	if(rcSrc.right > sizeSrc.cx)
		rcSrc.right = sizeSrc.cx;
	if(rcSrc.bottom > sizeSrc.cy)
		rcSrc.bottom = sizeSrc.cy;

	// si sizeDst est à {0,0}, on ne fait pas de redimensionnement
	if(sizeDst.cx==0 && sizeDst.cy==0)
	{
		sizeDst.cx = rcSrc.right-rcSrc.left;
		sizeDst.cy = rcSrc.bottom-rcSrc.top;
	}

	//création de DCs compatibles et du bitmap de destination
	HDC hDCSrc = CreateCompatibleDC(hdc);
	HDC hDCDst = CreateCompatibleDC(hdc);
	HBITMAP hBmpDst = CreateCompatibleBitmap(hdc, sizeDst.cx, sizeDst.cy);

	// sélection des bitmaps dans les DCs (avec sauvegarde des anciens)
	HBITMAP hOldBmpSrc = (HBITMAP)SelectObject(hDCSrc, hBmpSrc);
	HBITMAP hOldBmpDst = (HBITMAP)SelectObject(hDCDst, hBmpDst);

	// recopie
	SetStretchBltMode(hDCDst, HALFTONE);
	StretchBlt(hDCDst, 0, 0, sizeDst.cx, sizeDst.cy, hDCSrc,
			rcSrc.left, rcSrc.top, 
			rcSrc.right-rcSrc.left, rcSrc.bottom-rcSrc.top, SRCCOPY);

	// sélection des anciens bitmaps dans les DCS
	SelectObject(hDCSrc, hOldBmpSrc);
	SelectObject(hDCDst, hOldBmpDst);

	// destruction des DCs
	DeleteDC(hDCSrc);
	DeleteDC(hDCDst);

	// retour
	return hBmpDst;
}

//*****************************************************************************
// DrawBmp : dessine un bitmap dans un DC.
// entrée : hdc : DC où dessiner le bitmap.
//          x    : coordonnée en x où dessiner le bitmap.
//          y    : coordonnée en y où dessiner le bitmap.
//          hBmp : bitmap à dessiner.
//*****************************************************************************
void DrawBmp(HDC hdc, int x, int y, HBITMAP hBmp)
{
	// création d'un DC compatible pour y sélectionner le bitmap à dessiner
	HDC hDCBmp = CreateCompatibleDC(hdc);
	HBITMAP hOldBmp = (HBITMAP)SelectObject(hDCBmp, hBmp);

	// recopie
	SIZE sizeBmp = GetBmpSize(hBmp);
	BitBlt(hdc, x, y, sizeBmp.cx, sizeBmp.cy, hDCBmp, 0, 0, SRCCOPY);

	// sélection ancien bitmap et destruction du DC
	SelectObject(hDCBmp, hOldBmp);
	DeleteDC(hDCBmp);
}

//*****************************************************************************
// WndProc : procédure de traitement des messages de la fenêtre principale.
//*****************************************************************************
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	// en fonction du message
	switch(uMsg)
	{
	case WM_CREATE :
		{
			// chargement du bitmap, récupération d'une zone avec
			// redimensionnement
			g_hBmpSrc = LoadBitmap(g_hAppInstance, MAKEINTRESOURCE(IDB_SPRITE));
			RECT rcSrc = {30, 0, 70, 40};
			SIZE sizeDst = {80, 80};
			HDC hdc = GetDC(hWnd);
			g_hBmpDst = RedimBmp(hdc, g_hBmpSrc, rcSrc, sizeDst);
			ReleaseDC(hWnd, hdc);
			return 0;
		}
	case WM_DESTROY :
		{
			// destruction des bitmaps, fin de l'application
			DeleteObject(g_hBmpSrc);
			DeleteObject(g_hBmpDst);
			PostQuitMessage(0);
			return 0;
		}
	case WM_PAINT :
		{
			// début du dessin
			PAINTSTRUCT ps;
			HDC hdc = BeginPaint(hWnd, &ps);

			// dessin des bitmaps
			DrawBmp(hdc, 0, 0, g_hBmpSrc);
			DrawBmp(hdc, 200, 0, g_hBmpDst);

			// fin du dessin
			EndPaint(hWnd, &ps);
			return 0;
		}
	}

	// traitement par défaut
	return DefWindowProc(hWnd, uMsg, wParam, lParam);
}

Codes Sources

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.