VC 6.0 使用GDI+类库进行图片处理

来源:互联网 发布:永久爱奇艺会员软件 编辑:程序博客网 时间:2024/06/07 16:59

读取图片长款的两种方法:

1.使用IPicture类读取图片

        IStream         *pStm;      CFileStatus   fstatus;      CFile             file;      LONG           cb;      LPVOID        pvData=NULL;      HGLOBAL      hGlobal;  long hr;if(file.Open(picName,CFile::modeRead)&&file.GetStatus(picName,fstatus)&&((cb=fstatus.m_size)!=-1))      {      hGlobal=GlobalAlloc(GMEM_MOVEABLE,cb);      if(hGlobal!=NULL)      {         if((pvData=GlobalLock(hGlobal))!=NULL)         {      file.ReadHuge(pvData,cb);      GlobalUnlock(hGlobal);      hr = CreateStreamOnHGlobal(hGlobal,TRUE,&pStm);         }    }    }else{return IMA_NOTFOUND;}        IPicture *pPic; hr = OleLoadPicture(pStm,fstatus.m_size,TRUE,IID_IPicture,(LPVOID*)&pPic);if(SUCCEEDED(hr))    {  OLE_XSIZE_HIMETRIC   hmWidth=0;      OLE_YSIZE_HIMETRIC   hmHeight=0;      pPic->get_Width(&hmWidth);      pPic->get_Height(&hmHeight);      CDC *p=CDC::FromHandle(::GetDC(NULL));  int lWidthPixels=MulDiv(hmWidth,p->GetDeviceCaps(LOGPIXELSX),2540);  int lHeightPixels=MulDiv(hmHeight,p->GetDeviceCaps(LOGPIXELSY),2540);   if( lHeightPixels != 672 || lWidthPixels != 1050){file.Close();GlobalFree(hGlobal);return IMA_PIXEL_ERROR;}}else{GlobalFree(hGlobal);file.Close();return IMA_LOADERROR;}file.Close();GlobalFree(hGlobal);return IMA_OK;

2. 使用GDI+类库

下载GDI+类库,在VC6.0的环境中点击Tools->Options...->Directories标签,在Include files下选择GDI+中的include文件夹,在library files下选择GDI+中的lib文件夹

                 

typedef unsigned __int32 ULONG_PTR;#include <gdiplus.h>using namespace Gdiplus; #pragma comment(lib, "gdiplus.lib")    CFileFind   find;      if (!find.FindFile(picName))    {        find.Close();        return IMA_NOTFOUND;    }    find.Close();    //init GDIGdiplus::GdiplusStartupInput gdiplusStartupInput;ULONG_PTR  gdiplusToken;GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);Image * psrcImg=NULL;       psrcImg = Image::FromFile(picName);if (!psrcImg || psrcImg->GetLastStatus() != Ok)    {        return IMA_LOADERROR;    }    int   lWidthPixels=psrcImg->GetWidth();       int   lHeightPixels=psrcImg->GetHeight(); if( lHeightPixels != 672 || lWidthPixels != 1050){return IMA_PIXEL_ERROR;}return IMA_OK;



原创粉丝点击