3

I am new to MFC. How to zoom Bitmap image from center. In my code image is zooming from top left corner. But image should zoom from center in scrollbar..

void CRightUp::ImageDraw(int sHeight,int sWidth){
    CDC *screenDC = GetDC();
    CDC mDC;
    mDC.CreateCompatibleDC(screenDC);
    CBitmap bitmap;
    bitmap.CreateCompatibleBitmap(screenDC, sWidth, sHeight);
    CBitmap *pob = mDC.SelectObject(&bitmap);
    mDC.SetStretchBltMode(HALFTONE);
    m_img.StretchBlt(mDC.m_hDC, 0, 0, sWidth, sHeight, 0, 0,   m_img.GetWidth(), m_img.GetHeight(), SRCCOPY);
    mDC.SelectObject(pob);
    m_logoImage.SetBitmap((HBITMAP)bitmap.Detach());
    ReleaseDC(screenDC);}
abhi312
  • 364
  • 1
  • 6
  • 25
  • what is m_logoImage? You are creating a new HBITMAP every time, instead of just drawing it. Why not just handle the in OnPaint method? – Garr Godfrey Jun 02 '15 at 05:40
  • @Garr Godfrey m_logoImage CStatic object. I have used picture control to display the image. – abhi312 Jun 02 '15 at 05:43
  • well, you may be able to do it by making the CStatic object the size of the window, and setting its alignment properties....or I'll post an answer for just painting it. Is CRightUp a CWnd derivative? – Garr Godfrey Jun 02 '15 at 05:44
  • CFormView inherits from CWnd. Depending on child controls you have, the answer I posted should work – Garr Godfrey Jun 02 '15 at 05:52
  • yeah !!! right............ – abhi312 Jun 02 '15 at 05:59
  • 1
    that said, your technique should work if you set the SS_CENTERIMAGE style on the m_logoImage and make it the full size of the client area. – Garr Godfrey Jun 02 '15 at 06:01

1 Answers1

3

Why bother with the m_logoImage:

void CRightUp::ImageDraw(int sHeight,int sWidth){
    RECT r;
    GetClientRect(&r);
    CDC *screenDC = GetDC();
    m_img.StretchBlt(screenDC->m_hDC, ((r.right-r.left)-sWidth)/2, 
         ((r.bottom-r.top)-sHeight)/2, 
         sWidth, sHeight, 0, 0,  
         m_img.GetWidth(), m_img.GetHeight(), SRCCOPY);
    ReleaseDC(screenDC);
}

This will only paint it for the moment. If you are calling ImageDraw rapidly (say, every 1/10th a second or more), it should be fine until it is completely zoomed. At that point, you can handle it in a CStatic or in your OnPaint or OnEraseBackground

Garr Godfrey
  • 8,257
  • 2
  • 25
  • 23