将图像保存到数字系统为C#的文件夹中
本文关键字:文件夹 系统 图像 保存 数字 | 更新日期: 2023-09-27 18:29:31
我的程序出现了一些问题。我正试图将图像保存在C#中。当我在电脑上拍摄新照片时,它们会保存,但会自行覆盖。有没有办法像用数字一样自动以不同的方式命名它们?例如:
- 图片1.jpg
- 图片2.jpg
- 图片3.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);
}
}
您可以添加类似时间戳的内容:
bitmap.Save("C:/Users/G73/Desktop/My OVMK Photos//OpenVMK" + DateTime.Now.ToString(yyyyMMddHHmmss) + ".jpg", ImageFormat.Jpeg);
一个非常简单的实现可以是:
- 对文件夹中具有相同名称的文件进行计数
- 将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);
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);
}
}
试试这样的东西:
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
如果您的文件名总是遵循等模式
"图像"+数字+.jpg
您可以编码以获得最大数量,然后创建一个带有递增的文件名
string file1 = "image1.jpg";
string file2 = "image13.jpg";
string number = file1.Substring(5, file1.IndexOf(".") - 5);
number = file2.Substring(5, file2.IndexOf(".") - 5);
但是,这取决于您有多少个文件。这可能会影响性能。其他选项可能是:
写一个只包含数字(最大数字)的文件每当你想写一个新的图像文件,
- 读那个数字
- 递增
- 用递增的数字保存图像文件
- 并用递增的数字重写该文件
您只需检查现有文件,并在建议的文件名中添加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);
....
}