破玩意(一)利用GDAL实现图像格式转换
2013-09-12 22:02
387 查看
最近磁盘空间不足,就整理一下资料,看到一些早期的代码,自己写的工具,尘封很久了,其中绝大部分对于我来说没用了,放上来可能有些人会有用吧?
用C#本身就可以做图像格式转换,但是这种方式存在一个问题,就是和内存相关,图像太大,使得内存不足,操作失败,如果做过图像拼接的话,就应该遇到过这个问题,这时可以用GDAL的栅格化处理,或者它配套的一些exe。这里说的格式转换,可以用到GDAL第二种方式——translate.exe,程序目录如下:
gdal18.dll、msvcp100.dll和msvcr100.dll是translate运行必备的dll,其中后两个在很多系统里面已经有了,考虑到没有的情况,故把它们拷贝到程序目录。一般的操作,我们在命令行窗口输入指令,调用translate.exe执行转换就行了,像这样。注意:貌似文件要和程序同一目录(忘了,看代码像是这样)
考虑到实现批量操作的情况,自己用C#封装了一下,代码如下:
最后,附上用C#自带图像转换方式的代码,有兴趣的人可以比较一下:
最最后附上源码,早期写的代码不规范,有兴趣看的将就一下。
用C#本身就可以做图像格式转换,但是这种方式存在一个问题,就是和内存相关,图像太大,使得内存不足,操作失败,如果做过图像拼接的话,就应该遇到过这个问题,这时可以用GDAL的栅格化处理,或者它配套的一些exe。这里说的格式转换,可以用到GDAL第二种方式——translate.exe,程序目录如下:
gdal18.dll、msvcp100.dll和msvcr100.dll是translate运行必备的dll,其中后两个在很多系统里面已经有了,考虑到没有的情况,故把它们拷贝到程序目录。一般的操作,我们在命令行窗口输入指令,调用translate.exe执行转换就行了,像这样。注意:貌似文件要和程序同一目录(忘了,看代码像是这样)
translate before.jpg after.png
考虑到实现批量操作的情况,自己用C#封装了一下,代码如下:
private void ImageWork(string fileName, string saveName, string fileExtension, string pixFormat, float ResolutionH, float ResolutionV) { try { string tempSouceName = "temp" + Path.GetExtension(fileName); saveName = Path.ChangeExtension(saveName, fileExtension); string tempDestName = "temp" + Path.GetExtension(saveName); File.Copy(fileName, tempSouceName, true); ProcessStartInfo psi = new ProcessStartInfo(); psi.UseShellExecute = false; psi.FileName = System.Windows.Forms.Application.StartupPath + "\\translate.exe"; psi.Arguments = string.Format(" {0} {1}", tempSouceName, tempDestName); psi.CreateNoWindow = true; psi.RedirectStandardInput = true; psi.RedirectStandardOutput = false; psi.RedirectStandardError = true; Process p = Process.Start(psi); //Process p = Process.Start("gdal_translate.exe", string.Format("{0} {1}", tempSouceName, tempDestName)); //while (!p.HasExited) //{ // System.Threading.Thread.Sleep(100); //} p.WaitForExit(); p.Close(); File.Copy(tempDestName, saveName, true); } catch (Exception ee) { ShowExceptionEvent(string.Format("【异常】{0},出现在:{1}", ee.Message, fileName)); } }
最后,附上用C#自带图像转换方式的代码,有兴趣的人可以比较一下:
/// <summary> /// C#自带图像转换,不足,大图像处理会出现内存不足 /// </summary> /// <param name="fileName">输入文件名</param> /// <param name="saveName">保存文件名</param> /// <param name="fileExtension">扩展名</param> /// <param name="pixFormat">像素格式</param> /// <param name="resolutionH">水平分辨率</param> /// <param name="resolutionV">垂直分辨率</param> public void ImageExculpate(string fileName, string saveName, string fileExtension, string pixFormat, float resolutionH, float resolutionV) { try { if (!File.Exists(fileName) || File.Exists(saveName)) return; Point PT = new Point(0, 0); ImageFormat imgFormat = null; PixelFormat pixelFormat; switch (fileExtension) { case ".bmp": imgFormat = ImageFormat.Bmp; break; case ".gif": imgFormat = ImageFormat.Gif; break; case ".jpeg": imgFormat = ImageFormat.Jpeg; break; case ".jpg": imgFormat = ImageFormat.Jpeg; break; case ".png": imgFormat = ImageFormat.Png; break; case ".tiff": imgFormat = ImageFormat.Tiff; break; case ".tif": imgFormat = ImageFormat.Tiff; break; default: break; } switch (pixFormat) { case "4": pixelFormat = PixelFormat.Format4bppIndexed; break; case "8": pixelFormat = PixelFormat.Format8bppIndexed; break; case "16": pixelFormat = PixelFormat.Format16bppRgb555; break; case "24": pixelFormat = PixelFormat.Format24bppRgb; break; case "32": pixelFormat = PixelFormat.Format32bppRgb; break; case "48": pixelFormat = PixelFormat.Format48bppRgb; break; default: pixelFormat = PixelFormat.Format16bppRgb555; break; } Bitmap bmpIn = new Bitmap(fileName); bmpIn.SetResolution(resolutionH, resolutionV); //新图 Bitmap bmpOut = new Bitmap(bmpIn.Width, bmpIn.Height, pixelFormat); bmpOut.SetResolution(resolutionH, resolutionV); Graphics g; //保存图 g = Graphics.FromImage(bmpOut); g.DrawImage(bmpIn, PT); saveName = Path.ChangeExtension(saveName, fileExtension); bmpOut.Save(saveName, imgFormat); g.Dispose(); bmpOut.Dispose(); } catch (Exception ee) { throw ee; } }
最最后附上源码,早期写的代码不规范,有兴趣看的将就一下。
相关文章推荐
- 利用OpenOffice.org和Swftools进行实现—doc-->pdf-->swf--格式的转换
- VC实现多格式图像的转换
- 利用ffmpeg实现视频格式转换成flv,保证能成功运行
- winform利用itextsharp.dll实现图片文件转换PDF格式文件
- Hadoop中利用自身类实现json格式转换为对象。
- 利用openOffice实现各种文档转换成pdf格式
- Java平台要实现类似豆丁百度文科的文档在线阅读,总体思路是讲doc docx等文件格式利用jcom转换成pdf再用swftools转为swf。再用flexpaper组件显示swf。
- C#使用GDAL将tif图像转换为jpg、bmp、png和gif格式的图像
- Java实现GDAL读取TIF并转换为OpenCV(Mat格式)
- Java平台要实现类似豆丁百度文科的文档在线阅读,总体思路是讲doc docx等文件格式利用jcom转换成pdf再用swftools转为swf。再用flexpaper组件显示swf。
- VC中实现多格式图像的灵活转换与字符串之间转换
- VC实现多格式图像的转换
- 使用IStream和GDI+在内存中实现图像格式转换
- 利用 libiconv 实现汉字编码 utf-8 格式 和 gbk格式的相互转换
- 利用cvtColor实现图像颜色空间的转换-比如RGB转灰度图等
- 利用opencv2和c++实现视频与图像的互相转换
- 利用ffmpeg实现视频格式转换成flv,保证能成功运行
- 利用GDI+实现常见图片格式任意转换的函数
- (原创)用verilog实现RGB格式图像到YCbCr或YUV格式的转换及其验证方法 (RGB2YCrCb)(RGB2YUV)
- Ubuntu系统上实现转换图像、音频和视频格式的方法