System.Drawing.Image data to System.Windows.Media.ImageSource (WPF)

时间:2021-06-04 14:18:14

1. Use MemeoryStream 

 1 ImageBrush backBrush = new ImageBrush();
 2 BitmapImage bi = new BitmapImage();
 3 bi.BeginInit();
 4 MemoryStream ms = new MemoryStream();
 5 iMove.app.EastOnline.Properties.Resources.Background.Save(ms, ImageFormat.Bmp); // System.Drawing.Image;
 6 ms.Seek(0, SeekOrigin.Begin);
 7 bi.StreamSource = ms;
 8 bi.EndInit();
 9 backBrush.ImageSource = Imaging.CreateBitmapSourceFromHBitmap(hBitImage, IntPtr.Zero, Int32Rect.Empty,
10                     BitmapSizeOptions.FromEmptyOptions());
11                 backBrush.Stretch = Stretch.Fill;
12                 this.Background = backBrush;

 

 

2. Use Imaging

ImageBrush backBrush = new ImageBrush();
         
IntPtr hBitImage = iMove.app.EastOnline.Properties.Resources.Background.GetHbitmap();

backBrush.ImageSource = Imaging.CreateBitmapSourceFromHBitmap(hBitImage, IntPtr.Zero, Int32Rect.Empty,
                    BitmapSizeOptions.FromEmptyOptions());
                backBrush.Stretch = Stretch.Fill;
this.Background = backBrush;