JD2022-TU1/main/tools/modules/JD.AssetBrowser/Utilities/ImageConverter.cs

32 lines
940 B
C#

using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Windows.Media.Imaging;
namespace JD.AssetBrowser.Utilities
{
public static class ImageConverter
{
public static BitmapImage ToBitmapImage(this Bitmap bitmap)
{
using (var memory = new MemoryStream())
{
bitmap.Save(memory, ImageFormat.Png);
memory.Position = 0;
var bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.StreamSource = memory;
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
bitmapImage.EndInit();
return bitmapImage;
}
}
public static System.Windows.Controls.Image ToImage(this Bitmap bitmap)
{
return new System.Windows.Controls.Image { Source = bitmap.ToBitmapImage() };
}
}
}