-
-
Notifications
You must be signed in to change notification settings - Fork 32
/
BitmapToImageSourceConverter.cs
27 lines (23 loc) · 1.08 KB
/
BitmapToImageSourceConverter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
namespace DiscordRPforVS
{
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Globalization;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Imaging;
public class BitmapToImageSourceConverter : IValueConverter
{
public Object Convert(Object value, Type targetType, Object parameter, CultureInfo culture)
{
if (!(value is Bitmap bitmap))
return null;
Rectangle rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
BitmapData bitmapData = bitmap.LockBits(rect, ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
bitmap.UnlockBits(bitmapData);
return BitmapSource.Create(bitmap.Width, bitmap.Height, bitmap.HorizontalResolution, bitmap.VerticalResolution, PixelFormats.Bgra32, null, bitmapData.Scan0, rect.Width * rect.Height * 4, bitmapData.Stride);
}
public Object ConvertBack(Object value, Type targetType, Object parameter, CultureInfo culture) => null;
}
}