//慢 1000ms
afterImg.Source = ToBitmapImage(bmp);
public BitmapImage ToBitmapImage(System.Drawing.Bitmap ImageOriginal)
{
BitmapImage bitmapImage = new BitmapImage();
using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
{
bitmapImage.BeginInit();
ImageOriginal.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
bitmapImage.StreamSource = ms;
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
bitmapImage.EndInit();
bitmapImage.Freeze();
}
return bitmapImage;
}
//快速 88ms
public static class BitmapSourceConvert
{
/// <summary>
/// Delete a GDI object
/// </summary>
/// <param name="o">The poniter to the GDI object to be deleted</param>
/// <returns></returns>
[DllImport("gdi32")]
private static extern int DeleteObject(IntPtr o);
/// <summary>
/// Convert an Bitmap to a WPF BitmapSource. The result can be used in the Set Property of Image.Source
/// </summary>
/// <param name="image">Bitmap</param>
/// <returns>The equivalent BitmapSource</returns>
public static BitmapSource ToBitmapSource(System.Drawing.Bitmap image)
{
IntPtr ptr = image.GetHbitmap();//obtain the Hbitmap
BitmapSource bs = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap
(
ptr,
IntPtr.Zero,
Int32Rect.Empty,
System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions()
);
DeleteObject(ptr);//release the HBitmap
return bs;
}
}
afterImg.Source = BitmapSourceConvert.ToBitmapSource(bmp);
c# image控件显示bitmap
最新推荐文章于 2025-03-04 19:44:41 发布