保存文件而不覆盖现有图像 [英] saving a file without overwriting the existing image

查看:90
本文介绍了保存文件而不覆盖现有图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下方法将文件保存到文件夹:



i save a file to a folder using:

private void btnsave_Click(object sender, EventArgs e)
{
    string path = @"H:\le sensored\le sensored\images";
    string name = "Osim";
    pictureBox6.Image.Save(path + @"\" + name + ".jpg", ImageFormat.Jpeg);
}



但是当我尝试保存另一张图片时会覆盖第一张图片,请将其他图片保存在同一文件路径中吗?


but when i try to save another picture it overwrites the 1st one,please can i save other image in that same file path?

推荐答案

当您尝试保存文件名为现有文件的文件

时,它将被覆盖。



你必须使用不同的文件名。



也许你应该使用一个保存数字的柜台

of保存图片并在文件写入后增加图片。



文件名可以是例如是Osim和计数器当前值的组合。



然后您的文件将被调用,例如Osim_1.jpg,Osim_2.jpg,Osim_3.jpg等。
When you try to save a file with a file name of an existing file
it will be overwritten.

You must use a different file name.

Maybe you should use a counter that holds the number
of saved pictures and increase it after a file has been written.

The file name could e.g. be a combination of "Osim" and the current value of the counter.

Then your files would be called e.g. Osim_1.jpg, Osim_2.jpg, Osim_3.jpg etc.


要做到这一点,你必须做两件事之一:

1)重命名首先将现有文件转换为其他内容 - File.Move [ ^ ]将为您完成,但您可能必须检查该文件是否存在。

2)使用不同的名称。



在任何一种情况下,检查文件是否存在 File.Exists [ ^ ]首先。



要使用其他名称,我会建议您在文件扩展名之前附加版本号 - 如果您使用整数值c检查它是否存在,如果存在则递增,然后重新检查。

所以你最终会得到

To do that, you will have to do one of two things:
1) Rename the existing file to something else first - File.Move[^] will do that for you, but you may have to check if that file exists as well.
2) Use a different name.

In either case, check if the file exists with File.Exists[^] first.

To use a different name, I'd suggest that you append a version number just before the file extension - and if you use an integer value to can check if it exists, and increment it if it does, then recheck.
So you would end up with
MyPic.jpg
MyPic.0001.jpg
MyPic.0002.jpg
...


这篇关于保存文件而不覆盖现有图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆