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
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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.
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:
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.
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