MFC/VC CxImage簡単な構成と使用(完全版)

15315 ワード

                                 :



http://blog.csdn.net/afterwards_/article/details/7997385               .



CxImage           ,   PC    GUI   ,                 ,          Windows    GDI            ,  png  !  mfc    CImage        ,  ,CImage png            ,            ,CImage              。



  ,            ,         png  ,   png    1  2   ,        ,      ….>_<



      GDIPLUS(   GDI+),     PNG   ,           ,  ,  CPU         (  ,                       ,           ,    CPU        )。



         CxImage,                CxImage(                png  ,      ,   ^0^1,  CxImage,   http://www.xdp.it/cximage/600/cximage600_full.7z   ,      6.00  。(ps       ,        ,   6.0  ,           7.0,           6.0   )



2,     ,         (    VS2005,  VC6      ),          CxImage                  ,           Debug        lib,      demo   ,               ,  demo       lib     。



3,    lib        ,   E:\libs\png.lib jpeg.lib zlib.libtiff.lib jbig.lib jasper.lib mng.lib cximage.lib,         cximage   ,                 ,    CxImage   , ximacfg.h            ,1   ,0    。    PNG             ,  cximage.lib     1.57M,     ,   2M 。     ,        png  ,         zlib.lib png.lib。



4,        lib,      



  ->  ->       ->VC++  



   :  :   E:\lib\



    :  :(your CxImagedir)\CxImage



        :



  ->  ->    ->   ->  ->     :  :



cximage.lib jpeg.lib png.lib tiff.libj2k.lib jasper.lib jbig.lib zlib.lib



   ->  ->    ->  



MFC  :   DLL   MFC



   :        



   ->  ->    ->C/C++->/MD

      : 16  



    :       



       ,        CxImage ,       :



#include “ximage.h”



CxImage image;



CString filename("c:\\1.png");

image.Load(filename,CXIMAGE_FORMAT_PNG);



image.Draw(hdc,0,0);



 



 



 。CxImage    



      CxImage                 。



CxImage               。        、  、      。       ,          , OpenIL,FreeImage,PaintLib  ,         ,  ,         。    ,            ,      ,                。    ,CxImage        。  ,        ,        。             ,  C    ,          C++ wrapper                 。 CxImage          。        ,            。              GDI+  ,                    ,            。



CxImage       MFC  C++ ,    ,  ,  ,           ,  BMP, JPEG, GIF, PNG, TIFF, MNG, ICO, PCX, TGA,WMF, WBMP, JBG, J2K       。    BMP<->JPG,PNG <>TIFF       。



              ,                 ,       。



     :Davide Pizzolato ,  : http://www.xdp.it/



  , http://www.codeproject.com/bitmap/CXImage.asp        Demo  .



 :  Codeproject     ,       ,              ,       ,          CodeProject      ,      .  :



License

The class CxImage is free; as for the TIFF, JPEG, PNGand ZLIB libraries : "If you use this source code in a product,acknowledgment is not required but would be appreciated."



CxImage is open source and licensedunder the zlib license . In anutshell, this means that you can use the code however you wish, as long as youdon't claim it as your own.



      codeproject   ,     ,  ,            --DavidePizzolato      



            ,         .



 



 。  CxImage  



                  CxImgLib.dsw(VC++6.0),           。        :  CxImage     。                ,      ,          ximcfg.h                    。JPG、PNG、TIFF      ,          100KB   。 CxImage        60KB。  ,                 。



 



 //      :ximacfg.h

#define CXIMAGE_SUPPORT_JPG 1

//     bmp->jpg      

#define CXIMAGE_SUPPORT_BMP 1

#define CXIMAGE_SUPPORT_GIF 1

#define CXIMAGE_SUPPORT_JPG 1

//       ,     ............

#define CXIMAGE_SUPPORT_PNG 0//    

#define CXIMAGE_SUPPORT_MNG 0

#define CXIMAGE_SUPPORT_ICO 1

#define CXIMAGE_SUPPORT_TIF 0//

#define CXIMAGE_SUPPORT_TGA 0//

#define CXIMAGE_SUPPORT_PCX 0//

#define CXIMAGE_SUPPORT_WBMP 0//

#define CXIMAGE_SUPPORT_WMF 0//

#define CXIMAGE_SUPPORT_J2K 0  // Beta, use JP2

#define CXIMAGE_SUPPORT_JBG 0



//.............





        



              ,     :



           :



·  CxImage : cximage.lib - staticlibrary



·  CxImageCrtDll : cximagecrt.dll - DLLnot using mfc



·  CxImageMfcDll : cximage.dll - DLLusing mfc



·  Demo : demo.exe - program linkedwith cximage.lib and the C libraries



·  DemoDll : demodll.exe - programlinked withcximagecrt.dll



·  j2k,jasper,jbig,jpeg,png,tiff,zlib : static Clibraries



                (      60MB)。



 



 。      CxImage          



   VC         ,          (ProjectSettings):



|- C/C++

|   |- Code Generation

|   |   |- Use run-time library : Multithreaded DLL (mustbe the same for 

|   |   |  all the linked libraries)  //        DLL  ,DEBUG   

|   |   |- Struct member alignment : must be the same forall the linked libraries

|   |- Precompiled headers : not using precompiled headers

|   |- Preprocessor

|       |- Additional Include Directories: ..\cximage(   CxImage  .h .cpp                ,       include            , include       )

|- Link

    |- General

        |- Object/library modules: png.lib 

                                                      jpeg.lib 

                                                      zlib.lib 

                                                      tiff.lib 

                                                      jasper.lib 

                                                     cximage.lib  (    lib   CxImage        lib       )



    CxImage  xfile.h、ximacfg.h、ximadef.h、ximage.cpp、ximage.h、xiofile.h、 xmemfile.cpp、xmemfile.h           CxImage.h         。     vc6 "tools"  "include"  .



                  :



1.                   (convert from a format to another)



 CxImage  image;   //     CxImage  



//  bmp     jpg  (bmp -> jpg)

image.Load("image.bmp", CXIMAGE_FORMAT_BMP);   //   bmp  ,        

    //      bmp      。

if (image.IsValid())......{

       // Returns true if the image has 256 colors  and a linear grey scale palette.

    if(!image.IsGrayScale()) image.IncreaseBpp(24);   // param nbit: 4, 8, 24

    image.SetJpegQuality(99);                //            ( 0 100,    ,    )

    image.Save("image.jpg",CXIMAGE_FORMAT_JPG);          //         jpg        。

}



 



//  png     tif  (png -> tif)

image.Load("image.png", CXIMAGE_FORMAT_PNG);

if (image.IsValid())...{

    image.Save("image.tif",CXIMAGE_FORMAT_TIF);

}



 



2。        (load an image resource)



            CxImage  ,       :



 



// Load the resource IDR_PNG1 from the PNG resource type

CxImage* newImage = new CxImage();

newImage->LoadResource(FindResource(NULL,MAKEINTRESOURCE(IDR_PNG1),

                       "PNG"),CXIMAGE_FORMAT_PNG);



 



  



 



//Load the resource IDR_JPG1 from DLL

CxImage* newImage = new CxImage();

HINSTANCE hdll=LoadLibrary("imagelib.dll");

if (hdll)...{

    HRSRC hres=FindResource(hdll,MAKEINTRESOURCE(IDR_JPG1),"JPG");

    newImage->LoadResource(hres,CXIMAGE_FORMAT_JPG,hdll);

    FreeLibrary(hdll);

}



 



  



 



//Load a bitmap resource;

HBITMAP bitmap = ::LoadBitmap(AfxGetInstanceHandle(),

                              MAKEINTRESOURCE(IDB_BITMAP1)));

CxImage *newImage = new CxImage();

newImage->CreateFromHBITMAP(bitmap);



 



3。             



(1)              Image  (decode animage from memory)



       :

------



CxImage image((BYTE*)buffer,size,image_type);//     buffer       Image  



// :



CxMemFile memfile((BYTE*)buffer,size); //     CxMemFile  

CxImage image(&memfile,image_type);



//



CxMemFile memfile((BYTE*)buffer,size);

CxImage* image = new CxImage();

image->Decode(&memfile,type);

 



============2) Image          (encode animage in memory)



--------



long size=0;//      

BYTE* buffer=0;//         

image.Encode(buffer,size,image_type);// image       type    copy buffer

...

free(buffer);



 :



CxMemFile memfile;            //     CxMemFile  

memfile.Open();

image.Encode(&memfile,image_type);

BYTE* buffer = memfile.GetBuffer();

long size = memfile.Size();

...

free(buffer);

 



---------------



4。           (copy/paste an image)



 



//copy(    )

HANDLE hDIB = image->CopyToHandle();

if (::OpenClipboard(AfxGetApp()->m_pMainWnd->GetSafeHwnd())) ...{

    if(::EmptyClipboard()) ...{

        if (::SetClipboardData(CF_DIB,hDIB) == NULL ) ...{

            AfxMessageBox( "Unable to set Clipboard data" );

}    }    }

CloseClipboard();



//paste(        )

HANDLE hBitmap=NULL;

CxImage *newima = new CxImage();

if (OpenClipboard()) hBitmap=GetClipboardData(CF_DIB);

if (hBitmap) newima->CreateFromHANDLE(hBitmap);

CloseClipboard();

 



5。 picture box     png     



 



HBITMAP m_bitmap = NULL;

CxImage image("myfile.png", CXIMAGE_FORMAT_PNG);

...

m_bitmap = image.MakeBitmap(m_picture.GetDC()->m_hDC);

m_picture.SetBitmap(m_bitmap);

...

if (m_bitmap) DeleteObject(m_bitmap);



 





 。  



   CxImage           。                           。  CxImage  (  )     。                    。CxImage::pDib       ,CxImage::pAlpha      ,CxImage::pSelection        ,                   。            ,           ,        CxImage::pLayers 。    ,       CxImage  。  ,            。   CxImage        :



 



class CxImage

...{

...

protected:

void* pDib;            //

BITMAPINFOHEADER head; //      (  )

CXIMAGEINFO info;      //      

BYTE* pSelection;      //       

BYTE* pAlpha;          //alpha  

CxImage** pLayers;     //   

}

typedef struct tagCxImageInfo ...{

DWORD   dwEffWidth;       //DWORD     

BYTE*   pImage;           //    

void*   pGhost;           //if this is a ghost, pGhost point to the body

DWORD   dwType;           //      

char    szLastError[256]; //    

long    nProgress;        //       

long    nEscape;          //    

long    nBkgndIndex;      //GIF, PNG, MNG    

RGBQUAD nBkgndColor;      //RGB      

BYTE    nQuality;         //JPEG    

long    nFrame;           //TIF, GIF, MNG   :     

long    nNumFrames;       //TIF, GIF, MNG   :   

DWORD   dwFrameDelay;     //GIF, MNG  

long    xDPI;             //     

long    yDPI;             //     

RECT    rSelectionBox;    //      

BYTE    nAlphaMax;        //         

bool    bAlphaPaletteEnabled;  //       Alpha     

bool    bEnabled;         //      

long    xOffset;

long    yOffset;

DWORD   dwEncodeOption;   //      

RGBQUAD last_c;           //      

BYTE    last_c_index;

bool    last_c_isvalid;

long    nNumLayers;

DWORD   dwFlags;

} CXIMAGEINFO;

  CxImage    : DavidePizzolato,       。1984     ,                 。    Askoll      。



  CxImage       ,              。



  :http://www.codeproject.com/bitmap/cximage.asp



  :http://www.xdp.it/