首页 > 代码库 > Win32中GDI+应用(四)--- 位图的打开与显示

Win32中GDI+应用(四)--- 位图的打开与显示

显示位图,你应该使用GDI+里面的Bitmap类或者Image类,这两个类都提供了方法从硬盘上的一个文件打开文件,创建相应的内存中的位图对象的工作。然后你可以使用Graphics类的DrawImage方法来绘制该位图。
下面的代码初始化GDI+,显示一个打开文件对话框并且创建Bitmap对象,显示位图:

	GdiplusStartupInput input;	ULONG_PTR gdiPlusToken;	if(GdiplusStartup(&gdiPlusToken,&input,NULL)!=Ok)	{		return -1;	}	char fileName[200];	OPENFILENAME openStruct;	memset(&openStruct,0,sizeof(OPENFILENAME));	openStruct.lStructSize=sizeof(OPENFILENAME);	openStruct.Flags=OFN_EXPLORER;	openStruct.lpstrFilter="Jpeg files(*.jpg)\0*.jpg\0\0";	openStruct.lpstrFile=fileName;	openStruct.nMaxFile=200;	if(GetOpenFileName(&openStruct))	{		imageFileName=fileName;	}	HDC hdc=GetDC(hPicWnd);	if(hdc==NULL)	{		MessageBox(NULL,"","",MB_OK);	}	Graphics *g=new Graphics(hdc);	WCHAR tmpFileName[100];	size_t numOfChars;	if(/*mbstowcs_s(&numOfChars,tmpFileName,100,fileName,199)*/MultiByteToWideChar(CP_ACP,MB_COMPOSITE,imageFileName.c_str(),200,tmpFileName,100)/*==-1*/)	{		MessageBox(NULL,"Error occured in string convertion!","Error!",MB_OK);       		return wParam;	}	HWND hPicWnd;		RECT rc;	GetWindowRect(hPicWnd,&rc);	RectF drawRect(rc.left,rc.top,rc.right-rc.left,rc.bottom-rc.top);	Bitmap *bmp=new Bitmap(tmpFileName);	bmpMain=bmp;	g->DrawImage(bmp,drawRect);	delete g;	ReleaseDC(hwnd,hdc);	GdiplusShutdown(gdiPlusToken);

转载地址:http://www.cppblog.com/dingding/archive/2008/06/27/54796.html