private BitmapImage ConvertBitmapToBitmapImage(System.Drawing.Bitmap bitmap) { BitmapImage bmi = new BitmapImage(); using (MemoryStream ms = new MemoryStream()) { bitmap.Save(ms, ImageFormat.Png); bmi.BeginInit(); bmi.StreamSource = ms; bmi.CacheOption = BitmapCacheOption.OnLoad; bmi.EndInit(); if (bmi.CanFreeze) { bmi.Freeze(); } } return bmi; }
using System; using System.Collections.Generic; using System.Drawing.Imaging; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace WpfApp41 { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); this.WindowState = WindowState.Maximized; this.Loaded += MainWindow_Loaded; } private void MainWindow_Loaded(object sender, RoutedEventArgs e) { string pngFile = @"../../1.png"; System.Windows.Controls.Image img = new System.Windows.Controls.Image(); using (FileStream fs = new FileStream(pngFile, FileMode.OpenOrCreate)) { System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(fs); img.Source = ConvertBitmapToBitmapImage(bitmap); } this.Content = img; } private BitmapImage ConvertBitmapToBitmapImage(System.Drawing.Bitmap bitmap) { BitmapImage bmi = new BitmapImage(); using (MemoryStream ms = new MemoryStream()) { bitmap.Save(ms, ImageFormat.Png); bmi.BeginInit(); bmi.StreamSource = ms; bmi.CacheOption = BitmapCacheOption.OnLoad; bmi.EndInit(); if (bmi.CanFreeze) { bmi.Freeze(); } } return bmi; } } }