Пример #1
0
        public static void OnClick(object sender, RoutedEventArgs e)
        {
            var dialog = new OpenFileDialog();

            dialog.Title  = "Bild auswählen...";
            dialog.Filter = "Alle Dateien|*.*";
            var result = dialog.ShowDialog();

            if (result.HasValue && result.Value)
            {
                var image = new BitmapImage();
                using (var stream = new FileStream(dialog.FileName, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    image.BeginInit();
                    image.StreamSource = stream;
                    image.CacheOption  = BitmapCacheOption.OnLoad;
                    image.EndInit();
                }
                var button = sender as Button;
                if (button != null)
                {
                    var imageControl = button.Content as Image;
                    if (imageControl != null)
                    {
                        var expression = imageControl.GetBindingExpression(Image.SourceProperty);
                        if (expression != null)
                        {
                            var source = expression.ResolvedSource;
                            if (source != null)
                            {
                                var property = source.GetType().GetProperty(expression.ResolvedSourcePropertyName);
                                if (property != null)
                                {
                                    var bytes = new BitmapImageToByteArrayConverter().Convert(image, typeof(BitmapImage), null, System.Threading.Thread.CurrentThread.CurrentCulture);
                                    property.SetValue(source, bytes);
                                }
                            }
                        }
                    }
                }
            }
        }
 public static void OnClick(object sender, RoutedEventArgs e)
 {
     var dialog = new OpenFileDialog();
     dialog.Title = "Bild auswählen...";
     dialog.Filter = "Alle Dateien|*.*";
     var result = dialog.ShowDialog();
     if (result.HasValue && result.Value)
     {
         var image = new BitmapImage();
         using (var stream = new FileStream(dialog.FileName, FileMode.Open, FileAccess.Read, FileShare.Read))
         {
             image.BeginInit();
             image.StreamSource = stream;
             image.CacheOption = BitmapCacheOption.OnLoad;
             image.EndInit();
         }
         var button = sender as Button;
         if (button != null)
         {
             var imageControl = button.Content as Image;
             if (imageControl != null)
             {
                 var expression = imageControl.GetBindingExpression(Image.SourceProperty);
                 if (expression != null)
                 {
                     var source = expression.ResolvedSource;
                     if (source != null)
                     {
                         var property = source.GetType().GetProperty(expression.ResolvedSourcePropertyName);
                         if (property != null)
                         {
                             var bytes = new BitmapImageToByteArrayConverter().Convert(image, typeof(BitmapImage), null, System.Threading.Thread.CurrentThread.CurrentCulture);
                             property.SetValue(source, bytes);
                         }
                     }
                 }
             }
         }
     }
 }