【问题标题】:set image source in wpf在 wpf 中设置图像源
【发布时间】:2015-06-28 15:45:30
【问题描述】:

当我想 delete old 并设置 old 并设置 new 图像时,我尝试按照代码 set image image source source 并显示旧图片。为什么?

Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();

string subpath = "UserProfile";
Directory.CreateDirectory(subpath);


// Set filter for file extension and default file extension 
dlg.DefaultExt = ".jpg";
dlg.Filter = "jpeg Files (*.jpg)|*.jpg";
// Display OpenFileDialog by calling ShowDialog method 
if (dlg.ShowDialog() == true)
{
       string fileName = "pic" + txtEmployeeID.Text + ".jpg";
       string newPath = "\\" + subpath + "\\" + fileName;

       if (File.Exists(Directory.GetCurrentDirectory() + "\\" + newPath))
       {
            imgUser.Source = null;
            File.Delete(Directory.GetCurrentDirectory() + newPath);
       }

  File.Copy(dlg.FileName.ToString(), Directory.GetCurrentDirectory() + "\\" + newPath);

       ImageSource imageSrc = BitmapFromUri(new Uri(Directory.GetCurrentDirectory() + newPath));
       imgUser.Source = imageSrc;

}



 public static ImageSource BitmapFromUri(Uri source)
 {
      var bitmap = new BitmapImage();
      bitmap.BeginInit();
      bitmap.UriSource = source;
      bitmap.CacheOption = BitmapCacheOption.OnLoad;
      bitmap.EndInit();
      return bitmap;
 }

【问题讨论】:

    标签: c# wpf image


    【解决方案1】:

    试试这个,对我有用。

            var bitmap = new BitmapImage();
            bitmap.BeginInit();
            bitmap.UriSource = new Uri(source.AbsoluteUri);
            bitmap.EndInit();
            return bitmap;
    

    完整的源代码

      public MainWindow()
        {
            InitializeComponent();
    
            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
    
            string subpath = "UserProfile";
            Directory.CreateDirectory(subpath);
    
    
            // Set filter for file extension and default file extension 
            dlg.DefaultExt = ".jpg";
            dlg.Filter = "jpeg Files (*.jpg)|*.jpg";
            // Display OpenFileDialog by calling ShowDialog method 
            if (dlg.ShowDialog() == true)
            {
                string fileName = "pic" + txtEmployeeID.Text + ".jpg";
                string newPath = "\\" + subpath + "\\" + fileName;
    
                if (File.Exists(Directory.GetCurrentDirectory() + "\\" + newPath))
                {
                    imgUser.Source = null;
                    File.Delete(Directory.GetCurrentDirectory() + newPath);
                }
    
                File.Copy(dlg.FileName.ToString(), Directory.GetCurrentDirectory() + "\\" + newPath);
    
                ImageSource imageSrc = BitmapFromUri(new Uri(Directory.GetCurrentDirectory() + newPath));
                imgUser.Source = imageSrc;
    
            }
    
    
    
    
    
        }
    
        public static ImageSource BitmapFromUri(Uri source)
        {
            var bitmap = new BitmapImage();
            bitmap.BeginInit();
            bitmap.UriSource = new Uri(source.AbsoluteUri);
            bitmap.CacheOption = BitmapCacheOption.OnLoad;
            bitmap.EndInit();
            return bitmap;
        }
    
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
    
            string subpath = "UserProfile";
            Directory.CreateDirectory(subpath);
    
    
            // Set filter for file extension and default file extension 
            dlg.DefaultExt = ".jpg";
            dlg.Filter = "jpeg Files (*.jpg)|*.jpg";
            // Display OpenFileDialog by calling ShowDialog method 
            if (dlg.ShowDialog() == true)
            {
                string fileName = "pic" + DateTime.Now.Millisecond + ".jpg";
                string newPath = "\\" + subpath + "\\" + fileName;
    
                if (File.Exists(Directory.GetCurrentDirectory() + "\\" + newPath))
                {
                    imgUser.Source = null;
                    // File.Delete(Directory.GetCurrentDirectory() + newPath);
                }
    
                File.Copy(dlg.FileName.ToString(), Directory.GetCurrentDirectory() + "\\" + newPath);
    
                ImageSource imageSrc = BitmapFromUri(new Uri(Directory.GetCurrentDirectory() + newPath));
                imgUser.Source = imageSrc;
    
            }
        }
    

    Xaml

     <Image x:Name="imgUser" Margin="88,70,49,72"> </Image>
    
        <Button Margin="0,277,425,0" Click="Button_Click"> </Button>
    

    【讨论】:

    • @MasoudAMR 你会试试这个,请为我工作。在调用new Uri 之前,它不会更新源
    • Binding 在 wpf 中工作得很好,我建议使用 Mvvm Implmentation of Image Change
    • 如何/在哪里使用这个?
    • MVVM 是主要用于 WPF 、Silverlight 和 WP8 的模式。它将您的 view =&gt; View Model =&gt; Model 分开。 Mvvm tutorials 。它在可维护性和可扩展性以及测试每一层方面都非常受欢迎。
    • 任何答案都不适合我的问题。
    猜你喜欢
    • 1970-01-01
    • 2013-02-28
    • 2014-03-10
    • 1970-01-01
    • 2019-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多