【问题标题】:Cannot save image in a folder that I created无法将图像保存在我创建的文件夹中
【发布时间】:2018-10-14 03:22:10
【问题描述】:

我目前正在使用 C# Windows 窗体创建一个图像大小调整程序。 所以,我让它用调整大小的图像覆盖现有图像。 我还制作了一个功能,使Originals文件夹并在其中保存原始图像,以便当我需要原始图像时可以使用文件夹中的图像。 下面是这个

for (int k = 0; k < openFileDialog.FileNames.Length; k++)
{
   fileNames.Add(openFileDialog.FileNames[k]);

   using (Stream stream = File.OpenRead(fileNames[k]))
   {
       //System.Collections.Generic.List<System.Drawing.Image>
       selectedImages.Add(Image.FromStream(stream));
       resizedImages.Add(Image.FromStream(stream));                                                 
   }
   filesCount++;
}

for (int k = 0; k < filesCount; k++)
{
    string filePath = Path.GetDirectoryName(fileNames[k]);
    Directory.CreateDirectory(filePath + "\\Originals");

    string selectedFileName = filePath + "\\Originals\\" + Path.GetFileName(fileNames[k]);
    string resizedFileName = filePath + "\\" + Path.GetFileNameWithoutExtension(fileNames[k]) + ".jpg";

    //GetImageFormat is my function that return ImageFormat. It has no problem.
    selectedImages[k].Save(selectedFileName, GetImageFormat(Path.GetExtension(fileNames[k])));
    resizedImages[k].Save(resizedFileName, ImageFormat.Jpeg);
}

这里的问题是 selectedImages[k].Save 发出 GDI+ 通用错误,尽管 resizedImages[k].Save 工作得很好。 我认为这是因为我创建的文件夹但我找不到解决方案。

【问题讨论】:

    标签: c# file gdi+ openfiledialog imaging


    【解决方案1】:

    我认为这是因为我创建的文件夹但我找不到 解决方案。

    错误的Directory.CreateDirectory 在不存在的情况下如果无法创建它会抛出异常。


    让我们来解决你遇到的问题

    • \\Originals\\ 不要这样做,如果您需要反斜杠,请使用 @\Originals\
    • 如果要组合路径,请使用Path.Combine
    • 当你可以使用foreach时,不要使用for
    • 没有必要创建这么多列表和循环
    • 如果您创建了一个图像,您需要处理它
    • 最重要的是,不要尝试将文件保存在具有打开文件句柄的文件之上。

    在这种情况下,您需要退出代码并删除所有冗余。您的大部分代码都绝对不需要,它使您的生活更难调试

    foreach (var file in openFileDialog.FileNames)
    {
       var name = Path.GetFileName(file);
       var path = Path.GetDirectoryName(file);
       var newPath = Path.Combine(path, "Originals");
       var newName = $"{Path.GetFileNameWithoutExtension(name)}.jpg";
    
       Directory.CreateDirectory(newPath);
    
       var newFullPath = Path.Combine(newPath, name);
       // why do anything fancy when you just want to move it
       File.Move(file, newFullPath);
    
       // lets open that file from there, so we don't accidentally cause the same 
       // problem again, then save it
       using (var image = Image.FromFile(newFullPath))
          image.Save(Path.Combine(path, newName), ImageFormat.Jpeg);  
    }
    

    虽然我不确定您的实际问题是什么,但我假设它是 GetImageFormat 方法,或者您试图用打开的句柄覆盖文件。但是,本着我认为您正在努力实现的精神,这可能会奏效

    【讨论】:

      【解决方案2】:

      问题是您正试图覆盖现有文件。 Image.Save 方法不支持。

      解决方法很简单,先删除文件再保存:

      File.Delete(selectedFileName);
      selectedImages[k].Save(selectedFileName, GetImageFormat(Path.GetExtension(fileNames[k])));
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-05-15
        • 1970-01-01
        • 1970-01-01
        • 2015-02-09
        • 1970-01-01
        • 2016-01-09
        • 1970-01-01
        相关资源
        最近更新 更多