利用GDI+加载图片

#include <comdef.h>

#ifndef ULONG_PTR

#define ULONG_PTR unsigned long*

#include "GdiPlus.h"

using namespace Gdiplus;

#endif

#pragma comment(lib,"gdiplus.lib")



HBITMAP CreateBitmapFromImage( LPCTSTR pFileName, DWORD* pWidth, DWORD* pHeight, void** pData )

{

	HDC hMemDC;

	HBITMAP hBitmap;

	GdiplusStartupInput m_gdiplusStartupInput;

	ULONG_PTR m_pGdiToken;



	GdiplusStartup( &m_pGdiToken, &m_gdiplusStartupInput, NULL );

	{

		hMemDC = CreateCompatibleDC( NULL );



		Image image( pFileName, FALSE );

		*pWidth = image.GetWidth();

		*pHeight = image.GetHeight();



		BITMAPINFO bi;		

		ZeroMemory( &bi, sizeof(BITMAPINFO) );



		bi.bmiHeader.biSize		= sizeof(BITMAPINFOHEADER);

		bi.bmiHeader.biBitCount		= 32;

		bi.bmiHeader.biWidth		= *pWidth;

		bi.bmiHeader.biHeight		= *pHeight;

		bi.bmiHeader.biCompression	= BI_RGB;

		bi.bmiHeader.biPlanes		= 1;



		hBitmap = CreateDIBSection( hMemDC, &bi, DIB_RGB_COLORS,

			pData, 0, 0 );

		SelectObject( hMemDC, hBitmap );

		RECT rect = { 0, 0, *pWidth, *pHeight };

		FillRect( hMemDC, &rect, (HBRUSH)GetStockObject(WHITE_BRUSH) );

		Graphics graphics( hMemDC );

		graphics.DrawImage( &image, 0, 0, *pWidth, *pHeight );

	}

	DeleteDC( hMemDC );

	GdiplusShutdown( m_pGdiToken );

	return hBitmap;

}

你可能感兴趣的:(图片)