本文介绍了我想覆盖文件夹中的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Guys,你好吗,我希望你会好起来我昨晚在我的项目中有另一个问题我有来自谷歌的代码将图像保存在带有图片框的目录文件夹中但当我在这个文件夹中添加相同的图片时soi得到一个错误文件名已存在我想覆盖图像如果图像也在文件夹中所以请指导我该怎么办





谢谢



我尝试了什么:



private void button1_Click(object发件人,EventArgs e)

{

OpenFileDialog opFile = new OpenFileDialog();

opFile.Title =选择图像;

opFile.Filter =jpg文件(* .jpg)| * .jpg |所有文件(*。*)| *。*;

string appPath = Path.GetDirectoryName (Application.ExecutablePath)+ @\\\ tCatImage \;

if(Directory.Exists(appPath)== false)

{

Directory.CreateD irectory(appPath);

}

if(opFile.ShowDialog()== DialogResult.OK)

{

//尝试

// {

string iName = opFile.SafeFileName; //< ---

string filepath = opFile.FileName;

// dbpath = appPath + @\+ iName;

// dbpath = dbpath.Replace(@\\,@\);

dbpath = iName;

MessageBox.Show(dbpath) ;

// MessageBox.Show(dbpath);

File.Copy(filepath,appPath + iName);

pictureBox1.Image = new位图(opFile.OpenFile());

//}

// catch(Exception exp)

// {

// MessageBox.Show(无法打开文件+ exp.Message);

//}

}

否则

{

opFile.Dispose();

}

}

解决方案

Hello Guys, How are you, I hope you will be fine well I have another issue in my project last night I have code from Google to save image in directory folder with picture box but when I add the same pic in this folder soi get an error filename already exist I wanna overwrite image if image is also in folder so please guide me how can I do this


Thanks

What I have tried:

private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog opFile = new OpenFileDialog();
opFile.Title = "Select a Image";
opFile.Filter = "jpg files (*.jpg)|*.jpg|All files (*.*)|*.*";
string appPath = Path.GetDirectoryName(Application.ExecutablePath) + @"\CatImage\";
if (Directory.Exists(appPath) == false)
{
Directory.CreateDirectory(appPath);
}
if (opFile.ShowDialog() == DialogResult.OK)
{
//try
//{
string iName = opFile.SafeFileName; // <---
string filepath = opFile.FileName;
//dbpath = appPath+ @"\" + iName;
//dbpath = dbpath.Replace(@"\\", @"\");
dbpath = iName;
MessageBox.Show(dbpath);
// MessageBox.Show(dbpath);
File.Copy(filepath, appPath + iName);
pictureBox1.Image = new Bitmap(opFile.OpenFile());
//}
//catch (Exception exp)
//{
// MessageBox.Show("Unable to open file " + exp.Message);
//}
}
else
{
opFile.Dispose();
}
}

解决方案


这篇关于我想覆盖文件夹中的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 13:30