#ifndef _AFXSTD_507A8D18_E3C1_47AA_8892_41786A393718_INCLUDE_H_ #define _AFXSTD_507A8D18_E3C1_47AA_8892_41786A393718_INCLUDE_H_ #if _MSC_VER > 1200 #pragma once #endif //!_MSC_VER > 1200 #include <atlimage.h> #include <string> using namespace std; //====================================CImageEx==================================== /// @brief 图像处理类 /// /// 格式: BMP、GIF、JPG、PNG、TIF等 /// 功能: 图像加载、保存 /// 图像信息的获取 /// 图像数据的获取与修改 /// 位图的绑定与获取 /// 图像绘制... //================================================================================ class CImageEx { public: CImageEx(); ~CImageEx(); public: //通过文件路径imagePath加载图像,必须带后缀 HRESULT LoadImage(string &imagePath); //指定文件路径imagePath保存图像,必须带后缀 HRESULT SaveImage(string &imagePath); //图像宽度(以像素为单位) int GetWidth(); //图像高度(以像素为单位) int GetHeight(); //图像数据 BYTE *GetData(); //每行的字节数 //值 > 0 图像方向为从上到下, GetData()获得的指针指向第一行 //值 < 0 图像方向为从下到上, GetData()获得的指针指向最后一行 int GetRowBytes(); //每个像素所占的bit数 int GetBPP(); //设置位置为x、y的像素的颜色 <span style="white-space:pre"> </span>void SetPixelColor(int x, int y, int color); void SetPixelColor(int x, int y, int r, int g, int b); //获取位置为x、y的像素的颜色 <span style="white-space:pre"> </span>int GetPixelColor(int x, int y); //获取位置为x、y的像素的颜色内存数据 void *GetPixelAddress( int x, int y ); //将一个DIB(或DDB)位图与CImage对象相连接 void AttachHBitMap(HBITMAP &pHbitMap); //返回HBITMAP位图句柄 HRESULT DetachHBitMap(HBITMAP &pHbitMap); //返回CBitMap CBitmap *GetBitmap(); //将数据绘制到hWnd的窗体上 void Draw(HWND hWnd); //将数据绘制保存到位图路径imagePath中 //宽度为 width,长度为 heigth void DrawToBitMap(string &imagePath, int width, int heigth); //获取CImage CImage *GetImagePtr(); private: CImage *m_pImage; }; #endif //!_AFXSTD_507A8D18_E3C1_47AA_8892_41786A393718_INCLUDE_H_
#include "stdafx.h" #include <windows.h> #include "Image.h" CImageEx::CImageEx() { m_pImage = NULL; } CImageEx::~CImageEx() { if (NULL != m_pImage) { m_pImage->Destroy(); } } HRESULT CImageEx::LoadImage( string &imagePath ) { if (NULL != m_pImage && !m_pImage->IsNull()) { m_pImage->Destroy(); } if (NULL == m_pImage) { m_pImage = new CImage; } return( m_pImage->Load((CString)imagePath.c_str()) ); } CImage * CImageEx::GetImagePtr() { return(m_pImage); } HRESULT CImageEx::DetachHBitMap( HBITMAP &pHbitMap ) { if (NULL == m_pImage || m_pImage->IsNull()) { return(E_POINTER); } pHbitMap = m_pImage->Detach(); return(S_OK); } CBitmap * CImageEx::GetBitmap() { if (NULL == m_pImage || m_pImage->IsNull()) { return(NULL); } return(CBitmap::FromHandle(m_pImage->Detach())); } BYTE * CImageEx::GetData() { if (NULL == m_pImage || m_pImage->IsNull()) { return(NULL); } return ( (BYTE *)m_pImage->GetBits() ); } int CImageEx::GetRowBytes() { if (NULL == m_pImage || m_pImage->IsNull()) { return(0); } return ( m_pImage->GetPitch() ); } int CImageEx::GetBPP() { if (NULL == m_pImage || m_pImage->IsNull()) { return(0); } return ( m_pImage->GetBPP() ); } int CImageEx::GetWidth() { if (NULL == m_pImage || m_pImage->IsNull()) { return(0); } return ( m_pImage->GetWidth() ); } int CImageEx::GetHeight() { if (NULL == m_pImage || m_pImage->IsNull()) { return(0); } return ( m_pImage->GetHeight() ); } HRESULT CImageEx::SaveImage( string &imagePath ) { if (NULL == m_pImage || m_pImage->IsNull()) { return(0); } return ( m_pImage->Save((CString)imagePath.c_str()) ); } void CImageEx::AttachHBitMap( HBITMAP &pHbitMap ) { if (NULL != m_pImage && !m_pImage->IsNull()) { m_pImage->Destroy(); } if (NULL == m_pImage) { m_pImage = new CImage; } m_pImage->Attach(pHbitMap); } void CImageEx::SetPixelColor( int x, int y, int color ) { if (NULL == m_pImage || m_pImage->IsNull()) { return; } m_pImage->SetPixel(x, y, (COLORREF)(color)); } void CImageEx::SetPixelColor( int x, int y, int r, int g, int b ) { if (NULL == m_pImage || m_pImage->IsNull()) { return; } m_pImage->SetPixelRGB(x, y, r, g, b); } int CImageEx::GetPixelColor( int x, int y ) { if (NULL == m_pImage || m_pImage->IsNull()) { return(0); } return(m_pImage->GetPixel(x, y)); } void *CImageEx::GetPixelAddress( int x, int y ) { if (NULL == m_pImage || m_pImage->IsNull()) { return(NULL); } return(m_pImage->GetPixelAddress(x, y)); } void CImageEx::Draw( HWND hWnd ) { if (NULL == m_pImage || m_pImage->IsNull()) { return; } RECT rect; HDC hDC = GetDC(hWnd); ::GetClientRect(hWnd, &rect); m_pImage->Draw(hDC, rect); //窗体重绘事件使用BeginPaint与EndPaint } void CImageEx::DrawToBitMap(string &imagePath, int width, int heigth) { if (NULL == m_pImage || m_pImage->IsNull()) { return; } HDC hDC = m_pImage->GetDC(); CDC *pCDC = CDC::FromHandle(hDC); //创建位图 CBitmap *pBitMap = new CBitmap; pBitMap->DeleteObject(); pBitMap->CreateCompatibleBitmap(pCDC, width, heigth); //创建绘制DC,绑定pBitMap CDC memDC; memDC.CreateCompatibleDC(pCDC); CBitmap *pOldBitMap = memDC.SelectObject(pBitMap); //将图像绘制到pBitMap中 ::SetStretchBltMode(memDC.m_hDC, HALFTONE); ::SetBrushOrgEx(memDC.m_hDC, 0, 0, NULL); m_pImage->StretchBlt(memDC.m_hDC, 0, 0, width, heigth, 0, 0, m_pImage->GetWidth(), m_pImage->GetHeight(), SRCCOPY); HBITMAP hBitMap = (HBITMAP)memDC.SelectObject(pOldBitMap->m_hObject); //位图资源 //保存到目的路径 CImage imageDes; imageDes.Attach(hBitMap); imageDes.Save((CString)imagePath.c_str()); memDC.DeleteDC(); if (NULL != pBitMap) { pBitMap->DeleteObject(); delete pBitMap; } m_pImage->ReleaseDC(); }
CImageEx imageEx; imageEx.LoadImage(string("C:\\Tulip.jpg")); imageEx.DrawToBitMap(string("C:\\Tulip2.jpg"), 200, 100);
数字图像处理 CImage类的使用与封装(jpg png gif tif bmp等格式图像的加载、数据读写、保存等功能),布布扣,bubuko.com
数字图像处理 CImage类的使用与封装(jpg png gif tif bmp等格式图像的加载、数据读写、保存等功能)
原文地址:http://blog.csdn.net/aoshilang2249/article/details/38185393