因要做一个小的登陆客户端,所用图片格式为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![](/Images/OutliningIndicators/None.gif)
8
hSource = FindResource(AfxGetResourceHandle(), MAKEINTRESOURCE(IDR_BACK_PNG), "PNG");
9![](/Images/OutliningIndicators/None.gif)
10
if(hSource == NULL)
11
return pRetImg;
12![](/Images/OutliningIndicators/None.gif)
13
hGlobal = LoadResource(AfxGetResourceHandle(), hSource);
14
if(hGlobal == NULL)
15![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
16
FreeResource(hGlobal); return pRetImg;
17
}
18![](/Images/OutliningIndicators/None.gif)
19
lpVoid = LockResource(hGlobal);
20
if(lpVoid == NULL)
21
goto Exit;
22![](/Images/OutliningIndicators/None.gif)
23
nSize = (UINT)SizeofResource(AfxGetResourceHandle(), hSource);
24![](/Images/OutliningIndicators/None.gif)
25
HGLOBAL hGlobal2 = GlobalAlloc(GMEM_MOVEABLE, nSize);
26
if(hGlobal2 == NULL)
27![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
28
goto Exit;
29
}
30![](/Images/OutliningIndicators/None.gif)
31
void* pData = GlobalLock(hGlobal2);
32
memcpy(pData, (void *)hGlobal, nSize);
33
GlobalUnlock(hGlobal2);
34![](/Images/OutliningIndicators/None.gif)
35
IStream* pStream = NULL;
36![](/Images/OutliningIndicators/None.gif)
37
if(CreateStreamOnHGlobal(hGlobal2, TRUE, &pStream) == S_OK)
38![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
39![](/Images/OutliningIndicators/InBlock.gif)
40
pRetImg = Image::FromStream(pStream, FALSE);
41
pStream->Release();
42
}
43![](/Images/OutliningIndicators/None.gif)
44
FreeResource(hGlobal2);
45![](/Images/OutliningIndicators/None.gif)
46![](/Images/OutliningIndicators/None.gif)
47
Exit:
48
UnlockResource(hGlobal); FreeResource(hGlobal);
49
return pRetImg;
50![](/Images/OutliningIndicators/None.gif)
posted on 2008-05-29 20:37
我风 阅读(11585)
评论(2) 编辑 收藏 引用