如何用相同的文件名但不同的格式替换控制台应用程序图像文件格式?

时间:2014-12-04 15:33:36

标签: c# .net winforms

本案例中的原始格式为.jpg,我想用.gif替换文件。 因此,如果原始文件为:test.jpg,则最终目录中只有test.gif

这就是我所做的:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Drawing;

namespace ConvertImagesFormats
{
    class Program
    {
        static void Main(string[] args)
        {
            FileInfo[] imagesToConvert;
            string sourceDirectory = @"C:\test";
            string targetDirectory = @"C:\test";
            DirectoryInfo di = new DirectoryInfo(sourceDirectory);
            imagesToConvert = di.GetFiles("*.jpg");
            Bitmap bmp;

            for (int i = imagesToConvert.Length - 1; i > 0; i--)
            {
                bmp = new Bitmap(imagesToConvert[i].FullName);
                bmp.Save(targetDirectory + "\\" + imagesToConvert[i].Name, 
                    System.Drawing.Imaging.ImageFormat.Gif);
            }
        }
    }
}

但是我得到例外:

bmp.Save(targetDirectory + "\\" + imagesToConvert[i].Name, 
    System.Drawing.Imaging.ImageFormat.Gif);

  

ExternalException

     

GDI +中发生了一般错误

     

System.Runtime.InteropServices.ExternalException未处理
  HResult = -2147467259消息= GDI +中发生一般错误   Source = System.Drawing ErrorCode = -2147467259 StackTrace:          在System.Drawing.Image.Save(String filename,ImageCodecInfo encoder,EncoderParameters encoderParams)          在System.Drawing.Image.Save(String filename,ImageFormat格式)          at ConvertImagesFormats.Program.Main(String [] args)在d:\ C-Sharp \ ConvertImagesFormats \ ConvertImagesFormats \ ConvertImagesFormats \ Program.cs:line   25          在System.AppDomain._nExecuteAssembly(RuntimeAssembly程序集,String [] args)          在System.AppDomain.ExecuteAssembly(String assemblyFile,Evidence assemblySecurity,String [] args)          在Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()          在System.Threading.ThreadHelper.ThreadStart_Context(对象状态)          在System.Threading.ExecutionContext.RunInternal(ExecutionContext   executionContext,ContextCallback回调,对象状态,布尔值   preserveSyncCtx)          at System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback callback,Object state,Boolean   preserveSyncCtx)          在System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback回调,对象状态)          在System.Threading.ThreadHelper.ThreadStart()InnerException:

1 个答案:

答案 0 :(得分:0)

有效吗?还添加了一点重构:

using System.Drawing.Imaging;
using System.IO;
using System.Drawing;

namespace ConvertImagesFormats
{
    class Program
    {
        static void Main(string[] args)
        {
            string sourceDirectory = @"C:\test";
            string targetDirectory = @"C:\test";
            ImageFormat imageFormat = ImageFormat.Gif;

            foreach(var image in Directory.GetFiles(sourceDirectory, "*.jpg"))
            {
                using (Bitmap bmp = new Bitmap(image))
                {
                    string targetName = Path.GetFileNameWithoutExtension(image) + "." + imageFormat;
                    bmp.Save(Path.Combine(targetDirectory, targetName), imageFormat);
                }
            }
        }
    }
}