LoadImage只能加载bmp,ico,cur文件,但是对于jpg,gif等文件则无能为力,下面就是介绍用VC怎么显示jpg,gif,bmp文件
1:LoadImage
HBITMAP hBitmap=(HBITMAP)::LoadImage(AfxGetInstanceHandle(),"128.bmp",IMAGE_BITMAP,0,0,LR_LOADFROMFILE|LR_DEFAULTCOLOR|LR_DEFAULTSIZE);
2:GdiPlus
A. 在stdafx.h中或者cpp文件的头部引入下面两条语句
#include <gdiplus.h>
using namespace Gdiplus;
B. 在项目属性-》连接器-》输入-》附加依赖项中加入gdiplus.lib
C. 初始化gdiplus(一般在InitInstance或者OnInitDialog中加入)
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
D.实现在一个Static(IDC_STATIC1)中显示,在其它地方也可以只要具有Window
RECT rect;
HDC hdc;
PAINTSTRUCT ps;
GetDlgItem(IDC_STATIC1)->GetClientRect(&rect);
hdc = GetDlgItem(IDC_STATIC1)->GetWindowDC()->m_hDC;
Graphics graphics(hdc);
//由于Gdiplus::Image img()语句的参数必须是wchar_t *的形式,下面就是将文件名strFileName从char *转换为wchar_t *
int wcsLen;
wchar_t* wszFileName;
wcsLen = ::MultiByteToWideChar(CP_ACP, NULL, strFileName, strlen(strFileName), NULL, 0);
wszFileName = new wchar_t[wcsLen + 1];
::MultiByteToWideChar(CP_ACP, NULL, strFileName, strlen(strFileName), wszFileName, wcsLen);
wszFileName[wcsLen] = '/0';
Gdiplus::Image img(wszFileName);
graphics.DrawImage(&img, 0, 0, rect.right - rect.left, rect.bottom - rect.top);
::ReleaseDC(GetDlgItem(IDC_STATIC1)->m_hWnd, hdc);
delete wszFileName;