gdi

找了一些有关gdi的一些代码 其中加载图片的方式都是用LoadBitmap从资源里加载,我想从一个外部文件加载该如何实现呢?

解决方案 »

  1.   

    改下下面代码中的路径,即可从你制定的路径载入JPG,JIF格式的图片 
    IPicture   *pPicture;//声明OLE提供的图像对象
      IStream   *pStrm;//声明一个流对象   
            CFileStatus   fstatus;   
            CFile   file;   
            LONG   cb;   
            if((file.Open("D:/sunset.jpg",CFile::modeRead))&&(file.GetStatus("D:/sunset.jpg",fstatus))&&((cb=fstatus.m_size)!=-1))   
            {   
                  HGLOBAL   hGlobal=GlobalAlloc(GMEM_MOVEABLE,cb);   
                  LPVOID   pvData=NULL;   
                  CDC   *pDC;   
                  pDC=(this->GetActiveWindow())->GetDC();//得到活动视图的设备上下文   
                  if(hGlobal!=NULL)   
                  {   
                        if((pvData=GlobalLock(hGlobal))!=NULL)   
                        {   
                              file.Read(pvData,cb);//将图像文件数据读入内存   
                              GlobalUnlock(hGlobal);   
                              CreateStreamOnHGlobal(hGlobal,TRUE,&pStrm);//创建流对象   
                              if(SUCCEEDED(OleLoadPicture(pStrm,fstatus.m_size,TRUE,IID_IPicture,(LPVOID*)&pPicture)))   //根据流对象pStrm初始化图像对象pPicture   
                              {   
                                    OLE_XSIZE_HIMETRIC   Width;   
                                    OLE_YSIZE_HIMETRIC   Height;   
                                    pPicture->get_Width(&Width);   
                                    pPicture->get_Height(&Height);   
                                    //得到图像的宽、高尺寸,注意该尺寸的逻辑单位不是图像的像素   
                                    if(FAILED(pPicture->Render(*pDC,100,80,300,230,0,Height,Width,-Height,NULL)))
                                        //在初始点(100,80)宽为200、高为150的矩形中显示图形   
                                    AfxMessageBox("Failed   to   render   the   picture"); 
                                    pPicture->Release();   
                              }   
                              else   
                                    AfxMessageBox("Error   loadimg   picture   from   stream!");   
                        }   
                  }   
            }   
            else   
                  AfxMessageBox("Can   open   image   file!");