您的位置:首页 > 其它

怎样显示Jpg,bmp,Gif图像文件

2011-06-09 14:01 483 查看
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;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: