使用CxImage进行图形和格式转换(CBitmap to jpg or png or gif or bmp)
2014-05-06 18:59
435 查看
CxImage类库介绍
CxImage类库是一个几乎可以管理所有的图象文件的C++类库。它可以快捷地存取、显示、转换各种图像。
其他的图形库?有那么多优秀的图形库,如OpenIL,FreeImage,PaintLib等等,它们是功能强大,齐全,而且是经常更新的。然而,如果你要使用他们,你可能会遇到一些麻烦,,因为它们大部分是平台无关的,用C语言写成,有的还夹杂着基本的C++ wrapper和大量的编译选项的声明需要你去处理。现在,一个新的GDI+类库来了,或许CxImage不是最有用的,但是至少你是有源代码的。这个类库不是MFC类库,完全是一个windows类库,因为一些特殊的构造和绘图函数,但是基础是平台无关的。
一.在你的程序中使用CxImage(Using MFC in Shared DLL + Debug 模式)
1.下载CxImage类库
地址:http://www.xdp.it/download.htm
http://www.codeproject.com/bitmap/cximage.asp
下载Download full source files (full) - 2.10 Mb
2.解压压缩包,并编译Demo2/Console.dsw,这个过程可能比较慢,中间文件为60M
3.编译完成后搜索*.lib文件,一共搜索出10个文件,把它们复制到你的工程目录中(任意)
它们分别为:CxImage.lib Jpeg.lib png.lib Tiff.lib jbig.lib zlib.lib j2k.lib cximagecrtd.lib(最后一个为静态MFC库时使用)
4.找到压缩包中的CxImage文件夹,把它里面的所有.h头文件,复制到你的工程目录中(任意)
5.在StdAfx.h文件中,添加这些文件
// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//
#if !defined(AFX_STDAFX_H__84F27293_E63B_443C_BA35_5322307D48DA__INCLUDED_)
#define AFX_STDAFX_H__84F27293_E63B_443C_BA35_5322307D48DA__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#define VC_EXTRALEAN // Exclude rarely-used stuff from Windows headers
#include <afxwin> // MFC core and standard components
#include <afxext> // MFC extensions
#include <afxdisp> // MFC Automation classes
#include <afxdtctl> // MFC support for Internet Explorer 4 Common Controls
#ifndef _AFX_NO_AFXCMN_SUPPORT
#include <afxcmn> // MFC support for Windows Common Controls
#endif // _AFX_NO_AFXCMN_SUPPORT
//添加CxImage到你的程序中(目录为你上面添加位置,我把它们放在了lib文件夹中了)
#include "lib/ximage.h"
//#pragma comment(lib,"lib/cximagecrtd.lib") //静态链接库时使用
#pragma comment(lib,"lib/cximage.lib")
#pragma comment(lib,"lib/Jpeg.lib")
#pragma comment(lib,"lib/consoled.lib")
#pragma comment(lib,"lib/png.lib")
#pragma comment(lib,"lib/zlib.lib")
#pragma comment(lib,"lib/tiff.lib")
#pragma comment(lib,"lib/jasper.lib")
#pragma comment(lib,"lib/j2k.lib")
#pragma comment(lib,"lib/jbig.lib")
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_STDAFX_H__84F27293_E63B_443C_BA35_5322307D48DA__INCLUDED_)
6.这样就可以在你的程序中使用CxImage类了
7.演示程序
void CTestDlg::OnButton1()
{
// TODO: Add your control notification handler code here
//取得你的图片资源文件IDB_BITMAP1
HBITMAP bitmap = ::LoadBitmap(AfxGetInstanceHandle(),
MAKEINTRESOURCE(IDB_BITMAP1));
//创建对象
CxImage *newImage = new CxImage();
newImage->CreateFromHBITMAP(bitmap);
//存储为文件,我发现的可以使用的有6个格式有三个格式未使用成功(gif,wbmg,wmf)....够用了吧?
newImage->Save("image.bmp",CXIMAGE_FORMAT_BMP); //注意不是CXIMAGE_SUPPORT_BMP
newImage->Save("image.tga",CXIMAGE_FORMAT_TGA);
newImage->Save("image.pcx",CXIMAGE_FORMAT_PCX);
newImage->Save("image.jpg",CXIMAGE_FORMAT_JPG);
newImage->Save("image.png",CXIMAGE_FORMAT_PNG);
newImage->Save("image.tiff",CXIMAGE_FORMAT_TIF);
newImage->Save("image.gif",CXIMAGE_FORMAT_GIF); //不成功,只能读取第一贞图片?
newImage->Save("image.wbmp",CXIMAGE_FORMAT_WBMP); //不成功
newImage->Save("image.wmf",CXIMAGE_FORMAT_WMF); //不成功
newImage->Save("image.mng",CXIMAGE_FORMAT_MNG); //默认-未打开此项功能
newImage->Save("image.jbig",CXIMAGE_FORMAT_JBG); //默认-未打开此项功能
/*
* CxImage配置文件ximacfg.h
* #define CXIMAGE_SUPPORT_BMP 1 //可用状态(即不可取得和转换成这种格式)
* #define CXIMAGE_SUPPORT_MNG 0 //不可用状态
*/
}
其他说明
1.所支持的格式(in Doc)
formats #define required libraries size [Kbyte] BMP
GIF
ICO
TGA
PCX
WBMP
WMF CXIMAGE_SUPPORT_BMP
CXIMAGE_SUPPORT_GIF
CXIMAGE_SUPPORT_ICO
CXIMAGE_SUPPORT_TGA
CXIMAGE_SUPPORT_PCX
CXIMAGE_SUPPORT_WBMP
CXIMAGE_SUPPORT_WMF
built in
24 JPEG CXIMAGE_SUPPORT_JPG
jpeg
88 PNG CXIMAGE_SUPPORT_PNG
png, zlib
104 MNG CXIMAGE_SUPPORT_MNG
mng, zlib, jpeg
148 TIFF CXIMAGE_SUPPORT_TIF
tiff, zlib, jpeg
124 JBIG CXIMAGE_SUPPORT_JBG
jbig
28 PNM,PPM,PGM
RAS CXIMAGE_SUPPORT_PNM
CXIMAGE_SUPPORT_RAS
jasper
176 JPEG-2000 CXIMAGE_SUPPORT_JP2
CXIMAGE_SUPPORT_JPC
CXIMAGE_SUPPORT_PGX
jasper
176
2.格式转换示例
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);
}
3.使用CxImage(In Doc),至今未弄懂....也没按这个弄好过...不知道为什么
Project Settings
|- C/C++
| |- Code Generation
| | |- Use run-time library : Multithreaded DLL (must be the same for
| | | all the linked libraries)
| | |- Struct member alignment : must be the same for all the linked
| | | libraries
| |- Precompiled headers : not using precompiled headers
| |- Preprocessor
| |- Additional Include Directories: ..cximage
|- 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(Using MFC in a Static Library + Debug 模式)
1.解压压缩包,打开Demo2/Console.dsw,设置如下:
Project -> Setting -> Using MFC in a Static Library(注意是所有的项目都设置成这个)
2.确保c/c++ -> Code Generation -> Using run-time library 为 Mutithreaded
3.Debug模式下编译,搜索*.lib文件,复制到工程目录,包含进工程(当前工程也必须是Using MFC in a Static Library + Debug 模式),即可,包含方法同上
注意:生成的LIB文件,比上面要少一个....不知道为什么...少了consoled.lib,但并无大碍,不包含它即可
三.使用CxImage(Using MFC in a Static Library + Realese 模式)
1.解压压缩包,打开Demo2/Console.dsw,设置如下:
Project -> Setting -> Using MFC in a Static Library(注意是所有的项目都设置成这个)
2.确保c/c++ -> Code Generation -> Using run-time library 为 Mutithreaded
3.Realese模式下编译,搜索*.lib文件,复制到工程目录,包含进工程(当前工程也必须是Using MFC in a Static Library + Realese模式),即可,包含方法同上
注意:生成的LIB文件,同样是少一个,不包含它即可
参考:
Open the CxImage workspace with all the libraries. Select Project->Properties. Select each project in the list and make sure they all have the same settings for the MFC library (Not Using MFC for a static library). Then select the C/C++ tab and select Code
Generation in the combo box. In the Use Run-time Library combo box, make sure all of them are set to Single-Threaded (or multi threaded depending on your settings). Then remake all the libraries and try to recompile your project
Thanks Kelly. It works! Thanks again!:) It turns out that CxImage static lib projects, in my case, are not set uniformly the same; some uses MFC shared library while some uses MFC static library
CxImage类库是一个几乎可以管理所有的图象文件的C++类库。它可以快捷地存取、显示、转换各种图像。
其他的图形库?有那么多优秀的图形库,如OpenIL,FreeImage,PaintLib等等,它们是功能强大,齐全,而且是经常更新的。然而,如果你要使用他们,你可能会遇到一些麻烦,,因为它们大部分是平台无关的,用C语言写成,有的还夹杂着基本的C++ wrapper和大量的编译选项的声明需要你去处理。现在,一个新的GDI+类库来了,或许CxImage不是最有用的,但是至少你是有源代码的。这个类库不是MFC类库,完全是一个windows类库,因为一些特殊的构造和绘图函数,但是基础是平台无关的。
一.在你的程序中使用CxImage(Using MFC in Shared DLL + Debug 模式)
1.下载CxImage类库
地址:http://www.xdp.it/download.htm
http://www.codeproject.com/bitmap/cximage.asp
下载Download full source files (full) - 2.10 Mb
2.解压压缩包,并编译Demo2/Console.dsw,这个过程可能比较慢,中间文件为60M
3.编译完成后搜索*.lib文件,一共搜索出10个文件,把它们复制到你的工程目录中(任意)
它们分别为:CxImage.lib Jpeg.lib png.lib Tiff.lib jbig.lib zlib.lib j2k.lib cximagecrtd.lib(最后一个为静态MFC库时使用)
4.找到压缩包中的CxImage文件夹,把它里面的所有.h头文件,复制到你的工程目录中(任意)
5.在StdAfx.h文件中,添加这些文件
// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//
#if !defined(AFX_STDAFX_H__84F27293_E63B_443C_BA35_5322307D48DA__INCLUDED_)
#define AFX_STDAFX_H__84F27293_E63B_443C_BA35_5322307D48DA__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#define VC_EXTRALEAN // Exclude rarely-used stuff from Windows headers
#include <afxwin> // MFC core and standard components
#include <afxext> // MFC extensions
#include <afxdisp> // MFC Automation classes
#include <afxdtctl> // MFC support for Internet Explorer 4 Common Controls
#ifndef _AFX_NO_AFXCMN_SUPPORT
#include <afxcmn> // MFC support for Windows Common Controls
#endif // _AFX_NO_AFXCMN_SUPPORT
//添加CxImage到你的程序中(目录为你上面添加位置,我把它们放在了lib文件夹中了)
#include "lib/ximage.h"
//#pragma comment(lib,"lib/cximagecrtd.lib") //静态链接库时使用
#pragma comment(lib,"lib/cximage.lib")
#pragma comment(lib,"lib/Jpeg.lib")
#pragma comment(lib,"lib/consoled.lib")
#pragma comment(lib,"lib/png.lib")
#pragma comment(lib,"lib/zlib.lib")
#pragma comment(lib,"lib/tiff.lib")
#pragma comment(lib,"lib/jasper.lib")
#pragma comment(lib,"lib/j2k.lib")
#pragma comment(lib,"lib/jbig.lib")
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_STDAFX_H__84F27293_E63B_443C_BA35_5322307D48DA__INCLUDED_)
6.这样就可以在你的程序中使用CxImage类了
7.演示程序
void CTestDlg::OnButton1()
{
// TODO: Add your control notification handler code here
//取得你的图片资源文件IDB_BITMAP1
HBITMAP bitmap = ::LoadBitmap(AfxGetInstanceHandle(),
MAKEINTRESOURCE(IDB_BITMAP1));
//创建对象
CxImage *newImage = new CxImage();
newImage->CreateFromHBITMAP(bitmap);
//存储为文件,我发现的可以使用的有6个格式有三个格式未使用成功(gif,wbmg,wmf)....够用了吧?
newImage->Save("image.bmp",CXIMAGE_FORMAT_BMP); //注意不是CXIMAGE_SUPPORT_BMP
newImage->Save("image.tga",CXIMAGE_FORMAT_TGA);
newImage->Save("image.pcx",CXIMAGE_FORMAT_PCX);
newImage->Save("image.jpg",CXIMAGE_FORMAT_JPG);
newImage->Save("image.png",CXIMAGE_FORMAT_PNG);
newImage->Save("image.tiff",CXIMAGE_FORMAT_TIF);
newImage->Save("image.gif",CXIMAGE_FORMAT_GIF); //不成功,只能读取第一贞图片?
newImage->Save("image.wbmp",CXIMAGE_FORMAT_WBMP); //不成功
newImage->Save("image.wmf",CXIMAGE_FORMAT_WMF); //不成功
newImage->Save("image.mng",CXIMAGE_FORMAT_MNG); //默认-未打开此项功能
newImage->Save("image.jbig",CXIMAGE_FORMAT_JBG); //默认-未打开此项功能
/*
* CxImage配置文件ximacfg.h
* #define CXIMAGE_SUPPORT_BMP 1 //可用状态(即不可取得和转换成这种格式)
* #define CXIMAGE_SUPPORT_MNG 0 //不可用状态
*/
}
其他说明
1.所支持的格式(in Doc)
formats #define required libraries size [Kbyte] BMP
GIF
ICO
TGA
PCX
WBMP
WMF CXIMAGE_SUPPORT_BMP
CXIMAGE_SUPPORT_GIF
CXIMAGE_SUPPORT_ICO
CXIMAGE_SUPPORT_TGA
CXIMAGE_SUPPORT_PCX
CXIMAGE_SUPPORT_WBMP
CXIMAGE_SUPPORT_WMF
built in
24 JPEG CXIMAGE_SUPPORT_JPG
jpeg
88 PNG CXIMAGE_SUPPORT_PNG
png, zlib
104 MNG CXIMAGE_SUPPORT_MNG
mng, zlib, jpeg
148 TIFF CXIMAGE_SUPPORT_TIF
tiff, zlib, jpeg
124 JBIG CXIMAGE_SUPPORT_JBG
jbig
28 PNM,PPM,PGM
RAS CXIMAGE_SUPPORT_PNM
CXIMAGE_SUPPORT_RAS
jasper
176 JPEG-2000 CXIMAGE_SUPPORT_JP2
CXIMAGE_SUPPORT_JPC
CXIMAGE_SUPPORT_PGX
jasper
176
2.格式转换示例
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);
}
3.使用CxImage(In Doc),至今未弄懂....也没按这个弄好过...不知道为什么
Project Settings
|- C/C++
| |- Code Generation
| | |- Use run-time library : Multithreaded DLL (must be the same for
| | | all the linked libraries)
| | |- Struct member alignment : must be the same for all the linked
| | | libraries
| |- Precompiled headers : not using precompiled headers
| |- Preprocessor
| |- Additional Include Directories: ..cximage
|- 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(Using MFC in a Static Library + Debug 模式)
1.解压压缩包,打开Demo2/Console.dsw,设置如下:
Project -> Setting -> Using MFC in a Static Library(注意是所有的项目都设置成这个)
2.确保c/c++ -> Code Generation -> Using run-time library 为 Mutithreaded
3.Debug模式下编译,搜索*.lib文件,复制到工程目录,包含进工程(当前工程也必须是Using MFC in a Static Library + Debug 模式),即可,包含方法同上
注意:生成的LIB文件,比上面要少一个....不知道为什么...少了consoled.lib,但并无大碍,不包含它即可
三.使用CxImage(Using MFC in a Static Library + Realese 模式)
1.解压压缩包,打开Demo2/Console.dsw,设置如下:
Project -> Setting -> Using MFC in a Static Library(注意是所有的项目都设置成这个)
2.确保c/c++ -> Code Generation -> Using run-time library 为 Mutithreaded
3.Realese模式下编译,搜索*.lib文件,复制到工程目录,包含进工程(当前工程也必须是Using MFC in a Static Library + Realese模式),即可,包含方法同上
注意:生成的LIB文件,同样是少一个,不包含它即可
参考:
Open the CxImage workspace with all the libraries. Select Project->Properties. Select each project in the list and make sure they all have the same settings for the MFC library (Not Using MFC for a static library). Then select the C/C++ tab and select Code
Generation in the combo box. In the Use Run-time Library combo box, make sure all of them are set to Single-Threaded (or multi threaded depending on your settings). Then remake all the libraries and try to recompile your project
Thanks Kelly. It works! Thanks again!:) It turns out that CxImage static lib projects, in my case, are not set uniformly the same; some uses MFC shared library while some uses MFC static library
相关文章推荐
- 使用CxImage进行图形和格式转换(CBitmap to jpg or png or gif or bmp)
- java将图片缩放实现类(能将jpg、bmp、png、gif图片文件,进行等比或非等比的大小转换)
- C#使用GDAL将tif图像转换为jpg、bmp、png和gif格式的图像
- 使用系统imgdecmp库解码JPG,GIF,PNG,BMP图片
- vc6.0使用gdi+在内存中绘图并将其保存为bmp,jpg,gif,png等格式的图片
- java png gif bmp to jpg
- 一次导出Word中的所有图形,可以是JPG,BMP,PNG,GIF,EMF,TIF格式
- java 将jpg、bmp、png、gif图片文件,进行等比或非等比的大小转换
- vc6.0使用gdi+在内存中绘图并将其保存为bmp,jpg,gif,png等格式的图片
- 数字图像处理 CImage类的使用与封装(jpg png gif tif bmp等格式图像的加载、数据读写、保存等功能)
- wince中显示BMP、JPG、Gif以及PNG的方法
- [转载]C#图片格式(JPG,BMP,PNG,GIF)等转换为ICO图标
- C# 判断图形文件(GIF,JPG,PNG)的图片格式的方法
- png,bmp,jpg,gif格式图像的区别
- 利用FSO取得BMP,JPG,PNG,GIF文件信息
- C++中使用CAtlHttpClient显示网络图片的方法,支持png,bmp,gif图片格式
- 图片:jpg png gif bmp 区别(四)
- 获取图片宽度和高度的类,支持JPG,GIF,PNG,BMP'
- 常见图片格式简单分析总结--JPG、PNG、GIF、BMP
- JPG png GIF BMP图片格式的区别