CXimage7.02的用法
版权声明:
本文为博主原创文章,转载请声明原文链接...谢谢。o_0。
更新时间:
2018-03-04 18:15:15
温馨提示:
学无止境,技术类文章有它的时效性,请留意文章更新时间,如发现内容有误请留言指出,防止别人"踩坑",我会及时更新文章
下载地址http://sourceforge.net/projects/cximage/
下载如果我是直接用vs2015编译的,编译里面的demo示例可以编译通过,并且会生成对应的静态库
创建一个新的简单的控制台项目来引用这些静态库,测试一些简单的功能
#include <stdio.h> #include <tchar.h> #include "ximage.h" #pragma comment(lib, "cximage.lib") #pragma comment(lib, "libdcr.lib") #pragma comment(lib, "png.lib") #pragma comment(lib, "tiff.lib") #pragma comment(lib, "jpeg.lib") #pragma comment(lib, "jasper.lib") #pragma comment(lib, "libpsd.lib") #pragma comment(lib, "jbig.lib") #pragma comment(lib, "mng.lib") #pragma comment(lib, "zlib.lib") int main() { CxImage image; image.Load(_T("home.png"), CXIMAGE_FORMAT_PNG); if (image.IsValid()) { if(!image.IsGrayScale()) { image.IncreaseBpp(24); } image.SetJpegQuality(80); image.Save(_T("home.jpg"), CXIMAGE_FORMAT_JPG); } image.Load(_T("home.png"), CXIMAGE_FORMAT_PNG); if (image.IsValid()) { image.Save(_T("home.tif"), CXIMAGE_FORMAT_PNG); image.Save(_T("home.ico"), CXIMAGE_FORMAT_ICO); } }
注意请自行设置项目的cximage的头文件和lib所在目录
CXimage支持的格式
CXIMAGE_FORMAT_BMP CXIMAGE_FORMAT_GIF CXIMAGE_FORMAT_JPG CXIMAGE_FORMAT_PNG CXIMAGE_FORMAT_MNG CXIMAGE_FORMAT_ICO CXIMAGE_FORMAT_TIF CXIMAGE_FORMAT_TGA CXIMAGE_FORMAT_PCX CXIMAGE_FORMAT_WBMP CXIMAGE_FORMAT_WMF CXIMAGE_FORMAT_JBG CXIMAGE_FORMAT_JP2 CXIMAGE_FORMAT_JPC CXIMAGE_FORMAT_PGX CXIMAGE_FORMAT_PNM CXIMAGE_FORMAT_RAS CXIMAGE_FORMAT_SKA CXIMAGE_FORMAT_RAW
取图像的一些信息
加载到内存后可以取图片的一些信息
CxImage image("1.jpg"); image.GetSize(); // 获取图像大小 image.getHeight(); // 获取图像高度(像素) image.getWidth(); // 获取图像宽度(像素) image.getNumColors(); // 获取图像颜色数 image.getBpp(); //获取图像位数 8位/16位/24位/32位 image.getType(); //获取图像类型
对图片的一些处理
图像重置大小并剪切
CxImage image("1.jpg"); image.Resample(96,96,2); image.Crop(letp,top,right,bottom);
图像旋转
image.Rotate(angle); image.RotateLeft(); image.RotateRight();
对背景透明处理
image.AlphaCreate(); image.SetTransColor(color); image.Save("2.png",CXIMAGE_FORMAT_PNG);
从资源中加载图像
//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)
解码内存中的图像
CxImage image((BYTE*)buffer,size,image_type); CxMemFile memfile((BYTE*)buffer,size); CxImage image(&memfile,image_type); 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); ... image.FreeMemory(buffer); CxMemFile memfile; memfile.Open(); image.Encode(&memfile,image_type); BYTE* buffer = memfile.GetBuffer(); long size = memfile.Size(); ... image.FreeMemory(buffer);
复制粘贴图像
//复制(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();
在图像控件中显示图像//复制(copy)
//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();
HBITMAP m_bitmap = NULL; CxImage image("myfile.png", CXIMAGE_FORMAT_PNG); ... CDC* hdc = m_picture.GetDC(); HBITMAP m_bitmap = image.MakeBitmap(hdc->m_hDC); HBITMAP hOldBmp = m_picture.SetBitmap(m_bitmap); if (hOldBmp) DeleteObject(hOldBmp); if (hdc->m_hDC) m_picture.ReleaseDC(hdc); ... if (m_bitmap) DeleteObject(m_bitmap);
图像灰度化
//图像灰度化 if (image.IsValid()) { int maxY = image.GetHeight(), int maxX = image.GetWidth(); int size = maxX*maxY; BYTE* bs = image.GetBits(); int w=((((24 * image.GetWidth()) + 31) / 32) * 4); // 4字节对齐 for (int j=0; j<maxY; j++) { for (int i=0; i<maxX; i++) { BYTE *b = bs+(image.GetHeight()-1-j)*w +i*3; int blue =*(b); int green =*(b+1); int red =*(b+2); int grayVal=(int)red*0.3+(int)green*0.59+(int)blue*0.11; CString csStr; csStr.Format("%d", grayVal); if(i >10 && i<100) { if(j == 50 || j== 100) { *b = 0; //blue *(b+1) = 0; // green *(b+2) = 255; // red } else { *b = grayVal; *(b+1) = grayVal; *(b+2) = grayVal; } } else { *b = grayVal; *(b+1) = grayVal; *(b+2) = grayVal; } } } }
创建一个多页的tif图
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); 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);
创建合并多个ico图标为一个图标组
//生成图标组 CxImageICO image1, image2; image1.Load(_T("home.ico"), CXIMAGE_FORMAT_ICO); image2.Load(_T("home32.ico"), CXIMAGE_FORMAT_ICO); CxImage *pimage[2]; pimage[0] = &image1; pimage[1] = &image2; FILE* hFile; hFile = fopen("multipage.ico", "w+b"); CxImageICO multiimage; multiimage.Encode(hFile, pimage, 2); fclose(hFile);
另一种生成方法直接加png图片不用生成各个尺寸的图标
CxImage image image.Load(_T("home.png")); CxImageICO img1, img2, img3; img1.Copy(image); img2.Copy(image); img3.Copy(image); img1.Resample(16, 16, 2); img2.Resample(32, 32, 2); img3.Resample(48, 48, 2); CxImage *pimage[3]; pimage[0] = &img1; pimage[1] = &img2; pimage[2] = &img3; FILE* hFile; hFile = fopen("multipage.ico", "w+b"); CxImageICO multiimage; multiimage.Encode(hFile, pimage, 3); fclose(hFile);