1 #pragma once 2 3 // 做成宽松的单例 4 class COperateImage 5 { 6 public: 7 void Show(CClientDC &dc, CPoint point); // point. 8 BOOL GetPicture(const CString &strFileName); 9 static COperateImage *GetInstance(); 10 11 12 protected: 13 COperateImage(void); 14 15 private: 16 static COperateImage *m_pOI; 17 static HBITMAP m_Bitmap; 18 static CString m_strPath; 19 20 private: 21 static CString GetPathName(); 22 HBITMAP loadImageFile(const CString &strFileName); 23 HBITMAP loadBMPImage(const CString &strFileName); 24 25 public: 26 ~COperateImage(void); 27 //public: 28 // void MyBmp(CClientDC &dc); 29 };
1 #include "StdAfx.h" 2 #include "OperateImage.h" 3 4 #include <initguid.h> 5 #include <imaging.h> 6 7 #pragma comment (lib,"Ole32.lib") 8 #pragma comment (lib, "Imaging.lib") 9 10 HBITMAP COperateImage::m_Bitmap = NULL; 11 COperateImage *COperateImage::m_pOI = NULL; 12 CString COperateImage::m_strPath = _T(""); 13 14 15 COperateImage::COperateImage(void) 16 { 17 } 18 19 COperateImage::~COperateImage(void) 20 { 21 } 22 23 24 25 void COperateImage::Show(CClientDC &dc, CPoint point) 26 { 27 if (NULL == m_Bitmap) return; 28 CBitmap bmp1; 29 bmp1.Attach(m_Bitmap); 30 BITMAP bm; 31 bmp1.GetBitmap(&bm); 32 33 //CClientDC dc(this); 34 CDC memDC; 35 memDC.CreateCompatibleDC(&dc); 36 CBitmap* pbmpOld = memDC.SelectObject(&bmp1); 37 //dc.SetStretchBltMode(COLORONCOLOR);//设置拉伸模式,否则图像会不清晰 38 dc.StretchBlt(point.x,point.y,bm.bmWidth,bm.bmHeight,&memDC,0,0,bm.bmWidth,bm.bmHeight,SRCCOPY);//绘制图像 39 memDC.SelectObject(pbmpOld); 40 memDC.DeleteDC(); 41 //dc.DeleteDC(); 42 } 43 44 BOOL COperateImage::GetPicture( const CString &strFileName ) 45 { 46 CString strPathName = m_strPath + strFileName; 47 int iIndex = strFileName.ReverseFind(‘.‘); 48 if (iIndex == -1) return FALSE; 49 CString strRight = strFileName.Right(iIndex).MakeUpper(); 50 if (m_Bitmap) DeleteObject(m_Bitmap); 51 if (strRight == _T("JPG")) 52 m_Bitmap = loadImageFile(strPathName); 53 else if (strRight == _T("BMP")) 54 m_Bitmap = loadBMPImage(strPathName); 55 else return FALSE; 56 57 if (m_Bitmap == NULL) return FALSE; 58 59 return TRUE; 60 } 61 62 CString COperateImage::GetPathName() 63 { 64 TCHAR szPath[MAX_PATH]; 65 GetModuleFileName(NULL, szPath, MAX_PATH); 66 CString PathName(szPath); 67 PathName = PathName.Left(PathName.ReverseFind(_T(‘\\‘)) + 1); 68 return PathName; 69 } 70 71 HBITMAP COperateImage::loadImageFile(const CString &strFileName) 72 { 73 IImagingFactory *pImgFactory =NULL; 74 IImage *pImage =NULL; 75 CoInitializeEx(NULL,COINIT_MULTITHREADED); 76 HBITMAP hResult =0; 77 if (SUCCEEDED(CoCreateInstance(CLSID_ImagingFactory, 78 NULL, 79 CLSCTX_INPROC_SERVER, 80 IID_IImagingFactory, 81 (void **)&pImgFactory))) 82 { 83 ImageInfo imageInfo; 84 if(SUCCEEDED(pImgFactory->CreateImageFromFile(strFileName,&pImage)) 85 && SUCCEEDED(pImage->GetImageInfo(&imageInfo))) 86 { 87 CWindowDC dc(0); 88 CDC dcBitmap; 89 dcBitmap.CreateCompatibleDC(&dc); 90 hResult =CreateCompatibleBitmap(dc.GetSafeHdc(),imageInfo.Width,imageInfo.Height); 91 if(hResult) 92 { 93 HGDIOBJ hOldBitmap = dcBitmap.SelectObject(hResult); 94 HDC safeHdc = dcBitmap.GetSafeHdc(); 95 pImage->Draw(safeHdc,CRect(0,0,imageInfo.Width,imageInfo.Height),NULL); 96 dcBitmap.SelectObject(hOldBitmap); 97 DeleteObject(hOldBitmap); 98 DeleteObject(safeHdc); 99 100 } 101 dcBitmap.DeleteDC(); 102 pImage->Release(); 103 } 104 pImgFactory->Release(); 105 CoUninitialize(); 106 } 107 return hResult; 108 } 109 110 COperateImage * COperateImage::GetInstance() 111 { 112 if (m_pOI == NULL) 113 { 114 m_pOI = new COperateImage(); 115 m_strPath = GetPathName(); 116 } 117 return m_pOI; 118 } 119 120 //void COperateImage::MyBmp( CClientDC &dc ) 121 //{ 122 // //CRect rect; 123 // //GetClientRect(&rect); 124 // CDC dcMem; 125 // dcMem.CreateCompatibleDC(&dc); 126 // CBitmap bmpBackground; 127 // CString strBMPPath = GetPathName() + _T("123.bmp"); 128 // bmpBackground.Attach(::SHLoadDIBitmap(strBMPPath));//动态加载 129 // BITMAP bitmap; 130 // bmpBackground.GetBitmap(&bitmap); 131 // CBitmap *pbmpOld=dcMem.SelectObject(&bmpBackground); 132 // dc.StretchBlt(0,0,bitmap.bmWidth,bitmap.bmHeight,&dcMem,0,0, 133 // bitmap.bmWidth,bitmap.bmHeight,SRCCOPY); 134 //} 135 136 HBITMAP COperateImage::loadBMPImage( const CString &strFileName ) 137 { 138 HBITMAP bitmap = ::SHLoadDIBitmap(strFileName); 139 return bitmap; 140 }
在这里通过单例的模式进行操作,因为我目前遇到的这个需求只是显示一张图片。
这里读取的是程序的当前路径的图片,用户也可以进行自己修改对应的图片路径,这里可以通过show的方式显示图片,同一张图片,在一个情况下只需加载一次即可,避免多次加载产生的效率问题。cclient dc 即图片要贴的位置,通过用户自己指定。