1、首先从此处下载源代码
http://www.codeproject.com/KB/graphics/cximage.aspx
2、然后将里面的工程全部编译一下,我觉得应该是生成对应的库。
3、然后用vc建个工程,同时将源代码里面除了Demo这些文件夹,全部考到新建工程的同一目录。
4、接下来就是最麻烦的配置了:
|- 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)
配置lib库:
Project->Setting->link选项卡:
在“对象/库模块”下添加
../png/Debug/png.lib ../jpeg/Debug/jpeg.lib ../zlib/Debug/zlib.lib ../tiff/Debug/tiff.lib ../jbig/Debug/jbig.lib ../jasper/Debug/jasper.lib ../cximage/Debug/cximage.lib
。
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/skyair624/archive/2008/10/01/3008014.aspx
在c/c++的附加包含路径里面填上:../CxImage(我也不知道这是干什么的)
5、添加头文件
#include "../CxImage/ximage.h" #include "../CxImage/ximacfg.h"
6、关键代码:
CxImage image; image.Load(_T("abc.bmp"), CXIMAGE_FORMAT_BMP); if (image.IsValid()){ cout << "ok" << endl; image.Save(_T("image.jpg"), CXIMAGE_FORMAT_JPG);// 把压缩后的图像以jpg文件类型保存起来。 }
首先在工程目录下你得放一个abc.bmp的图片,可以用下面这个代码产生:
void CapScreen(char filename[]) { CDC *pDC; pDC = CDC::FromHandle(GetDC(GetDesktopWindow())); if(pDC == NULL) return; int BitPerPixel = pDC->GetDeviceCaps(BITSPIXEL); int Width = pDC->GetDeviceCaps(HORZRES); int Height = pDC->GetDeviceCaps(VERTRES); CDC memDC; if(memDC.CreateCompatibleDC(pDC) == 0) return; CBitmap memBitmap, *oldmemBitmap; if(memBitmap.CreateCompatibleBitmap(pDC, Width, Height) == NULL) return; oldmemBitmap = memDC.SelectObject(&memBitmap); if(oldmemBitmap == NULL) return; if(memDC.BitBlt(0, 0, Width, Height, pDC, 0, 0, SRCCOPY) == 0) return; BITMAP bmp; memBitmap.GetBitmap(&bmp); FILE *fp = fopen(filename, "w+b"); BITMAPINFOHEADER bih = {0}; bih.biBitCount = bmp.bmBitsPixel; bih.biCompression = BI_RGB; bih.biHeight = bmp.bmHeight; bih.biPlanes = 1; bih.biSize = sizeof(BITMAPINFOHEADER); bih.biSizeImage = bmp.bmWidthBytes * bmp.bmHeight; bih.biWidth = bmp.bmWidth; BITMAPFILEHEADER bfh = {0}; bfh.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER); bfh.bfSize = bfh.bfOffBits + bmp.bmWidthBytes * bmp.bmHeight; bfh.bfType = (WORD)0x4d42; fwrite(&bfh, 1, sizeof(BITMAPFILEHEADER), fp); fwrite(&bih, 1, sizeof(BITMAPINFOHEADER), fp); BYTE * p = new BYTE[bmp.bmWidthBytes * bmp.bmHeight]; GetDIBits(memDC.m_hDC, (HBITMAP) memBitmap.m_hObject, 0, Height, p, (LPBITMAPINFO) &bih, DIB_RGB_COLORS); fwrite(p, 1, bmp.bmWidthBytes * bmp.bmHeight, fp); delete [] p; fclose(fp); memDC.SelectObject(oldmemBitmap); }
7、最后运行就可以了
我的可以将5M的bmp文件压缩成168K的jpeg
8、note:
image.Load(_T("image.bmp"), CXIMAGE_FORMAT_BMP); 一定要这样用,否则会报错的,这是我在
http://topic.csdn.net/u/20081214/21/D012F730-C165-4A4C-9B01-3E449C7CB48B.html发现的。
可能显得很笨拙,折腾我3个小时,跟大家分享一下吧!干软件的要团结!