Reputation: 11
This is the part where the image set to the picture controller:
void CMFCApplication3View::drawImg1(Mat img) {
int nX = img.rows;
int nY = img.cols;
CImage image;
Mat2CImage(&img, image);
CRect rect;//픽쳐 컨트롤의 크기를 저장할 CRect 객체
pictureControl.GetWindowRect(rect);//GetWindowRect를 사용해서 픽쳐 컨트롤의 크기를 받는다.
CDC* dc; //픽쳐 컨트롤의 DC를 가져올 CDC 포인터
dc = pictureControl.GetDC(); //픽쳐 컨트롤의 DC를 얻는다.
image.StretchBlt(dc->m_hDC, 0, 0, nX, nY, SRCCOPY);//이미지를 픽쳐 컨트롤 크기로 조정 rect.Width(), rect.Height()
ReleaseDC(dc);//DC 해제
}
void CMFCApplication3View::drawImg2(Mat img) {
int nX = img.rows;
int nY = img.cols;
CImage image;
Mat2CImage(&img, image);
CRect rect;//픽쳐 컨트롤의 크기를 저장할 CRect 객체
pictureControl2.GetWindowRect(rect);//GetWindowRect를 사용해서 픽쳐 컨트롤의 크기를 받는다.
CDC* dc; //픽쳐 컨트롤의 DC를 가져올 CDC 포인터
dc = pictureControl2.GetDC(); //픽쳐 컨트롤의 DC를 얻는다.
image.StretchBlt(dc->m_hDC, 0, 0, nX, nY, SRCCOPY);//이미지를 픽쳐 컨트롤 크기로 조정 rect.Width(), rect.Height()
ReleaseDC(dc);//DC 해제
}
Upvotes: 0
Views: 660
Reputation: 3401
You don't even attempt to stretch the image to the destination rectangle in this piece of code, you just draw it to a rectangle equal to the image dimensions, which is not a stretch. You get the destination rectangle, but do nothing with it.
You should better implement some "best-fit" algorithm, ie stretch the image to the maximum possible dimensions, occupying either the whole width or the whole height of the destination rectangle, while maintaining its original proportions, ie not distorting it. Here is some code:
CRect rect;
picturecontrol.GetClientRect(rect);
int nImgW = img.cols, nImgH = img.rows; // Original image dimensions
int nCtlW = rect.Width(), nCtlH = rect.Height(); // Destination control dimensions
int nW, nH; // Stretched (draw) dimensions
if (nImgW*nCtlH > nCtlW*nImgH)
{ // Image is proportionately wider than the control, fit it horizontally
nW = nCtlW;
nH = MulDiv(nW, nImgH, nImgW);
}
else
{ // Image is proportionately taller than the control, fit it vertically
nH = nCtlH;
nW = MulDiv(nH, nImgW, nImgH);
}
image.StretchBlt(dc->m_hDC, 0, 0, nW, nH, SRCCOPY);
To determine whether the image is proportionately wider or taller than the destination rectangle the condition originally was of course nImgW/nImgH > nCtlW/nCtlH
, but was changed to what is shown in the code above, so as to avoid possible inaccuracies due to integer division (it returns the integer quotient, rather than a rounded value, and has a smaller range too).
A side note, normally you should draw your images in response to the WM_PAINT
message (in the OnDraw()
, OnPaint()
or the controls' OnPaint()
events), using the CDC*
passed to the function or calling CPaintDC
. These call BeginPaint()/EndPaint()
instead of GetDC()/ReleaseDC()
- you must invalidate the control to trigger a repaint. For a static picture control you can call ctl.SetBitmap()
(this in any other point in your code, not while processing WM_PAINT
).
Upvotes: 1