• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
LinuxHunter
原创+技巧=无坚不摧
博客园    首页    新随笔    联系   管理    订阅  订阅
利用GDI+加载图片
利用GDI+显示JPG,PNG等类型图片
#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;
}
posted on 2010-11-23 20:15  LinuxHunter  阅读(3263)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3