//The DockPanel_Button_Click method works because the of the way the XAML was written for the Image control. //Notice the soure is explicitly set to a BitmapImage. // //By writing the application this way, we avoid handling the download ourselves. The below method simply //extracts information we have already downloaded. // //UI XAML: // // <Image VerticalAlignment="Top" DockPanel.Dock="Left" Height="90" Width="65" Margin="7" > // <Image.Source> // <BitmapImage UriSource="{Binding Path=ImageURL}" /> // </Image.Source> // </Image> // void DockPanel_Button_Click(Object sender, RoutedEventArgs e) { DockPanel dock = sender as DockPanel; if (dock != null) { if (_vm == null) { _vm = (AddStuffViewModel)DataContext; } Movie movie = (MovieSearchResult)dock.DataContext; var image = Helper.FindVisualChild <Image>(dock); using (MemoryStream memoryStream = new MemoryStream()) { JpegBitmapEncoder encoder = new JpegBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(image.Source as BitmapImage)); encoder.Save(memoryStream); movie.Image = memoryStream.GetBuffer(); } } e.Handled = true; }
public AddStuffView() { InitializeComponent(); DataContext = new AddStuffViewModel(new PictureDialogService()); }