在wtl中显示jpg,png

  • Post author:
  • Post category:其他


以下方法仅在wtl中测试过

data,len就是要显示的jpg或者png的数据和长度,可以直接从内存加载

void AccountDlg::ShowJpgGif(CImage& img,char* data,int len, int x, int y)
{
	IStream *pStm; 
	HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, len); 
	if (hGlobal != NULL) 
	{ 
		LPVOID pvData = GlobalLock(hGlobal);
		if (pvData != NULL) 
		{ 
			memcpy(pvData,data,len);
			GlobalUnlock(hGlobal); 
			CreateStreamOnHGlobal(hGlobal, TRUE, &pStm); 
			img.Load(pStm);
		}
	}
}

使用方法:

在对话框上放置一个Picture Control,把type设置成Bitmap即可

CImage img;
ShowJpgGif(img,pdata,len,0,0);
HBITMAP h = img.Detach();
CStatic stc = GetDlgItem(IDC_STC_VERIFY);
stc.SetBitmap(h);

如果想直接加载文件可以用下面这个,直接指定文件的路径即可

void ShowJpgGif(CImage& img,char* strPath, int x, int y)
{
	IStream *pStm; 
	int len = 0;
	FILE* f = fopen(strPath,"rb");
	if (f != NULL)
	{ 
		fseek(f,0,SEEK_END);
		len = ftell(f);
		rewind(f);

		HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, len); 
		LPVOID pvData = NULL; 
		if (hGlobal != NULL) 
		{ 
			pvData = GlobalLock(hGlobal);
			if (pvData != NULL) 
			{ 
				//file.Read(pvData, cb); 
				fread(pvData,1,len,f);
				fclose(f);
				GlobalUnlock(hGlobal); 
				CreateStreamOnHGlobal(hGlobal, TRUE, &pStm); 
				img.Load(pStm);
			}
		}
	}
}

使用方法:

CImage img;
ShowJpgGif(img,"aa.png",0,0);
m_hBitmap = img.Detach();
m_imgStc = GetDlgItem(IDC_STC_IMAGE);
m_imgStc.SetBitmap(m_hBitmap);



版权声明:本文为varding原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。