WPF(C#)中Bitmap与BitmapImage相互转换

原文:WPF(C#)中Bitmap与BitmapImage相互转换

一、WPF的Image控件中设置ImageSource

image1.Source = new BitmapImage(new Uri(@"image file path", Urikind.RelativeOrAbsolute));

还可以使用:

WPF(C#)中Bitmap与BitmapImage相互转换
System.IO.FileStream fs = new System.IO.FileStream(filepath, System.IO.FileMode.Open, System.IO.FileAccess.Read);

byte[] buffer = new byte[fs.Length]; fs.Read(buffer, 0, buffer.Length);
fs.Close(); fs.Dispose();
System.IO.MemoryStream ms = new System.IO.MemoryStream(buffer);
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.StreamSource = ms;
bitmapImage.CacheOption = BitmapCacheOption.OnLoad; bitmapImage.EndInit();
ms.Dispose();
image1.Source = bitmapImage;
WPF(C#)中Bitmap与BitmapImage相互转换

还可以使用:

WPF(C#)中Bitmap与BitmapImage相互转换
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
bitmapImage.UriSource = new Uri(szPath);//szPath为图片的全路径
bitmapImage.EndInit();
bitmapImage.Freeze();
image1.Source = bitmapImage;
WPF(C#)中Bitmap与BitmapImage相互转换

二、Bitmap转BitmapImage 
先将Bitmap储存成memorystream,然后指定给BitmapImage

WPF(C#)中Bitmap与BitmapImage相互转换
private BitmapImage BitmapToBitmapImage(System.Drawing.Bitmap bitmap)
{
BitmapImage bitmapImage = new BitmapImage();
using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
{
  bitmap.Save(ms, bitmap.RawFormat);
  bitmapImage.BeginInit();
  bitmapImage.StreamSource = ms;
  bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
  bitmapImage.EndInit();
  bitmapImage.Freeze();
}
  return bitmapImage;
}

image1.Source = BitmapToBitmapImage(bitmap);
WPF(C#)中Bitmap与BitmapImage相互转换

三、Bitmap转BitmapSource

WPF(C#)中Bitmap与BitmapImage相互转换
/*-------------------------------------------------------------------------
//Imaging.CreateBitmapSourceFromHBitmap方法,基于所提供的非托管位图和调色板信息的指针,
//返回一个托管的BitmapSource
---------------------------------------------------------------------------*/
Bitmap bitmap = CaptureScreen.GetDesktopImage();
IntPtr ip = bitmap.GetHbitmap();//从GDI+ Bitmap创建GDI位图对象

BitmapSource bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(ip, IntPtr.Zero, Int32Rect.Empty,
System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());

image1.Source = bitmapSource;
WPF(C#)中Bitmap与BitmapImage相互转换

四、BitmapSource转Bitmap

WPF(C#)中Bitmap与BitmapImage相互转换
BitmapSource m = (BitmapSource)image1.Source;

System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(m.PixelWidth, m.PixelHeight, System.Drawing.Imaging.PixelFormat.Format32bppPArgb); 

System.Drawing.Imaging.BitmapData data = bmp.LockBits(
new System.Drawing.Rectangle(System.Drawing.Point.Empty, bmp.Size), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppPArgb); 

m.CopyPixels(Int32Rect.Empty, data.Scan0, data.Height * data.Stride, data.Stride); bmp.UnlockBits(data);
WPF(C#)中Bitmap与BitmapImage相互转换

WPF(C#)中Bitmap与BitmapImage相互转换

上一篇:.NET 使用 JustAssembly 比较两个不同版本程序集的 API 变化


下一篇:WPF 的命令的自动刷新时机——当你 CanExecute 会返回 true 但命令依旧不可用时可能是这些原因