【问题标题】:Saving Images to a folder with a numeric system C#将图像保存到具有数字系统 C# 的文件夹中
【发布时间】:2013-09-01 12:13:23
【问题描述】:

我的程序有问题。我正在尝试将图像保存在 C# 中,它们会保存,但是当我在计算机上拍摄新照片时它们会覆盖自己。反正有没有像数字一样自动命名它们?例如:

  • Image1.jpg
  • Image2.jpg
  • Image3.jpg

我正在使用 Visual Studio 2012 和 C# 中的编码。这是我使用的一些代码:

  private void button3_Click(object sender, EventArgs e)
  {   


        Rectangle form = this.Bounds;
        using (Bitmap bitmap = new Bitmap(form.Width, form.Height))
        {
            using (Graphics graphic =
                Graphics.FromImage(bitmap))
            {
                graphic.CopyFromScreen(form.Location,
                    Point.Empty, form.Size);
            }


                bitmap.Save("C:/Users/G73/Desktop/My OVMK Photos//OpenVMK.jpg", ImageFormat.Jpeg);


        }
    }

【问题讨论】:

标签: c# visual-studio image-processing


【解决方案1】:

您可以添加类似时间戳的内容:

bitmap.Save("C:/Users/G73/Desktop/My OVMK Photos//OpenVMK" + DateTime.Now.ToString(yyyyMMddHHmmss) + ".jpg", ImageFormat.Jpeg);

【讨论】:

  • 我不明白。这被标记为答案,但不能解决问题的要求。不良工程警报。 #redflag 我正在前往 meta 的路上;除了 OP 选择的答案之外,这是社区审核答案的一个很好的案例。一个会打破上述要求解决的解决方案的案例 正确答案是;如果您在同一秒保存两个文件会发生什么?
  • @Gusdor 如果这是您的问题,请使用 HHmmssffff 作为格式。如果您设法将其保存在同一千分之一秒内,则可以。我的解决方案提供了一种方法,但您仍然可以根据需要对其进行修改。正如我所看到的,因为他将其保存到用户特定的文件夹中,并且它使用了一个按钮事件,这意味着人工干预以及他一次保存一个图像的事实,因此它与一秒钟之内的速度非常不同。
  • 我的问题是它被标记为答案,但即使在“一秒钟内”错误发生之前,它也明显是错误的。考虑维护场景。代码更改很容易引入无形的问题。我想你可以 Thread.Sleep 为你的字符串格式中指定的最低增量,但如果字符串格式发生变化(格式和睡眠),你有两件事要改变。制作由增量或visa-verse驱动的格式比使用原始示例格式的解决方案需要更多代码。
  • 用“meh,它的罕见”来传递失败状态是一种可怕的态度——我对自己的回答感到内疚。我们应该努力纠正这种心态。
【解决方案2】:

一个非常简单的实现可能是:

  • 统计文件夹中的同名文件
  • 将 Count+1 附加到您要保存的文件的名称

类似:

var files = Directory.GetFiles(@"C:/Users/G73/Desktop/My OVMK Photos", "OpenVMK*");
...
var newFileName = string.Format(@"C:/Users/G73/Desktop/My OVMK Photos/OpenVMK{0}.jpg", files.Length+1);
bitmap.Save(newFileName, ImageFormat.Jpeg);

【讨论】:

  • 这将计算不应计算的名为“OpenVMKsomethingelse.jpg”的文件。
【解决方案3】:

试试这样的:

string filename = "file";
string extension = "txt";
string path = System.IO.Path.Combine(filename, extension); //desired path

int i = 1;

while(System.IO.File.Exists(path));
{
    //assemble fallback path
    path = System.IO.Path.Combine(string.Format("{0} ({1})", filename, i++), extension);

    if(int == Int32.MaxValue && System.IO.File.Exists(path))
        throw new InvalidOperationException("Too many files and commenters are pedants");
}

//save your file

【讨论】:

  • 儿子,如果你的 int.MaxValue 文件在相同的模式中具有相同的递增名称,你会遇到更大的问题。我已经在循环中断中进行了编辑,但无论如何,它仍然是一个例外。
【解决方案4】:
private void button3_Click(object sender, EventArgs e)
{

    Rectangle form = this.Bounds;
    using (Bitmap bitmap = new Bitmap(form.Width, form.Height))
    {
        using (Graphics graphic =
            Graphics.FromImage(bitmap))
        {
            graphic.CopyFromScreen(form.Location,
                Point.Empty, form.Size);
        }

        int count = 1;

        string baseFileName = "OpenVMK";
        string ext = ".jpg";
        string pathToFile = "C:/Users/G73/Desktop/My OVMK Photos";            
        while(System.IO.File.Exists(System.IO.Path.Combine(pathToFile, string.Format("{0}{1}{2}", baseFileName, i++, ext))) {};

        bitmap.Save(System.IO.Path.Combine(pathToFile, string.Format("{0}{1}{2}", baseFileName, i++, ext)), ImageFormat.Jpeg);


    }
}

【讨论】:

    【解决方案5】:

    如果您的文件名总是遵循诸如

    之类的模式

    “图片”+数字+.jpg

    您可以编写代码以获取最大数量,然后创建一个递增的文件名

    string file1 = "image1.jpg";
    string file2 = "image13.jpg";
    string number = file1.Substring(5, file1.IndexOf(".") - 5);
    number = file2.Substring(5, file2.IndexOf(".") - 5);
    

    但是,这取决于您拥有多少文件。这会影响性能。 其他选项可能是:

    写一个只包含数字的文件(最大数字) 每当你想写一个新的图像文件时,

    • 读那个号码
    • 增加它,
    • 用递增的数字保存图像文件
    • 并用递增的数字重写该文件

    【讨论】:

      【解决方案6】:

      您可以检查现有文件,然后在建议的文件名中添加 1、2、3 等:

          private static int GetNextFileNumber(String fileFullName) {
            int result = -1;
      
            String fileName = Path.GetFileNameWithoutExtension(fileFullName);
            String ext = Path.GetExtension(fileFullName);
      
            foreach (String file in Directory.GetFiles(Path.GetDirectoryName(fileFullName))) {
              if (!String.Equals(ext, Path.GetExtension(file), StringComparison.OrdinalIgnoreCase))
                continue;
      
              String name = Path.GetFileNameWithoutExtension(file);
      
              if (!name.StartsWith(fileName, StringComparison.OrdinalIgnoreCase))
                continue;
      
              if (result < 0)
                if (name.Equals(fileName, StringComparison.OrdinalIgnoreCase))
                  result = 1;
      
              int num = 0;
      
              if (int.TryParse(name.Substring(fileName.Length), out num))
                if (num > result)
                  result = num + 1;
            }
      
            return result;
          }
      
          private static String GetNextFileName(String fileFullName) {
            int number = GetNextFileNumber(fileFullName);
      
            if (number <= 0)
              return fileFullName;
      
            return Path.Combine(Path.GetDirectoryName(fileFullName), 
                                Path.GetFileNameWithoutExtension(fileFullName) + (number).ToString() + Path.GetExtension(fileFullName));
          }
      
      
         ....
      
         private void button3_Click(object sender, EventArgs e)
         { 
            ....
            // bitmap.Save("C:/Users/G73/Desktop/My OVMK Photos/OpenVMK.jpg", ImageFormat.Jpeg);
      
            // Instead of file_name use GetNextFileName(file_name):
            bitmap.Save(GetNextFileName("C:/Users/G73/Desktop/My OVMK Photos/OpenVMK.jpg"), ImageFormat.Jpeg);
            ....
         }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-04-05
        • 2012-06-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多