Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Zoom image on mouse position

Zoom image on mouse position

Scheduled Pinned Locked Moved C / C++ / MFC
graphicshelpquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    Gopi Nath
    wrote on last edited by
    #1

    Hello, I am displaying an image in a static control (in a dialog) and trying to stretch according to the zoom factor on mouse position. Zoom on mouse position is working fine, but the image is not fitting inside the static control, going all over the dialog. Below is the code.

    // source image
    Gdiplus::Bitmap *gpBitmap = new Gdiplus::Bitmap(mImagePath);

    int origW = gpBitmap->GetWidth(); // 5298 - original width
    int origH = gpBitmap->GetHeight(); // 2728 - original height
    
    // destination rectangle
    RECT rcVPC;
    GetClientRect(hWnd, &rcVPC); // 501 X 255 - size of the static control
    HDC hDc = GetDC(hWnd);
    
    float perW = (float)rcVPC.right / (float)origW;
    float perH = (float)rcVPC.bottom / (float)origH;
    	
    int newW = origW \* perW \* mVPZoomFactor; // starting factor is 1.0 and multiplying with 1.2 with each zoom
    int newH = origH \* perH \* mVPZoomFactor;
    
    Gdiplus::Bitmap \*bmpResized = new Gdiplus::Bitmap(newW, newH, gpBitmap->GetPixelFormat());
    Gdiplus::Graphics \*graphics = new Gdiplus::Graphics(bmpResized);
    graphics->DrawImage(gpBitmap, 0, 0, newW, newH);
    
    int zoomWidth = (int)(0.5 + newW \* mVPZoomFactor);
    int zoomHeight = (int)(0.5 + newH \* mVPZoomFactor);
    
    //Find the position "factor"
    double dXFactor = (double)rcVPC.right / stX; // stX, stY - mouse position on zoom
    double dYFactor = (double)rcVPC.bottom / stY;
    
    //Find the origin
    int left = stX - zoomWidth / dXFactor;
    int right = stY - zoomHeight / dYFactor;
    
    HBRUSH   hB = GetSysColorBrush(COLOR\_3DDKSHADOW);
    
    FillRect(hDc, &rcVPC, hB);
    
    HDC hdcMem = CreateCompatibleDC(hDc);
    
    bmpResized->GetHBITMAP(0, &hBmpVPCanvas);
    
    HBITMAP oldBmp = (HBITMAP)SelectObject(hdcMem, hBmpVPCanvas);
    
    //use StretchBlt
    StretchBlt(hDc, left, right, zoomWidth, zoomHeight, hdcMem, 0, 0, newW, newH, SRCCOPY);
    
    SelectObject(hdcMem, oldBmp);
    
    DeleteDC(hdcMem);
    
    delete gpBitmap;
    

    Can anyone help me, what i am doing wrong? Regards, Gopinath.

    L 1 Reply Last reply
    0
    • G Gopi Nath

      Hello, I am displaying an image in a static control (in a dialog) and trying to stretch according to the zoom factor on mouse position. Zoom on mouse position is working fine, but the image is not fitting inside the static control, going all over the dialog. Below is the code.

      // source image
      Gdiplus::Bitmap *gpBitmap = new Gdiplus::Bitmap(mImagePath);

      int origW = gpBitmap->GetWidth(); // 5298 - original width
      int origH = gpBitmap->GetHeight(); // 2728 - original height
      
      // destination rectangle
      RECT rcVPC;
      GetClientRect(hWnd, &rcVPC); // 501 X 255 - size of the static control
      HDC hDc = GetDC(hWnd);
      
      float perW = (float)rcVPC.right / (float)origW;
      float perH = (float)rcVPC.bottom / (float)origH;
      	
      int newW = origW \* perW \* mVPZoomFactor; // starting factor is 1.0 and multiplying with 1.2 with each zoom
      int newH = origH \* perH \* mVPZoomFactor;
      
      Gdiplus::Bitmap \*bmpResized = new Gdiplus::Bitmap(newW, newH, gpBitmap->GetPixelFormat());
      Gdiplus::Graphics \*graphics = new Gdiplus::Graphics(bmpResized);
      graphics->DrawImage(gpBitmap, 0, 0, newW, newH);
      
      int zoomWidth = (int)(0.5 + newW \* mVPZoomFactor);
      int zoomHeight = (int)(0.5 + newH \* mVPZoomFactor);
      
      //Find the position "factor"
      double dXFactor = (double)rcVPC.right / stX; // stX, stY - mouse position on zoom
      double dYFactor = (double)rcVPC.bottom / stY;
      
      //Find the origin
      int left = stX - zoomWidth / dXFactor;
      int right = stY - zoomHeight / dYFactor;
      
      HBRUSH   hB = GetSysColorBrush(COLOR\_3DDKSHADOW);
      
      FillRect(hDc, &rcVPC, hB);
      
      HDC hdcMem = CreateCompatibleDC(hDc);
      
      bmpResized->GetHBITMAP(0, &hBmpVPCanvas);
      
      HBITMAP oldBmp = (HBITMAP)SelectObject(hdcMem, hBmpVPCanvas);
      
      //use StretchBlt
      StretchBlt(hDc, left, right, zoomWidth, zoomHeight, hdcMem, 0, 0, newW, newH, SRCCOPY);
      
      SelectObject(hdcMem, oldBmp);
      
      DeleteDC(hdcMem);
      
      delete gpBitmap;
      

      Can anyone help me, what i am doing wrong? Regards, Gopinath.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Gopi Nath wrote:

      what i am doing wrong?

      Probably mixing GDI and GDI+; you should only use one of these, preferably GDI+. Zooming an image is quite simple in GDI+ as you just need to specify the target rectangle's position and size.

      G 1 Reply Last reply
      0
      • L Lost User

        Gopi Nath wrote:

        what i am doing wrong?

        Probably mixing GDI and GDI+; you should only use one of these, preferably GDI+. Zooming an image is quite simple in GDI+ as you just need to specify the target rectangle's position and size.

        G Offline
        G Offline
        Gopi Nath
        wrote on last edited by
        #3

        Thank you Richard, tried with GDI+ only with some changes, now, i am able to get the required output. Regards, Gopinath.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups