破玩意(一)利用GDAL实现图像格式转换

最近磁盘空间不足,就整理一下资料,看到一些早期的代码,自己写的工具,尘封很久了,其中绝大部分对于我来说没用了,放上来可能有些人会有用吧?

用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; }
        }

最最后附上源码,早期写的代码不规范,有兴趣看的将就一下。


转载自:https://blog.csdn.net/langyuewu/article/details/11618489

You may also like...