49 lines
1.6 KiB
C#
49 lines
1.6 KiB
C#
using System;
|
|
using System.Drawing;
|
|
using System.Drawing.Imaging;
|
|
using System.IO;
|
|
using System.Windows.Data;
|
|
using System.Windows.Media.Imaging;
|
|
|
|
namespace VisionCommonUI {
|
|
public class BitmapConverter : IValueConverter {
|
|
public object Convert(
|
|
object value,
|
|
Type targetType,
|
|
object parameter,
|
|
System.Globalization.CultureInfo culture) {
|
|
if (value == null) return null;
|
|
BitmapImage image;
|
|
using (var memory = new MemoryStream()) {
|
|
Bitmap bmp = (Bitmap)value;
|
|
bmp.Save(memory, ImageFormat.Bmp);
|
|
memory.Position = 0;
|
|
|
|
var bitmapImage = new BitmapImage();
|
|
bitmapImage.BeginInit();
|
|
bitmapImage.StreamSource = memory;
|
|
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
|
|
bitmapImage.EndInit();
|
|
|
|
image = bitmapImage;
|
|
}
|
|
|
|
return image;
|
|
}
|
|
|
|
private static ImageFormat GetImgFormat(object parameter) {
|
|
string format = (string)parameter;
|
|
return format == "png" ? ImageFormat.Png
|
|
: ImageFormat.Bmp;
|
|
}
|
|
|
|
public object ConvertBack(
|
|
object value,
|
|
Type targetType,
|
|
object parameter,
|
|
System.Globalization.CultureInfo culture) {
|
|
throw new NotImplementedException();
|
|
}
|
|
}
|
|
}
|