巧用StretchBlt实现图像放大镜

来源:互联网 发布:无烟锅 知乎 编辑:程序博客网 时间:2024/05/17 04:07

巧用StretchBlt实现图像放大镜

化二院电算站   汪援东

---- 在有些应用中需要对图像的局部进行放大,本例程用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;  //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);
}


 

 

原创粉丝点击