FreeImage通用文件加载与转换器

在上篇中我谈到FreeImage可以用于通用图像的加载与显示,这里继续下面的操作,将上面载入的图像转换成比较常用的格式,FreeImage提供了常用的bmp,jpeg,tiff等格式,稍微修改一下上一篇中的代码,将上一篇中OnDraw定义的局部变量FIBITMAP* dib;转而定义成视类的成员变量。

在构造函数中设置  :dib = 0;这样FreeImage_Unload(dib)的操作就不应该在OnDraw函数中调用,而应在析构函数中调用。

以上设置完成后,在菜单栏:另存为……,添加命令响应函数,添加如下代码:

void CGenricImageTransformerView::OnFileSaveAs() { // TODO: Add your command handler code here CFileDialog fileDlg(FALSE,".bmp","img.*",OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT, "Bmp(*.bmp)|*.bmp|Jpg(*.jpg)|*.jpg|/ Tif(*.tif)|*.tif|Any(*.*)|*.*||", NULL); if(IDOK == fileDlg.DoModal()) { CString strFileExt = fileDlg.GetFileExt(); // strFileExt.MakeLower(); CString filename = fileDlg.GetFileName(); FREE_IMAGE_FORMAT fif; int nType; if(!strFileExt.Compare("bmp")) nType = 1; if(!strFileExt.Compare("jpg")) nType = 2; if (!strFileExt.Compare("tif")) nType = 3; switch(nType) { case 1: fif = FIF_BMP; break; case 2: fif = FIF_JPEG; break; case 3: fif = FIF_TIFF; break; default: fif = FIF_UNKNOWN; } FreeImage_Save(fif,dib,filename,0); } }

这样你的程序就可以很容易的载入图像,并将其转换为常用的格式了。

 

或者:利用FreeImage中给出的一个例程,利用图像保存函数:

BOOL CGenricImageTransformerView::GenericWriter(FIBITMAP *dib, const char *filename) { FREE_IMAGE_FORMAT fif = FIF_UNKNOWN; BOOL bSuccess = FALSE; fif = FreeImage_GetFIFFromFilename(filename); if(fif != FIF_UNKNOWN) { BOOL bCanSave; FREE_IMAGE_TYPE imgType = FreeImage_GetImageType(dib); if(imgType == FIT_BITMAP) { WORD bpp = FreeImage_GetBPP(dib); bCanSave = (FreeImage_FIFSupportsWriting(fif) && FreeImage_FIFSupportsExportBPP(fif,bpp)); } else { bCanSave = FreeImage_FIFSupportsExportType(fif,imgType); } if(bCanSave) { bSuccess = FreeImage_Save(fif,dib,filename); } } return bSuccess; }

再调用上面的函数,思路要清晰很多:

void CGenricImageTransformerView::OnFileSaveAs() { // TODO: Add your command handler code here CFileDialog fileDlg(FALSE,".bmp","img.*",OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT, "bmp(*.bmp)|*.bmp|jpg(*.jpg)|*.jpg|/ tif(*.tif)|*.tif|png(*.png)|*.png|/ Any(*.*)|*.*||", NULL); if(IDOK == fileDlg.DoModal()) { CString filename = fileDlg.GetFileName(); BOOL bSuccess = GenericWriter(dib,filename); if(!bSuccess) MessageBox("Not support that type!"); } }

 

 

你可能感兴趣的:(image,command,null,tiff)