1

I try to follow code to set image source in WPF, when i want to delete old and set new image, rightly but image not refresh and show old picture. why?

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;
 }
Masoud AMR
  • 134
  • 2
  • 17

1 Answers1

1

Try this, Works for me.

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

Full Source Code

  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>
Eldho
  • 7,795
  • 5
  • 40
  • 77
  • @MasoudAMR will you try this , please work fine for me. Until `new Uri` is invoked it will not update the source – Eldho Jun 28 '15 at 06:42
  • 1
    `Binding` works in wpf wonderful , I would suggest to use that [Mvvm Implmentation of Image Change](http://lunarfrog.com/blog/2012/11/21/image-file-binding/) – Eldho Jun 28 '15 at 06:52
  • how/where do use this? – Masoud AMR Jun 28 '15 at 06:56
  • `MVVM` is pattern that is mostly used in WPF ,Silverlight and WP8 . It separate your `view => View Model => Model` . [Mvvm tutorials](http://stackoverflow.com/questions/1405739/mvvm-tutorial-from-start-to-finish) . It is well appreciated pattern for maintainability and scalability and testing each layer. – Eldho Jun 28 '15 at 07:01
  • any answers aren't for my question. – Masoud AMR Jun 28 '15 at 07:03
  • @MasoudAMR You might need to remove all your code form code behind to write in view model to achieve MVVM, If your middle of project . – Eldho Jun 28 '15 at 07:08
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/81764/discussion-between-eldho-and-masoud-amr). – Eldho Jun 28 '15 at 07:10
  • @Clemens ok, but this is not answer for me, didn't work. – Masoud AMR Jun 28 '15 at 07:15