从资源中获取图片并显示

1.CoInitialize, CoInitialzeEx

      CoInitializeCoInitializeEx都是windowsAPI,主要是告诉windows以什么方式为程序创建COM对象,原因是程序调用com库函数(除CoGetMalloc和内存分配函数)之前必须初始化com库。
      
有哪些方式呢?单线程和多线程。
      CoInitialize
指明以单线程方式创建。
      CoInitializeEx
可以指定COINIT_MULTITHREADED以多线程方式创建。
      
创建单线程方式的COM服务器时不用考虑串行化问题,多线程COM服务器就要考虑。
      
在使用中,使用CoInitialize创建可使对象直接与线程连接,得到最高的性能。创建多线程对象可以直接接收所有线程的调用,不必像单线程那样需要消息排队,但却需要COM创建线程间汇集代理,这样访问效率不高。 

注:新的应用程序应该调用CoInitializeEx而不是CoInitialize一般是在 Dll 中使用 COM 才会需要使用的。

 

2.CoInitializeEx编译通过的问题

在初始化Com套间的时候,调用CoInitializeEx(NULL, COINIT_MULTITHREADED); 可编译器总是提示编译:

 error C2065: 'CoInitializeEx' : undeclared identifier

error C2065: 'COINIT_MULTITHREADED' : undeclared identifier

最后,在stdafx.h文件中的增加红色代码就可以了。

......

#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0400           //
使用CoInitializeEx
#endif

#include <afxwin.h>         // MFC core and standard components
#include <afxext.h>         // MFC extensions
#include <afxcview.h>
#include <afxdisp.h>        // MFC Automation classes
#include <afxdtctl.h>  // MFC support for Internet Explorer 4 Common Controls
#ifndef _AFX_NO_AFXCMN_SUPPORT
#include <afxcmn.h>   // MFC support for Windows Common Controls
#endif // _AFX_NO_AFXCMN_SUPPORT

......

3.例子

 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  }

 另注:在Visual studio中添加png图片资源步骤:

 首先,右击"资源文件",添加"资源",会开打选择资源类型的对话框,由于png、jpg等资源没有提供默认的选择按钮,所以单击"自定义"按钮,把所要添加的资源添加进去。在代码中,当要使用添加进来的资源时,需要把上面的代码某部分修改为:  

HRSRC hRes= FindResource(m_hWnd,MAKEINTRESOURCE(IDR_PNG_BOARD), _T("PNG")  ); //IDR_JPG_BOARD资源号,而第三个参数PNG是你自定义资源时添加的资源类型名称

你可能感兴趣的:(图片)