VC下显示JPG,BMP,GIF等图像格式(转载)

方法一:用COM组件的 IPicture接口(VC)

EVC下显示图片

用COM组件的 IPicture接口(VC)

在OnPaint   或 OnDraw中加入下代码即可 (注意 屏蔽掉 CXXDialog::OnPaint CXX::OnDraw)

dc.SetBkMode( TRANSPARENT );

CFile mFile; 
LONG nLength;

//1 打开文件并获得文件的真实大小
if ( mFile.Open( _T( "./res/test.jpg" ),CFile::modeRead )
   &&( ( nLength = mFile.GetLength() ) > 0 ) ) 
{ 
   //2 从堆中分配指定数量字节的一整块,这时系统无法提供零零碎碎的局部或全局的堆
   HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, nLength ); 
   LPVOID pvData = NULL; 
   if ( hGlobal != NULL ) 
   { 
    //3 锁定全局内存对象并返回它的首地址
    if ( ( pvData = GlobalLock(hGlobal) ) != NULL ) 
    { 
     //4 把文件内容读进全局内存对象的地址空间中
     mFile.Read( pvData, nLength );
     mFile.Close();

     //5 GlobalUnlock函数把以GMEM_MOVEABLE方式分配的内存对象的锁定计数器减1
     GlobalUnlock( hGlobal );

     //6 从全局内存中创建stream对象,第二个参数指定当stream释放时是否自动释放全局内存
     IStream *pStm;
     CreateStreamOnHGlobal( hGlobal, TRUE, &pStm) ;

     //7 创建一个新的picture对象并用stream对象中的内容初始化
     IPicture *pPic; 
     if( SUCCEEDED( OleLoadPicture( pStm, nLength, TRUE, IID_IPicture, (LPVOID*)&pPic ) ) ) 
     { 
      //8 释放不要的stream对象并清空stream指针
      pStm->Release();
      pStm=NULL;
     
      OLE_XSIZE_HIMETRIC mWidth; 
      OLE_YSIZE_HIMETRIC mHeight;

      //9 得到picture对象中图片的宽和高
      pPic->get_Width( &mWidth ); 
      pPic->get_Height( &mHeight );

      CRect rc;
      GetClientRect( &rc );
      double fX,fY;

      //10 GetDeviceCaps(HORZRES)得到屏幕的宽度(单位:像素)
      //    GetDeviceCaps(HORZSIZE)得到屏幕的宽度(单位:毫米)
      fX = (double) dc.GetDeviceCaps(HORZRES) * (double)mWidth / ( (double)dc.GetDeviceCaps( HORZSIZE ) * 100.0 ); 
      fY = (double) dc.GetDeviceCaps(VERTRES) * (double)mHeight / ( (double)dc.GetDeviceCaps( VERTSIZE ) *100.0 );

     
      
      //11 把图像显示在dc中

      if( FAILED( pPic->Render( dc.GetSafeHdc(), 0, 0, (DWORD)fX , (DWORD)fY , 0, mHeight, mWidth, -mHeight, NULL ) ) ) 
       AfxMessageBox( _T("渲染图片失败") );

      /*根据背景大小缩放图片
      CRect rcClient;
      GetClientRect( &rcClient );
      
      BOOL bWidth = rcClient.Width() / fX > rcClient.Height() / fY;
      if ( bWidth )
      {
       if( FAILED( pPic->Render( dc.GetSafeHdc(), 0, 0, (DWORD)rcClient.Width() , (DWORD)rcClient.Width() * fY / fX , 0, mHeight, mWidth, -mHeight, NULL ) ) ) 
        AfxMessageBox( _T("渲染图片失败") );
      }
      else
      {
       if( FAILED( pPic->Render( dc.GetSafeHdc(), 0, 0, (DWORD)rcClient.Height() * fX/ fY, (DWORD)rcClient.Height() , 0, mHeight, mWidth, -mHeight, NULL ) ) ) 
        AfxMessageBox( _T("渲染图片失败") );
      }
      */
      
      //12 释放不要的picture对象,并把指针清空
      pPic->Release(); 
      pPic=NULL;

      //13 释放不要的全局内存对象,这个千万别忘了(32位程序不需要这行,系统会自动释放;16位程序一定要)
      FreeResource(hGlobal);

     }
     else
      AfxMessageBox( _T("从stream中装载图片失败") );

    } 
   }
   else
    AfxMessageBox( _T("分配内存失败") );

else
   AfxMessageBox( _T("打开文件失败") );

//-------------------------------------------------------------------------------------------------------------------------------------------------------------

方法二:用GDI+(VC)

 

添加Gdiplus.lib到工程中

头文件中添加

#include "Gdiplus.h" 
using namespace Gdiplus;


添加类成员变量

GdiplusStartupInput m_gdiPlusInPut;
ULONG_PTR m_gdiPlusToken;

构造函数中初始化GDI+

GdiplusStartup( &m_gdiPlusToken, &m_gdiPlusInPut, NULL );


析构函数中 //销毁GDI+

GdiplusShutdown(gdiplusToken);

OnPaint()中

//CDialog::OnPaint();
CPaintDC dc( this );

//建立图形对象 
Graphics mGraphics( dc.GetSafeHdc() );

//装入图像文件

Image img( L"./res/test.jpg", TRUE );


 

//在指定区域pdestPoints显示图像

/*//根据背景大小按比例缩放

CRect rcClient;
GetClientRect( &rcClient );


BOOL bWidth = rcClient.Width() / img.GetWidth() > rcClient.Height() / img.GetHeight();
if ( bWidth )
{
   mGraphics.DrawImage( &img, 0, 0, rcClient.Width(), rcClient.Width() * img.GetHeight() / img.GetWidth() );
}
else
{
   mGraphics.DrawImage(&img, 0, 0, rcClient.Height() * img.GetWidth() / img.GetHeight(), rcClient.Height() );
}

//原始大小
mGraphics.DrawImage(&img, 0, 0, img.GetWidth(), img.GetHeight() );

 

EVC下显示图片:

void CXXDlg::OnPaint() 
{
CPaintDC dc(this); // device context for painting

dc.SetBkMode( TRANSPARENT );
CBitmap mBitmap;
mBitmap.Attach( SHLoadImageFile( _T( "./player.jpg" ) ) );

BITMAP mInfo;
mBitmap.GetBitmap( &mInfo );

CDC mPicDC;
mPicDC.CreateCompatibleDC( &dc );
CBitmap* pOldBitmap = mPicDC.SelectObject( &mBitmap );
dc.BitBlt( 0, 0, mInfo.bmWidth, mInfo.bmHeight, &mPicDC, 0, 0, SRCCOPY );

mPicDC.SelectObject( pOldBitmap );
mBitmap.DeleteObject();
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值