vc資源中載入png圖片以及顯示
阿新 • • 發佈:2018-12-27
因要做一個小的登陸客戶端,所用圖片格式為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 我風 閱讀(10434) 評論(2) 編輯 收藏 引用
碰到的第一個問題是.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
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);
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 }
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 我風 閱讀(10434) 評論(2) 編輯 收藏 引用