rainbowzc

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: :: :: 管理 ::

将bmp,png gif,jpg 格式之间的相互转换,有很多类库可以使用...jpeglib,Image, CxImage 等等....

其他我没用过,我只用过CxImage....感觉还不错...首先到

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

下载Download full source files - 2.10 Mb   .

需要使用CxImage的时候#include  "ximage.h"

你的库是不是把JPG给关掉了?
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
其他的可以不打开
那么 可以不导入 相应的 png.lib和tiff.lib
从CxImage中将xfile.h、ximacfg.h、ximadef.h、ximage.cpp、ximage.h、xiofile.h、xmemfile.cpp、xmemfile.h拷贝到工程文件夹下并将这些文件加入工程,然后将CxImage各文件夹下Debug文件夹中的lib文件也拷贝到工程下,并在VC中做如下设置

Project Settings
|- C/C++
|   |- Code Generation
|   |   |- Use run-time library : Multithreaded DLL (must be the same for
|   |   |  all the linked libraries)  //应该只要是多线程DLL即可,DEBUG的也行
|   |   |- Struct member alignment : must be the same for all 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/Debug/png.lib
                                   ../jpeg/Debug/jpeg.lib
                                   ../zlib/Debug/zlib.lib
                                   ../tiff/Debug/tiff.lib
                                   ../jasper/Debug/jasper.lib
                                    ../cximage/Debug/cximage.lib  (前面的路径设为从CxImage中拷贝到工程中的lib文件所在的目录)

要在picture box中显示一个png格式的文件,只需:
CxImage image("myfile.png", CXIMAGE_FORMAT_PNG);
HBITMAP m_bitmap = image.MakeBitmap(m_picture.GetDC()->m_hDC);
m_picture.SetBitmap(m_bitmap);
其它格式则类推。
Examples: how to ...
... convert from a format to another
CxImage  image;
// bmp -> jpg
image.Load("image.bmp", CXIMAGE_FORMAT_BMP);
if (image.IsValid()){
if(!image.IsGrayScale()) image.IncreaseBpp(24);
image.SetJpegQuality(99);
image.Save("image.jpg",CXIMAGE_FORMAT_JPG);
}
// png -> tif
image.Load("image.png", CXIMAGE_FORMAT_PNG);
if (image.IsValid()){
image.Save("image.tif",CXIMAGE_FORMAT_TIF);
}
... load an image resource
//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);
... decode an image from memory
CxImage image((BYTE*)buffer,size,image_type);
orCxMemFile memfile((BYTE*)buffer,size);
CxImage image(&memfile,image_type);
orCxMemFile memfile((BYTE*)buffer,size);
CxImage* image = new CxImage();
image->Decode(&memfile,type);
... encode an image in memory
long size=0;
BYTE* buffer=0;
image.Encode(buffer,size,image_type);
...
free(buffer);
orCxMemFile memfile;
memfile.Open();
image.Encode(&memfile,image_type);
BYTE* buffer = memfile.GetBuffer();
long size = memfile.Size();
...
free(buffer);
... create a multipage TIFF
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);
orFILE* 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/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();
需要大家注意的是:整个CxImage类库非常大。如果你只需要能处理其中的几种格式,你可以在主要的头文件ximage.h中找到一些开关选项来关闭一些图像库。JPG、PNG、TIFF中的每一个库,都会向最终程序增加约100KB的内容。而CxImage类库压缩后只有约60KB。所以,你需要谨慎挑选一些你真正需要的类库。作者提供的示例工程在编译后,你会发现如下一些文件: ·CxImage : cximage.lib - static library ·CxImageCrtDll : cximagecrt.dll - DLL not using mfc ·CxImageMfcDll : cximage.dll - DLL using mfc ·Demo : demo.exe - program linked with cximage.lib and the C libraries ·DemoDll : demodll.exe - program linked with cximagecrt.dll ·j2k,jasper,jbig,jpeg,png,tiff,zlib : static C libraries 构建这些工程需要耗费几分钟的时间(中间文件可达60MB)。

 

Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1414164

 
posted on 2006-11-29 19:32  ct  阅读(331)  评论(0编辑  收藏  举报