none
How to save and load an image selected with OpenFileDialogue? No errors but image does not show up RRS feed

  • Question

  • Hello everyone. Been at this for a while. I had it working on UWP, but having trouble on WPF. Basically I want to use OpenFileDialogue and select an image from my computer. I would then like to save a copy of that image in my application and also display the image in an Image control. I'm using Caliburn.micro for my MVVM framework

    My XAML code:

    <Button x:Name="MyImageButton" Content="Select Image" HorizontalAlignment="Center"  />

    <Image x:Name="MyImageSample" Source="{Binding MyImage}" />

    My C# code:

    public void MyImageButton()
            {
    
                // Configure open file dialog box
                Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
                dlg.FileName = "Image"; // Default file name
                dlg.DefaultExt = ".png"; // Default file extension
                dlg.Filter = "Image Files (.jpg)|*.jpg;*.jpeg;*.png;*.gif;*.tif;"; // Filter files by extension
    
                // Show open file dialog box
                Nullable<bool> result = dlg.ShowDialog();
    
                // Process open file dialog box results
                if (result == true)
                {
                    // Open document
                    string filename = dlg.FileName;
                    BitmapImage filefoto = new BitmapImage();
                    filefoto.UriSource = new Uri(filename);
                    _myImage = filefoto;
                    NotifyOfPropertyChange(() => MyImage);
                }
            }
    
    
    
            private BitmapImage _myImage;
    
            public BitmapImage MyImage
            {
                get { return _myImage; }
    
                set
                {
                    _myImage= value;
                }
            }

    Saturday, August 17, 2019 5:46 AM

All replies

  • Hi,
    change your code:

            // Open document
            string filename = dlg.FileName;
            BitmapImage filefoto = new BitmapImage();
            filefoto.BeginInit();
            filefoto.UriSource = new Uri(filename);
            filefoto.EndInit();
            _myImage = filefoto;

    or:

            // Open document
            string filename = dlg.FileName;
            BitmapImage filefoto = new BitmapImage(new Uri(filename));
            _myImage = filefoto;


    --
    Best Regards / Viele Grüße
    Peter Fleischer (former MVP for Developer Technologies)
    Homepage, Tipps, Tricks


    Sunday, August 18, 2019 5:32 AM
  • So somebody had the right answer and I was coming back to mark it as correct, but now that answer has been deleted. For those of you looking for the answer you have to add filefoto.BeginInit(); and filefoto.EndInit(); around filefoto.UriSource = new Uri(filename);

    filefoto.BeginInit();
    filefoto.UriSource = new Uri(filename);
    filefoto.EndInit();

    That will get it to work. However, make sure to scale the image because if you don't and it's a fairly large image who's background matches it might confuse you into thinking it's not there when really you're just staring at the corner of a huge image.

    Tuesday, August 20, 2019 11:57 PM