您的位置:首页 > 其它

使图片 放大 缩小 原始 最佳

2011-09-26 11:33 148 查看
对图片进行放大等操作时,关键是找一个基准点--------也就是说,什么时候的图片为起始图片,所有的放大缩小都是参照这个图片进行的。

在本例中 选择最佳图片为基准点。

所谓最佳----------是指的 图片的宽与高都再可见视图中。

定义变量:

CRect   m_ClientRect;  // 视图客户区

int  m_ImgW; //  从文件加载后,得到的原始图片宽
int  m_ImgH; //  原始图片高
float m_fRate; // 最佳图片情况时,原始图片需缩小的比率
float m_fzoom; //以最佳图片为参照点,控制放大缩小


初始化变量:

m_ImgW=m_ImgH=0;
m_fRate=1.0;
m_fzoom=1.0;


OnPaint():

if (m_pImage!=NULL)
{
if(m_pBmp==NULL)  // OnSize中 delete掉m_pBmp  否则放大对话框时有问题,因为m_pBmp还是原来的尺寸,所以放大可能出现问题
{
m_pBmp=new Bitmap(m_ClientRect.Width(),m_ClientRect.Height());
}
Graphics gbmp(m_pBmp);
gbmp.Clear(Color(255,255,255,255));

int nWidth=m_ImgW/m_fRate*m_fzoom;   //m_ImgW/m_fRate为最佳图像的宽  nWidth为缩放操作后图片的宽
int nHeigth=m_ImgH/m_fRate*m_fzoom;  //m_ImgH/m_fRate 为最佳图像的高 nHeight为缩放操作后图片的高

if (m_fzoom>1) //在最佳图片基础上进行放大 图片左上角位置为m_Point
{
gbmp.DrawImage(m_pImage,m_Point.x,m_Point.y,nWidth,nHeigth);
}else{  // 在最佳图片基础上缩小   图片的位置在视图中间

//m_ClientRect.Width()/2为客户区宽度中间,m_ClientRect.Width()/2-nWidth/2为图片左上角X值
//m_ClientRect.Height()/2为客户区高度中间,m_ClientRect.Height()/2-nHeigth/2,nWidth,nHeigth为图片左上角Y值
gbmp.DrawImage(m_pImage,m_ClientRect.Width()/2-nWidth/2,m_ClientRect.Height()/2-nHeigth/2,nWidth,nHeigth);
}

Graphics g(GetDC()->m_hDC);
g.DrawImage(m_pBmp,0,m_ClientRect.top);
}


OnSize():要适时修改比率m_fRate 因为此时客户区大小改变了,所以要进行修改

if (m_pBmp!=NULL)
{
delete m_pBmp;
m_pBmp=NULL;
}

if (m_pImage!=NULL)
{
float fRateW=(float)m_ImgW/m_ClientRect.Width();
float fRateH=(float)m_ImgH/m_ClientRect.Height();
m_fRate=max(fRateH,fRateW);
}


放大缩小等操作直接控制m_zoom即可:

void NewImageView::OnBnClickedEnLarge()
{
// TODO: 在此添加控件通知处理程序代码

m_fzoom*=1.25;
InvalidateRect(m_ClientRect);

}


void NewImageView::OnBnClickedButton1() //缩小
{
// TODO: 在此添加控件通知处理程序代码

m_fzoom/=1.25;
//Invalidate(FALSE);

InvalidateRect(m_ClientRect);
}


void NewImageView::OnBnClickedButton4() //最佳
{
// TODO: 在此添加控件通知处理程序代码

m_fzoom=1.0;

InvalidateRect(m_ClientRect);

}


void NewImageView::OnBnClickedButton3()  //原始
{
// TODO: 在此添加控件通知处理程序代码

m_fzoom=m_fRate;

InvalidateRect(m_ClientRect);
}


以下为源程序(含其他功能):

.h

#pragma once
#include "afxwin.h"

// NewImageView 窗体视图

class NewImageView : public CFormView
{
DECLARE_DYNCREATE(NewImageView)

protected:
NewImageView();           // 动态创建所使用的受保护的构造函数
virtual ~NewImageView();

public:
enum { IDD = IDD_FORMVIEW };
#ifdef _DEBUG
virtual void AssertValid() const;
#ifndef _WIN32_WCE
virtual void Dump(CDumpContext& dc) const;
#endif
#endif

protected:
virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支持

DECLARE_MESSAGE_MAP()

public:
Image *m_pImage;
Bitmap *m_pBmp;

CPoint m_Point;
CPoint m_LbtndownP;
CPoint m_PreP;

GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
public:
virtual void OnInitialUpdate();
public:
afx_msg void OnPaint();

//-----------
public:
CButton m_enlarge;
CRect   m_ClientRect;  // 视图客户区

int  m_ImgW; //  从文件加载后,得到的原始图片宽
int  m_ImgH; //  原始图片高
float m_fRate; // 最佳图片情况时,原始图片需缩小的比率
float m_fzoom; //以最佳图片为参照点,控制放大缩小

public:
afx_msg void OnSize(UINT nType, int cx, int cy);
public:
afx_msg void OnBnClickedEnLarge();

//---------------

void SetImage(CString str);
public:
afx_msg void OnBnClickedButton4();
public:
afx_msg void OnBnClickedButton1();
public:
afx_msg BOOL OnEraseBkgnd(CDC* pDC);
public:
afx_msg void OnBnClickedButton3();
public:
afx_msg void OnBnClickedButton5();
public:
afx_msg void OnBnClickedButton6();
};


.cpp

// NewImageView.cpp : 实现文件
//

#include "stdafx.h"
#include "TreePath.h"
#include "NewImageView.h"

#include "MainFrm.h"

#include "TreePathView.h"

// NewImageView

IMPLEMENT_DYNCREATE(NewImageView, CFormView)

NewImageView::NewImageView()
: CFormView(NewImageView::IDD)
{
m_pImage=NULL;
m_pBmp=NULL;
m_Point.x=m_Point.y=0;
m_PreP=m_LbtndownP=m_Point;

m_ImgW=m_ImgH=0;
m_fRate=1.0;
m_fzoom=1.0;

}

NewImageView::~NewImageView()
{
if (m_pImage!=NULL)
{
delete m_pImage;
m_pImage=NULL;
}

GdiplusShutdown(gdiplusToken);

}

void NewImageView::DoDataExchange(CDataExchange* pDX)
{
CFormView::DoDataExchange(pDX);
DDX_Control(pDX, IDC_BUTTON2, m_enlarge);
}
BEGIN_MESSAGE_MAP(NewImageView, CFormView)
ON_WM_PAINT()
ON_WM_SIZE()
ON_BN_CLICKED(IDC_BUTTON2, &NewImageView::OnBnClickedEnLarge)
ON_BN_CLICKED(IDC_BUTTON4, &NewImageView::OnBnClickedButton4)
ON_BN_CLICKED(IDC_BUTTON1, &NewImageView::OnBnClickedButton1)
ON_WM_ERASEBKGND()
ON_BN_CLICKED(IDC_BUTTON3, &NewImageView::OnBnClickedButton3)
ON_BN_CLICKED(IDC_BUTTON5, &NewImageView::OnBnClickedButton5)
ON_BN_CLICKED(IDC_BUTTON6, &NewImageView::OnBnClickedButton6)
END_MESSAGE_MAP()

// NewImageView 诊断

#ifdef _DEBUG
void NewImageView::AssertValid() const
{
CFormView::AssertValid();
}

#ifndef _WIN32_WCE
void NewImageView::Dump(CDumpContext& dc) const
{
CFormView::Dump(dc);
}
#endif
#endif //_DEBUG

// NewImageView 消息处理程序

void NewImageView::OnInitialUpdate()
{
CFormView::OnInitialUpdate();

GdiplusStartup(&gdiplusToken,&gdiplusStartupInput,NULL);

// TODO: 在此添加专用代码和/或调用基类
}

void NewImageView::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: 在此处添加消息处理程序代码
// 不为绘图消息调用 CFormView::OnPaint()

{
CRect topRect;
GetClientRect(&topRect);
topRect.bottom=m_ClientRect.top;
dc.FillSolidRect(topRect,RGB(0,155,155));
}

if (m_pImage!=NULL)
{
if(m_pBmp==NULL) // OnSize中 delete掉m_pBmp 否则放大对话框时有问题,因为m_pBmp还是原来的尺寸,所以放大可能出现问题
{
m_pBmp=new Bitmap(m_ClientRect.Width(),m_ClientRect.Height());
}
Graphics gbmp(m_pBmp);
gbmp.Clear(Color(255,255,255,255));

int nWidth=m_ImgW/m_fRate*m_fzoom; //m_ImgW/m_fRate为最佳图像的宽 nWidth为缩放操作后图片的宽
int nHeigth=m_ImgH/m_fRate*m_fzoom; //m_ImgH/m_fRate 为最佳图像的高 nHeight为缩放操作后图片的高

if (m_fzoom>1) //在最佳图片基础上进行放大 图片左上角位置为m_Point
{
gbmp.DrawImage(m_pImage,m_Point.x,m_Point.y,nWidth,nHeigth);
}else{ // 在最佳图片基础上缩小 图片的位置在视图中间

//m_ClientRect.Width()/2为客户区宽度中间,m_ClientRect.Width()/2-nWidth/2为图片左上角X值
//m_ClientRect.Height()/2为客户区高度中间,m_ClientRect.Height()/2-nHeigth/2,nWidth,nHeigth为图片左上角Y值
gbmp.DrawImage(m_pImage,m_ClientRect.Width()/2-nWidth/2,m_ClientRect.Height()/2-nHeigth/2,nWidth,nHeigth);
}

Graphics g(GetDC()->m_hDC);
g.DrawImage(m_pBmp,0,m_ClientRect.top);
}else{

GetDC()->FillSolidRect(m_ClientRect,RGB(255,255,255));
}

}

void NewImageView::OnSize(UINT nType, int cx, int cy)
{
CFormView::OnSize(nType, cx, cy);

CRect rect;

/* if (GetDlgItem(IDC_BUTTON2))
{
GetDlgItem(IDC_BUTTON2)->GetClientRect(&rect);
}
*/

if (m_enlarge.GetSafeHwnd())
{
m_enlarge.GetClientRect(&rect);
GetClientRect(&m_ClientRect);
m_ClientRect.top=rect.bottom;

if (m_pBmp!=NULL)
{
delete m_pBmp;
m_pBmp=NULL;
}

if (m_pImage!=NULL)
{
float fRateW=(float)m_ImgW/m_ClientRect.Width();
float fRateH=(float)m_ImgH/m_ClientRect.Height();
m_fRate=max(fRateH,fRateW);
}

}

// TODO: 在此处添加消息处理程序代码
}

void NewImageView::OnBnClickedEnLarge() { // TODO: 在此添加控件通知处理程序代码 m_fzoom*=1.25; InvalidateRect(m_ClientRect); }

void NewImageView::SetImage(CString str)
{

if(m_pBmp)
delete m_pBmp;
m_pBmp=new Bitmap(m_ClientRect.Width(),m_ClientRect.Height());
m_pImage=Image::FromFile(str);

m_ImgH=m_pImage->GetHeight();
m_ImgW=m_pImage->GetWidth();
float fRateW=(float)m_ImgW/m_ClientRect.Width();
float fRateH=(float)m_ImgH/m_ClientRect.Height();

m_fRate=max(fRateH,fRateW);
InvalidateRect(m_ClientRect);

}
void NewImageView::OnBnClickedButton4() //最佳 { // TODO: 在此添加控件通知处理程序代码 m_fzoom=1.0; InvalidateRect(m_ClientRect); }

void NewImageView::OnBnClickedButton1() //缩小 { // TODO: 在此添加控件通知处理程序代码 m_fzoom/=1.25; //Invalidate(FALSE); InvalidateRect(m_ClientRect); }

BOOL NewImageView::OnEraseBkgnd(CDC* pDC)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值

return FALSE;

return CFormView::OnEraseBkgnd(pDC);
}

void NewImageView::OnBnClickedButton3() //原始 { // TODO: 在此添加控件通知处理程序代码 m_fzoom=m_fRate; InvalidateRect(m_ClientRect); }

void NewImageView::OnBnClickedButton5() // 上一个图
{
// TODO: 在此添加控件通知处理程序代码

CTreePathView * pTree=(CTreePathView* )(((CMainFrame *)AfxGetMainWnd())->m_wndSplitter.GetPane(0,0));

CTreeCtrl & treeCtrl=pTree->GetTreeCtrl();

HTREEITEM hItem=treeCtrl.GetSelectedItem();
if (hItem!=NULL)
{
HTREEITEM hItemP=treeCtrl.GetNextItem(hItem,TVGN_PREVIOUS);

if (hItemP!=NULL)
{
CString str;
str=pTree->GetFullPath(hItemP);

if (pTree->IsImage(str))
SetImage(str);
treeCtrl.SelectItem(hItemP);
treeCtrl.SetFocus();
InvalidateRect(m_ClientRect);
}else{
treeCtrl.SelectItem(hItem);
treeCtrl.SetFocus();
}
}
}

void NewImageView::OnBnClickedButton6() //下一个
{
// TODO: 在此添加控件通知处理程序代码

CTreePathView * pTree=(CTreePathView* )(((CMainFrame *)AfxGetMainWnd())->m_wndSplitter.GetPane(0,0));
CTreeCtrl & treeCtrl=pTree->GetTreeCtrl();
HTREEITEM hItem=treeCtrl.GetSelectedItem();

if (hItem!=NULL)
{
HTREEITEM hItemN=treeCtrl.GetNextItem(hItem,TVGN_NEXT);

if (hItemN!=NULL)
{
CString str;
str=pTree->GetFullPath(hItemN);

if (pTree->IsImage(str))
SetImage(str);

treeCtrl.SelectItem(hItemN);
treeCtrl.SetFocus();
InvalidateRect(m_ClientRect);
}else{

treeCtrl.SelectItem(hItem);
treeCtrl.SetFocus();

}

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: