用Com组件IImagingFactory画图
用api只能画位图,那么如果是其他格式的图片就要进行解码。
IImagingFactory则实现了这一功能。
因为写的太久了,所以没什么感受了,那么废话不多说,就把代码粘过来吧
1 #include <initguid.h>
2 #include <imgguids.h>
3 #include <imaging.h>
4 MyWnd::MyDraw()
5 {
6
7 IImagingFactory* pImgFac;
8 IImage* m_pImage;
9 ImageInfo m_pImageInfo;
10 if(FAILED(CoCreateInstance( CLSID_ImagingFactory, NULL, CLSCTX_INPROC_SERVER, IID_IImagingFactory, (void**)&pImgFac)))
11 return TRUE;
12
13 HRSRC hRes=FindResource(m_hWnd,
14 MAKEINTRESOURCE(IDR_JPG_BOARD),
15 RT_RCDATA); //IDR_JPG_BOARD资源号
16
17 if (hRes==NULL)
18 return FALSE;
19 HGLOBAL hGlobal=LoadResource(m_hWnd,hRes);
20 DWORD size=SizeofResource(m_hWnd,hRes);
21 LPVOID pData=LockResource(hGlobal);
22
23 if (pImgFac == NULL || pData == NULL || size == 0)
24 return NULL;
25 if (SUCCEEDED(pImgFac->CreateImageFromBuffer(pData, size, BufferDisposalFlagGlobalFree , &m_pImage)))
26 {
27 m_pImage->GetImageInfo(&m_pImageInfo);
28 HDC hdc = ::GetDC(NULL);
29 memHdc = ::CreateCompatibleDC(hdc);
30 hImg = CreateCompatibleBitmap(hdc,m_pImageInfo.Width,m_pImageInfo.Height);
31 SelectObject(memHdc,hImg);
32
33 rsWeight=m_pImageInfo.Width;
34 rsHight=m_pImageInfo.Height;
35 RECT rect = { 0, 0, m_pImageInfo.Width,m_pImageInfo.Height};
36 m_pImage->Draw(memHdc,&rect,NULL);
37 }
38 else
39 {
40 return NULL;
41 }
42 return TRUE;
43
44 }
2 #include <imgguids.h>
3 #include <imaging.h>
4 MyWnd::MyDraw()
5 {
6
7 IImagingFactory* pImgFac;
8 IImage* m_pImage;
9 ImageInfo m_pImageInfo;
10 if(FAILED(CoCreateInstance( CLSID_ImagingFactory, NULL, CLSCTX_INPROC_SERVER, IID_IImagingFactory, (void**)&pImgFac)))
11 return TRUE;
12
13 HRSRC hRes=FindResource(m_hWnd,
14 MAKEINTRESOURCE(IDR_JPG_BOARD),
15 RT_RCDATA); //IDR_JPG_BOARD资源号
16
17 if (hRes==NULL)
18 return FALSE;
19 HGLOBAL hGlobal=LoadResource(m_hWnd,hRes);
20 DWORD size=SizeofResource(m_hWnd,hRes);
21 LPVOID pData=LockResource(hGlobal);
22
23 if (pImgFac == NULL || pData == NULL || size == 0)
24 return NULL;
25 if (SUCCEEDED(pImgFac->CreateImageFromBuffer(pData, size, BufferDisposalFlagGlobalFree , &m_pImage)))
26 {
27 m_pImage->GetImageInfo(&m_pImageInfo);
28 HDC hdc = ::GetDC(NULL);
29 memHdc = ::CreateCompatibleDC(hdc);
30 hImg = CreateCompatibleBitmap(hdc,m_pImageInfo.Width,m_pImageInfo.Height);
31 SelectObject(memHdc,hImg);
32
33 rsWeight=m_pImageInfo.Width;
34 rsHight=m_pImageInfo.Height;
35 RECT rect = { 0, 0, m_pImageInfo.Width,m_pImageInfo.Height};
36 m_pImage->Draw(memHdc,&rect,NULL);
37 }
38 else
39 {
40 return NULL;
41 }
42 return TRUE;
43
44 }
呵呵 本人太懒了 所以“明天”才过这么久