因要做一个小的登陆客户端,所用图片格式为png.但较少,所以想直接放到资源中.
碰到的第一个问题是.net2003中加载png图片到资源中总是不成功.试了下发现jpg也不行,jpg图片会提示"不是正确的JPEG格式",后来用vc6.0打开资源文件加载成功的.vs2003中原因还未找到,有知道的朋友告知一声.
而后的问题就是如何把资源中的二进制的图片数据显示出来. gg之.整理代码如下:
1 Image *pRetImg = NULL;
2
3 HGLOBAL hGlobal = NULL;
4 HRSRC hSource = NULL;
5 LPVOID lpVoid = NULL;
6 int nSize = 0;
7
8 hSource = FindResource(AfxGetResourceHandle(), MAKEINTRESOURCE(IDR_BACK_PNG), "PNG");
9
10 if(hSource == NULL)
11 return pRetImg;
12
13 hGlobal = LoadResource(AfxGetResourceHandle(), hSource);
14 if(hGlobal == NULL)
15 {
16 FreeResource(hGlobal); return pRetImg;
17 }
18
19 lpVoid = LockResource(hGlobal);
20 if(lpVoid == NULL)
21 goto Exit;
22
23 nSize = (UINT)SizeofResource(AfxGetResourceHandle(), hSource);
24
25 HGLOBAL hGlobal2 = GlobalAlloc(GMEM_MOVEABLE, nSize);
26 if(hGlobal2 == NULL)
27 {
28 goto Exit;
29 }
30
31 void* pData = GlobalLock(hGlobal2);
32 memcpy(pData, (void *)hGlobal, nSize);
33 GlobalUnlock(hGlobal2);
34
35 IStream* pStream = NULL;
36
37 if(CreateStreamOnHGlobal(hGlobal2, TRUE, &pStream) == S_OK)
38 {
39
40 pRetImg = Image::FromStream(pStream, FALSE);
41 pStream->Release();
42 }
43
44 FreeResource(hGlobal2);
45
46
47Exit:
48 UnlockResource(hGlobal); FreeResource(hGlobal);
49 return pRetImg;
50
posted on 2008-05-29 20:37
我风 阅读(11626)
评论(2) 编辑 收藏 引用