【问题标题】:Saving Image from Image Control to file in current directory将图像从图像控件保存到当前目录中的文件
【发布时间】:2025-12-04 21:10:01
【问题描述】:

我有一个名为 image1 的图像控件 (WPF),当我单击命令按钮时它会加载图像(这是有关该事件的代码)。现在,如果我想将该图像文件复制到当前目录(即项目目录)上,我该怎么办?

private void commandButton1_Click(object sender, System.Windows.RoutedEventArgs e)
    {
        Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
        dlg.DefaultExt = "*.jpg";
        dlg.Filter = "Image Files|*.jpg";
        Nullable<bool> result = dlg.ShowDialog();
        if (result == true)
        {                
            image1.Source = new BitmapImage(new Uri(dlg.FileName));   
        }
        // to do
    }
}

【问题讨论】:

  • 这样做不会遇到 UAC 问题吗?
  • 谢谢男人!很有帮助

标签: c# wpf image controls


【解决方案1】:

使用File.Copy 方法。您可以从Path.GetFileName 获取不带路径的文件名:

// get file name from full source path (for example)
string targetFileName = Path.GetFileName(dlg.FileName);

// copy to relative path, i.e. current directory
File.Copy(dlg.FileName, targetFileName); 

如果您的问题是关于如何保存 BitmapSource,您可以查看BitmapEncoder

【讨论】:

  • 那么你应该接受这个答案。请参阅 here 它是如何工作的。
最近更新 更多