经验证,可靠实用。 //通过资源画图 static void DrawBitmap(UINT bitmap,CDC *pDC,CRect rect) { CBitmap bmp; if (bmp.LoadBitmap(bitmap)) { BITMAP bmpInfo; bmp.GetBitmap(&bmpInfo); CDC dcMemory; dcMemory.CreateCompatibleDC(pDC); dcMemory.SelectObject(&bmp); pDC->SetStretchBltMode(HALFTONE); pDC->StretchBlt(rect.left,rect.top,rect.Width(),rect.Height(),&dcMemory,0,0,bmpInfo.bmWidth,bmpInfo.bmHeight,SRCCOPY); } } //通过BMP文件画图 static void drawBitmapFormFile(CString BitmapFilePath,CDC *pDC,CRect rect) { HANDLE filehandle=::LoadImage(NULL,BitmapFilePath,IMAGE_BITMAP,0,0,LR_LOADFROMFILE); if(filehandle!=NULL) { CBitmap bmp; if(bmp.Attach(filehandle)) { BITMAP bmpInfo; bmp.GetBitmap(&bmpInfo); CDC dcMemory; dcMemory.CreateCompatibleDC(pDC); dcMemory.SelectObject(&bmp); pDC->SetStretchBltMode(HALFTONE); pDC->StretchBlt(rect.left,rect.top,rect.Width(),rect.Height(),&dcMemory,0,0,bmpInfo.bmWidth,bmpInfo.bmHeight,SRCCOPY); bmp.Detach(); } } } BOOL CShowBitmapView::ShowJpgGif(CDC* pDC,CString strPath, int x, int y)//读取文件信息方式打开JPG等图片文件 { IStream *pStm; CFileStatus fstatus; CFile file; LONG cb; //打开文件并检测文件的有效性 if (file.Open(strPath,CFile::modeRead)&& file.GetStatus(strPath,fstatus)&& ((cb = fstatus.m_size) != -1)) { HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, cb); LPVOID pvData = NULL; if (hGlobal != NULL) { pvData = GlobalLock(hGlobal); if (pvData != NULL) { //file.ReadHuge(pvData, cb); //6.0中可能是用这个函数 file.Read(pvData, cb); //VC2005.NET中用这个函数 GlobalUnlock(hGlobal); CreateStreamOnHGlobal(hGlobal, TRUE, &pStm); } } } else { return false; } //打开文件结束 //显示JPEG和GIF格式的图片,GIF只能显示一帧,还不能显示动画, //要显示动画GIF请使用ACTIVE控件。 IPicture *pPic; //load image from file stream 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); double fX,fY; //get image height and width fX = (double)pDC->GetDeviceCaps(HORZRES)*(double)hmWidth/((double)pDC->GetDeviceCaps(HORZSIZE)*100.0); fY = (double)pDC->GetDeviceCaps(VERTRES)*(double)hmHeight/((double)pDC->GetDeviceCaps(VERTSIZE)*100.0); //use render function display image if(FAILED(pPic->Render(*pDC,x,y,(DWORD)fX,(DWORD)fY,0,hmHeight,hmWidth,-hmHeight,NULL))) { pPic->Release(); return false; } pPic->Release(); } else { return false; } return true; }