Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

How to fix resized image in MFC?

Hi, it was ok when run following code to paint Mat on Device Contro/DC/CDC:

Visual Studio 2010, MFC, OpenCV 247

image description

void CMainDlg::OnPaint()
{
    CRect rc; GetClientRect(&rc);
    CClientDC dc(this);

    Mat mat; mat=imread("aaa.bmp");
    int x=mat.cols;
    int y=mat.rows;

    // Create BitmapInfo
    BITMAPINFO bmi;
    BITMAPINFOHEADER* pheader= &bmi.bmiHeader;
    pheader->biSize         = sizeof( BITMAPINFOHEADER );
    pheader->biPlanes       = 1;
    pheader->biCompression   = BI_RGB;
    pheader->biXPelsPerMeter = 100;
    pheader->biYPelsPerMeter = 100;
    pheader->biClrUsed      = 0;
    pheader->biClrImportant  = 0;
    pheader->biWidth    = x;
    pheader->biHeight   = -y;
    pheader->biBitCount = 24;
    bmi.bmiHeader.biSizeImage = 0;

    // Stretch Image
    StretchDIBits( dc.GetSafeHdc()
                ,0,0,rc.Width(), rc.Height()
                ,0,0,x,y,
                mat.data,&bmi,DIB_RGB_COLORS,SRCCOPY);

    // Compare
    imshow("CV imshow()",mat);
}

in a MFC dialog(CDialogEx), but when I resize the mat like this

void CMainDlg::OnPaint()
{
    CRect rc; GetClientRect(&rc);
    CClientDC dc(this);

    Mat mat; mat=imread("aaa.bmp");
    resize(mat,mat,cv::Size(rc.Width(),rc.Height()));
    int x=mat.cols;
    int y=mat.rows;
    // ...some code...
}

the image got in shift!? why??

Resoult:

image description

There's same case in:

  1. Move mat to global area.

  2. paint in other function(out of OnPaint())