目录

1. 自适应方法

    /* 自适应方法       */
    CRect rect;
    CRect rect1;

    CImage image; //创建图片类  
    image.Load(picname); //根据图片路径加载图片   


    //获取Picture Control控件的大小
    GetDlgItem(IDC_PICTURE)->GetWindowRect(&rect);//将窗口矩形选中到picture控件上  
    ScreenToClient(&rect);//将客户区选中到Picture控件表示的矩形区域内  
    GetDlgItem(IDC_PICTURE)->GetClientRect(&rect);

    CWnd *pWnd=GetDlgItem(IDC_PICTURE);//获得pictrue控件窗口的句柄  
    pWnd->GetClientRect(&rect);//获取句柄指向控件区域的大小  

    CDC *pDC=pWnd->GetDC();//获得pictrue控件的DC  

    SetStretchBltMode(pDC->m_hDC,STRETCH_HALFTONE);
    if(image.GetWidth()<=rect.Width() && image.GetHeight()<=rect.Width()) //图片小于控件区域,不缩放
    {
        rect1 = CRect(rect.TopLeft(), CSize(image.GetWidth(),image.GetHeight()));
        image.StretchBlt(pDC->m_hDC,rect1,SRCCOPY);//将图片画到Picture控件表示的矩形区域 
    }
    else
    {
        float xScale=(float)rect.Width()/(float)image.GetWidth();
        float yScale=(float)rect.Height()/(float)image.GetHeight();
        float ScaleIndex=(xScale<=yScale? xScale:yScale);
        rect1 = CRect(rect.TopLeft(), CSize((int)image.GetWidth()*ScaleIndex,(int)image.GetHeight()*ScaleIndex));
        image.StretchBlt(pDC->m_hDC,rect1,SRCCOPY);//将图片画到Picture控件表示的矩形区域
    }
    image.Draw(pDC->m_hDC, rect);//将图片绘制到picture表示的区域内  
    ReleaseDC(pDC);

2. 加载原图方法

/* 加载原图的方法*/
    CImage image; //创建图片类  
    image.Load(picname); //根据图片路径加载图片  

    CRect rect;//定义矩形类  
    int cx = image.GetWidth();//获取图片宽度  
    int cy = image.GetHeight();//获取图片高度  

    GetDlgItem(IDC_PICTURE)->GetWindowRect(&rect);//将窗口矩形选中到picture控件上  
    ScreenToClient(&rect);//将客户区选中到Picture控件表示的矩形区域内  
    GetDlgItem(IDC_PICTURE)->MoveWindow(rect.left, rect.top, cx, cy, TRUE);//将窗口移动到Picture控件表示的矩形区域  

    CWnd *pWnd=GetDlgItem(IDC_PICTURE);//获得pictrue控件窗口的句柄  

    pWnd->GetClientRect(&rect);//获得pictrue控件所在的矩形区域  

    CDC *pDC=pWnd->GetDC();//获得pictrue控件的DC  

    image.Draw(pDC->m_hDC, rect); //将图片画到Picture控件表示的矩形区域  
    ReleaseDC(pDC);//释放picture控件的DC  
扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。