Zoom image on mouse position
-
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.
-
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.
-
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.