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