• 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)*.jpg";
    	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

  • 相关阅读:
    探讨变量的内存分配方式
    色彩之RGB和灰阶
    Perl语言:qw简写
    【转】位操作
    [转]Perl学习笔记
    Spaghetti code&Ravioli code&Lasagna code&Spaghetti with meatballs
    交叉编译lsusb
    GCC,LLVM,Clang编译器对比
    如何判断自己是否到了该辞职的时候
    Javascript Array和String的互转换。
  • 原文地址:https://www.cnblogs.com/d3inc/p/3799900.html
Copyright © 2020-2023  润新知