MFC中显示图片

// TODO:  在此添加控件通知处理程序代码
    CString strFilePath = "E:\\JnPlatformV2.1\\bin\\Debug\\0.jpg";
    CImage image;
    image.Load(strFilePath);

    //以下两个矩形主要作用是,获取对话框上面的Picture Control的width和height,
    //并设置到图片矩形rectPicture,根据图片矩形rectPicture对图片进行处理,
    //最后绘制图片到对话框上Picture Control上面
    CRect rectControl;                        //控件矩形对象
    CRect rectPicture;                        //图片矩形对象


    int x = image.GetWidth();
    int y = image.GetHeight();
    //Picture Control的ID为IDC_IMAGE
    CWnd  *pWnd = GetDlgItem(IDC_STATIC_HWD);
    pWnd->GetClientRect(rectControl);


    CDC *pDc = GetDlgItem(IDC_STATIC_HWD)->GetDC();
    SetStretchBltMode(pDc->m_hDC, STRETCH_HALFTONE);

    rectPicture = CRect(rectControl.TopLeft(), CSize((int)rectControl.Width(), (int)rectControl.Height()));

    ((CStatic*)GetDlgItem(IDC_STATIC_HWD))->SetBitmap(NULL);

    //以下两种方法都可绘制图片
    //image.StretchBlt(pDc->m_hDC, rectPicture, SRCCOPY); //将图片绘制到Picture控件表示的矩形区域
    image.Draw(pDc->m_hDC, rectPicture);                //将图片绘制到Picture控件表示的矩形区域

    image.Destroy();
    pWnd->ReleaseDC(pDc);

 

你可能感兴趣的:(MFC学习)