加载图片

来源:互联网 发布:域名所有者查询 编辑:程序博客网 时间:2024/04/28 18:10

1.建立单文档工程

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/com/html/42e3cd0e-2413-494a-8be8-2952089e02d2.asp

/////////.bmp/////////

void CYXRMView::OnDraw(CDC* pDC)
{
 CYXRMDoc* pDoc = GetDocument();
 ASSERT_VALID(pDoc);
 // TODO: add draw code for native data here
HBITMAP hBitmap = (HBITMAP)LoadImage(AfxGetInstanceHandle(),"River Sumida.bmp",
IMAGE_BITMAP,0,0,LR_LOADFROMFILE|LR_CREATEDIBSECTION);
CBitmap m_bitmap;
m_bitmap.Attach(hBitmap);
BITMAP bm;
m_bitmap.GetBitmap(&bm);
CDC dcImage;
dcImage.CreateCompatibleDC(pDC);
dcImage.SelectObject(&m_bitmap);
CRect rect;
GetClientRect(rect);
pDC->BitBlt(0,0,rect.Width(),rect.Height(),&dcImage,0,0,SRCCOPY);
}

/////////.jpg/////////

void CYXRMView::OnDraw(CDC* pDC)
{
 CYXRMDoc* pDoc = GetDocument();
 ASSERT_VALID(pDoc);
 // TODO: add draw code for native data here
 IPicture *pPic;
 IStream *pStm;
 CRect rect;
 
 CFileStatus fstatus;
 CFile file;
 LONG cb;
 
 if (file.Open("11665_.jpg",CFile::modeRead)&&file.GetStatus("11665_.jpg",
  fstatus)&&
  ((cb = fstatus.m_size) != -1))
 {
  HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, cb);
  LPVOID pvData = NULL;
  if (hGlobal != NULL)
  {
   if ((pvData = GlobalLock(hGlobal)) != NULL)
   {
    file.ReadHuge(pvData, cb);
    GlobalUnlock(hGlobal);
    CreateStreamOnHGlobal(hGlobal, TRUE, &pStm);
    
    if(SUCCEEDED(OleLoadPicture(pStm,fstatus.m_size,TRUE,IID_IPicture,(LPVOID*)&pPic)))
    {
     OLE_XSIZE_HIMETRIC hmWidth;
     OLE_YSIZE_HIMETRIC hmHeight;
     
     pPic->get_Width(&hmWidth);
     pPic->get_Height(&hmHeight);
     
     ////////按窗口尺寸显示////////
     CRect rect; 
     GetClientRect(&rect);
     if(FAILED(pPic->Render(pDC->m_hDC,0,0,rect.Width(),rect.Height(),0,hmHeight,hmWidth,-hmHeight,NULL)))
      AfxMessageBox("Failed To Render The picture!");
     
     ////////原大显示//////
     /*CSize sz( hmWidth, hmHeight );
     pDC->HIMETRICtoDP(&sz);  // 转换 MM_HIMETRIC 模式单位为 MM_TEXT 像素单位
     if(FAILED(pPic->Render(pDC->m_hDC,0,0,sz.cx,sz.cy,0,hmHeight,hmWidth,-hmHeight,NULL)))
     AfxMessageBox("Failed To Render The picture!"); */
     pPic->Release();
    }
    else
     AfxMessageBox("Error Loading Picture From Stream!");
   }
  }
 }
 else
  AfxMessageBox("Can't Open Image File!");
}

原创粉丝点击