对图像的局部进行放大

在有些应用中需要对图像的局部进行放大,本例程用VC++ 5.0编写,用CDC的StretchBlt实现图像放大镜,主要有以下功能:1.移动MOUSE放大显示图像的不同部位,2.左击增加放大倍率,3.右击减少放大倍率。 当放大倍率较大时产生抖动。

---- 实现过程:

---- 1.使用AppWizard生成SDI应用IMAGE。

---- 2.为CImageView增加以下成员数据:

CSize m_sizeDest;
CSize m_sizeSource;
CBitmap * m_pBitmap;
CDC * m_pdcMem;
int oldx,oldy,s,d; file://s确定被放大区域,
d确定放大显示区域,放大倍率=d/s
bool recover;
long mana;

---- 3.在资源中加入自己喜欢的位图并设为IDB_BITMAP1。

---- 4.对CImageView的以下消息编程:

CImageView::CImageView()
{
// Initialize values
m_pdcMem = new CDC;
m_pBitmap = new CBitmap;
recover = true;
s = 30; d = 45;
mana = SRCCOPY;
}

CImageView::~CImageView()
{
delete m_pdcMem;
delete m_pBitmap;
}

void CImageView::OnDraw(CDC* pDC)
{
static bool load;
if (!load) {
BITMAP bm;
load = !load;
m_pBitmap- >LoadBitmap(IDB_BITMAP1);
m_pdcMem- >CreateCompatibleDC(pDC);
m_pdcMem- >SelectObject(m_pBitmap);
m_pBitmap- >GetObject(sizeof(bm),&bm);
m_sizeSource.cx = bm.bmWidth;
m_sizeSource.cy = bm.bmHeight;
m_sizeDest = m_sizeSource;
pDC- >StretchBlt(0,0,m_sizeSource.cx,m_sizeSource.cy,
m_pdcMem,0,0,m_sizeSource.cx,m_sizeSource.cy,mana);
}
else {
pDC- >StretchBlt(0,0,m_sizeSource.cx,m_sizeSource.cy,
m_pdcMem,0,0,m_sizeSource.cx,m_sizeSource.cy,mana);
}
}
void CImageView::OnMouseMove(UINT nFlags, CPoint point)
{
CString cord;
int dd;
CRect srect,drect,mrect;
CMainFrame * pFrame = (CMainFrame *)AfxGetApp()- >m_pMainWnd ;
CStatusBar * pStatus = &pFrame- >m_wndStatusBar ;
if (pStatus)
{
cord.Format("X = %d, Y = %d",point.x,point.y);
pStatus- >SetPaneText (1,cord);
srect.left = point.x - s;
srect.top = point.y - s;
srect.right = point.x + s;
srect.bottom = point.y + s;

drect.left = point.x - d;
drect.top = point.y - d;
drect.right = point.x + d;
drect.bottom = point.y + d;

mrect.left = oldx - d;
mrect.top = oldy - d;
mrect.right = oldx + d;
mrect.bottom = oldy + d;
dd = 2*d;
CDC * pDC = GetDC();
OnPrepareDC(pDC);
if (recover)
{
pDC- >BitBlt(mrect.left,mrect.top,dd,dd,
m_pdcMem,mrect.left,mrect.top,mana);
}
pDC- >StretchBlt(drect.left,drect.top,
drect.Width(),drect.Height(),m_pdcMem,srect.left,
srect.top,srect.Width(),srect.Height(),SRCCOPY);
oldx = point.x; oldy = point.y;
ReleaseDC(pDC);
}
recover = true;
CView::OnMouseMove(nFlags, point);
}

void CImageView::OnRButtonDown(UINT nFlags, CPoint point)
{
if (d > 5)
{
CDC * pDC = GetDC();
pDC- >StretchBlt(oldx - d,oldy - d,2*d,
2*d,m_pdcMem,oldx - d,oldy - d,2*d,2*d,mana);
d -= 10;
ReleaseDC(pDC);
CImageView::OnMouseMove(nFlags, point);
}
CView::OnRButtonDown(nFlags, point);
}

void CImageView::OnLButtonDown(UINT nFlags, CPoint point)
{
if (d < 150)
{
d += 10;
CImageView::OnMouseMove(nFlags, point);
}
CView::OnLButtonDown(nFlags, point);

 
更多分享请关注:软信网-编程-http://www.iis365.net.cn

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值