如何在我的 WPF 应用程序中保存图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14099869/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 06:50:14 来源:igfitidea点击:
How To Save Image in My WPF Application
提问by nitin-sharma
In My WPF application i am not able to save image inside my application in snap folder. Below is the code i am using.
在我的 WPF 应用程序中,我无法将图像保存在我的应用程序中的 snap 文件夹中。下面是我正在使用的代码。
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp";
if (ofd.ShowDialog() == DialogResult.OK)
{
string filepath = ofd.FileName;
File.Copy(ofd.FileName, Application.StartupPath + "\snaps\" + ofd.SafeFileName,true);
photoTextBox.Text= ofd.SafeFileName;
pictureBox1.Image = Image.FromFile(ofd.FileName);
}
回答by Yashpal Singla
Code to open the file browser
打开文件浏览器的代码
string filepath;
//browse Button
private void button4_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog open = new OpenFileDialog();
open.Multiselect = false;
open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp";
bool? result = open.ShowDialog();
if (result == true)
{
filepath = open.FileName; // Stores Original Path in Textbox
ImageSource imgsource = new BitmapImage(new Uri(filepath)); // Just show The File In Image when we browse It
Clientimg.Source = imgsource;
}
}
And below is the code used for saving the file
下面是用于保存文件的代码
private static String GetDestinationPath(string filename, string foldername)
{
String appStartPath = System.IO.Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName);
appStartPath = String.Format(appStartPath + "\{0}\" + filename, foldername);
return appStartPath;
}
How to use it
如何使用它
string name = System.IO.Path.GetFileName(filepath);
string destinationPath = GetDestinationPath(name,"YourFolderName");
File.Copy(filepath, destinationPath, true);

