private void открытьToolStripMenuItem_Click(object sender, EventArgs e) { openFileDialog1.Multiselect = true; openFileDialog1.Filter = "Image files (*.BMP, *.JPG, *.GIF, *.TIF, *.PNG, *.ICO, *.EMF, *.WMF)|*.bmp;*.jpg;*.gif; *.tif; *.png; *.ico; *.emf; *.wmf"; if (openFileDialog1.ShowDialog() == DialogResult.OK) { System.IO.FileStream fs = new System.IO.FileStream(openFileDialog1.FileName, System.IO.FileMode.Open); System.Drawing.Image img = System.Drawing.Image.FromStream(fs); fs.Close(); pictureBox1.Image = img; } } 

Made in Windows Forms. C wpf just getting acquainted. Tell me how to open an image in Picture (WPF) and so that it scales under the application window?

  • Well, you have already opened, right? - Alexey Sarovsky
  • The code does not work in wpf - ms.x
  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

2 answers 2

You need to use Image control.

I will show an example with a button that, when clicked, opens OpenFileDialog . After selecting the image file - it is shown in this control.

Code example:

CODE

 // Создадим класс для реализации интерфейса ICommand // Чтобы использовать его в XAML'е public class Command : ICommand { public Command(Action action) { this.action = action; } Action action; EventHandler canExecuteChanged; event EventHandler ICommand.CanExecuteChanged { add { canExecuteChanged += value; } remove { canExecuteChanged -= value; } } public bool CanExecute(object parameter) { return true; } public void Execute(object parameter) { action(); } } // В классе Content реализуем интерфейс INotifyPropertyChanged // Чтобы послать нотификацию о том, что свойство Image поменялось public class Content : INotifyPropertyChanged { public Content() { // Инициализация команды openFileDialogCommand = new Command(ExecuteOpenFileDialog); // Инициализация OpenFileDialog openFileDialog = new OpenFileDialog() { Multiselect = true, Filter = "Image files (*.BMP, *.JPG, *.GIF, *.TIF, *.PNG, *.ICO, *.EMF, *.WMF)|*.bmp;*.jpg;*.gif; *.tif; *.png; *.ico; *.emf; *.wmf" }; } readonly OpenFileDialog openFileDialog; // Наша картинка public ImageSource Image { get; private set; } readonly ICommand openFileDialogCommand; public ICommand OpenFileDialogCommand { get { return openFileDialogCommand; } } // Действие при нажатии на кнопку "Open File Dialog" void ExecuteOpenFileDialog() { if(openFileDialog.ShowDialog() == true) { using(var stream = new FileStream(openFileDialog.FileName, FileMode.Open)) { Image = BitmapFrame.Create(stream, BitmapCreateOptions.None, BitmapCacheOption.OnLoad); RaisePropertyChanged("Image"); } } } // Реализация интерфейса INotifyPropertyChanged public event PropertyChangedEventHandler PropertyChanged; void RaisePropertyChanged(string propertyName) { if(PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); DataContext = new Content(); } } 

XAML

 <Grid> <Grid.RowDefinitions> <RowDefinition /> <RowDefinition Height="Auto" /> </Grid.RowDefinitions> <Image Source="{Binding Image, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" /> <Button Grid.Row="1" Command="{Binding OpenFileDialogCommand}" Content="Open File Dialog" /> </Grid> 

    Let's try a little easier

    XAML:

     <Image x:Name="img1" Stretch="Fill"/> <Button Content="" x:Name="buttonOpen" Click="buttonOpen_Click"/> 

    C #:

     OpenFileDialog openDialog = new OpenFileDialog(); openDialog.Filter = "Image files (*.BMP, *.JPG, *.GIF, *.TIF, *.PNG, *.ICO, *.EMF, *.WMF)|*.bmp;*.jpg;*.gif; *.tif; *.png; *.ico; *.emf; *.wmf"; if (openDialog.ShowDialog() == ModalResult.Ok) { img1.Source = new BitmapImage(new Uri(openDialog.FileName)); }