How can i open a image file in WPF by the user using openFileDialog ?

Vinit Joshi 1 Reputation point
2021-05-19T19:06:24.167+00:00

I have created an Image tag in the XAML file. when i selected an image using an openFileDialog . I cannot be able to Bind the data in openFileDialog FileName to Image.Source.

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,671 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
764 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. DaisyTian-1203 11,616 Reputation points
    2021-05-20T02:16:20.007+00:00

    I will show you a sample to open image with OpenFileDialog in C# code,
    XAML code:

      <StackPanel>  
            <Image Name="imgPhoto" Stretch="Fill" Width="300" Height="300"/>  
            <Button Height="23" HorizontalAlignment="Center" Margin="12,0,0,34" Name="btnOpen" VerticalAlignment="Bottom" Width="75" Click="btnOpen_Click">Open</Button>  
        </StackPanel>  
    

    C# code:

      private void btnOpen_Click(object sender, RoutedEventArgs e)  
            {  
                OpenFileDialog op = new OpenFileDialog();  
                op.Title = "Select a picture";  
                op.Filter = "All supported graphics|*.jpg;*.jpeg;*.png|" +  
                  "JPEG (*.jpg;*.jpeg)|*.jpg;*.jpeg|" +  
                  "Portable Network Graphic (*.png)|*.png";  
                if (op.ShowDialog() == true)  
                {  
                    imgPhoto.Source = new BitmapImage(new Uri(op.FileName));  
                }  
            }  
    

    Result picture is:
    98045-3.png


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

  2. Peter Fleischer (former MVP) 19,231 Reputation points
    2021-05-19T20:29:24.097+00:00

    Hi,
    try following demo:

    XAML:

    <Window x:Class="Window090"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:WpfApp1.WpfApp090"
            mc:Ignorable="d"
            Title="Show Image" Height="450" Width="800">
      <Window.DataContext>
        <local:ViewModel/>
      </Window.DataContext>
      <StackPanel>
        <Button Content="Load Piucture" Command="{Binding}"/>
        <Image Source="{Binding Picture}"/>
      </StackPanel>
    </Window>
    

    And ViewModel

    Imports System.ComponentModel
    Imports System.Runtime.CompilerServices
    Imports System.Windows.Forms
    
    Namespace WpfApp090
      Public Class ViewModel
        Implements ICommand, INotifyPropertyChanged
    
        Public Property Picture As BitmapImage
        Public Sub Execute(parameter As Object) Implements ICommand.Execute
          Using ofd As New OpenFileDialog
            If ofd.ShowDialog = DialogResult.OK Then
              Picture = New BitmapImage(New Uri(ofd.FileName))
              OnPropertyChanged(NameOf(Picture))
            End If
          End Using
        End Sub
    
        Public Event CanExecuteChanged As EventHandler Implements ICommand.CanExecuteChanged
        Public Function CanExecute(parameter As Object) As Boolean Implements ICommand.CanExecute
          Return True
        End Function
    
        Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
        Private Sub OnPropertyChanged(<CallerMemberName> Optional propName As String = "")
          RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propName))
        End Sub
      End Class
    End Namespace
    
    0 comments No comments