每次上传者裁剪图像时我都会保存一个新图像,我想检查每个新图像是否存在。我有这段代码:
private void pictureBox5_MouseUp(object sender, MouseEventArgs e)
{
Selecting = false;
// Copy the selected area.
SelectedArea = GetSelectedArea(pictureBox5.Image, Color.Transparent, Points);
SelectedArea.Save(@"C:\Users\User\Desktop\Gallery\image1cropped.png", ImageFormat.Png);
}
我希望它像image2cropped一样保存,image3cropped ..并检查它是否存在,如
if(File.Exists(@"C:\Users\User\Desktop\Gallery\image1cropped.png", ImageFormat.Png);
我想要检查像image2cropped,image3cropped ..等等。 想法?
答案 0 :(得分:0)
如果我理解:您正在将区域保存到文件中。第一次,你将它保存到image1cropped.png,然后你想自动使用image2cropped,以便不覆盖第一个文件,对吧? 在这种情况下,您可以使用以下代码:
int i = 0; // set 0 to start at 1 for "image1cropped"
string freeFileName; // the final fileName that doesn't exist
// loop while file imageXcropped exists
do
{
i++;
freeFileName = @"C:\Users\User\Desktop\Gallery\image" + i + "cropped.png";
} while (File.Exists(freeFileName));
// at this point freeFileName doesn't exists, you can use it
// use : SelectedArea.Save(freeFileName, ImageFormat.Png);