Platform: Windows / Linux
License: Freeware
Release: 5.51
Last update: 19 Jan 2003
History: cximage_history.htm
Article: http://www.codeproject.com/bitmap/cximage.asp
Documentation: CxImage
Class Members & Operations [^]
| Download: | |
CxImage is a C++ class to manage virtually any
kind of images. It can load, save, display, transform images in a very simple
and fast way.
Around there are many good libraries (OpenIL, FreeImage, PaintLib ...), these are powerful, complete, and constantly updated. However if you ever try to use them, you may find some difficulties; because these libraries are mainly platform independent, written in C, sometimes with a basic C++ wrapper and with tons of compiler switch declarations. Now with the new GDI+ classes on the scene, CxImage isn't so useful, but at least here you have the source code.
No. Altogether it's a windows library, because of some particular constructors and the painting functions, but the backbone is platform independent.
Easy: PNG and MNG! The library contains LZW encoders/decoders.
Due to Unisys patent enforcement, you should be aware of the limitations.
The TIFF library is also affected by this. With CxImage it's
a simple operation to remove a specific format, you are free to make your
choice.
With the release 5.00, CxImage uses RLE compression as default for GIF images,
it is less efficient but it's free. CxImage can also read and write truecolor
images with the GIF format, it is a useless option, because the result is
larger than a BMP, just try it if you are curious, IE5 loads correctly this
format.

I'm sorry, but each JPG, PNG and TIFF library adds about 100KB
to the final application. CxImage impact is less than 50KB. So
you should support and link only the formats that your application really
needs. These are the weights of the switches:
| Option | Size [Kbyte] |
| CxImage core (all switches off) | 20 |
| CXIMAGE_SUPPORT_ALPHA + CXIMAGE_SUPPORT_SELECTION + CXIMAGE_SUPPORT_TRANSFORMATION | 20 |
| CXIMAGE_SUPPORT_DSP | 16 |
| CXIMAGE_SUPPORT_WINDOWS | 8 |
| Built in codecs (BMP, GIF, ICO, TGA, PCX, WBMP, WMF) | 24 |
| JPEG | 88 |
| PNG | 104 |
| TIFF | 124 |
Maybe. CxImage has been designed to be easily extended,
but CxImage first should be stable enough to guarantee a good
base class for the other formats. In any case, the actual formats cover the
99% of the normal demand.
Starting form my CxDib class, that implements memory
DIBs only, I tried to add some members to read images from files. Looking
for a solution, I found a nice MFC class named CImage on the
net, release 1.4 (1998). CImage supports BMP, GIF, PNG and JPG,
but suffers many little bugs and uses a complex class structure, so I decided
to strip it to the base and merge CxDib with the CImage
philosophy, to obtain the new CxImage class. Also I updated the
libraries for JPG, PNG and ZLIB.
With CxImage is very easy to add new image types, so I added
the TIFF library (rev. 6) and a minimal support for ICONs, MNG,
TGA and PCX. Finally I added some specific functions to obtain an image from
global HANDLEs (windows clipboard) and objects (windows resources).
With the release 5, CxImage has now a good support for memory files, new methods
and file formats, and it is more portable: it works also with WinCE and Linux.
CTwain code; to Abe
for multi page tiffs code; to Chris
Shearer Cooper for memory file suggestions and code; to Brent
Corkum for BCMenu code.More specific credits and disclaimers are in every header file of each library.
You must compile all the libraries before you can link the demo
application. The whole operation is quite simple: open the CxImgLib.dsw
workspace, select the menu "Build/Batch Build..." and click the "Build" button.
![]() |
![]() |
This will need some minutes to complete (the intermediate files
occupy 60MB!). When everything is done, select the demo project and launch
the application.
The demo application shows how to:
//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);or
//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);
}
or
//Load a bitmap resource; HBITMAP bitmap = ::LoadBitmap(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDB_BITMAP1))); CxImage *newImage = new CxImage();
newImage->CreateFromHBITMAP(bitmap);
CxImage image((BYTE*)buffer,size,image_type);or
CxMemFile memfile((BYTE*)buffer,size); CxImage image(&memfile,image_type);or
CxMemFile memfile((BYTE*)buffer,size); CxImage* image = new CxImage(); image->Decode(&memfile,type);
long size=0; BYTE* buffer=0; image.Encode(buffer,size,image_type); ... free(buffer);or
CxMemFile memfile; memfile.Open(); image.Encode(&memfile,image_type); BYTE* buffer = memfile.GetBuffer(); long size = memfile.Size(); ... free(buffer);
CxImage *pimage[3];
pimage[0]=&image1;
pimage[1]=&image2;
pimage[2]=&image3;
FILE* hFile;
hFile = fopen("multipage.tif","w+b");
CxImageTIF multiimage;
multiimage.Encode(hFile,pimage,3);
fclose(hFile);
or
FILE* hFile;
hFile = fopen("c:\\multi.tif","w+b");
CxImageTIF image;
image.Load("c:\\1.tif",CXIMAGE_FORMAT_TIF);
image.Encode(hFile,true);
image.Load("c:\\2.bmp",CXIMAGE_FORMAT_BMP);
image.Encode(hFile,true);
image.Load("c:\\3.png",CXIMAGE_FORMAT_PNG);
image.Encode(hFile);
fclose(hFile);
//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();
CxImage image("myfile.png", CXIMAGE_FORMAT_PNG);
HBITMAP m_bitmap = image.MakeBitmap(m_picture.GetDC()->m_hDC);
m_picture.SetBitmap(m_bitmap);
Win95,WinNT, Win98, WinME, W2K, WinXP, WinCE, Linux = Yes
For any questions, e-mail to: ing.davide.pizzolato@libero.it