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