-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathImageExtensions.cs
More file actions
44 lines (39 loc) · 1.04 KB
/
ImageExtensions.cs
File metadata and controls
44 lines (39 loc) · 1.04 KB
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Interop;
using System.Windows.Media.Imaging;
namespace BytecodeApi.Wpf.Extensions;
/// <summary>
/// Provides a set of <see langword="static" /> methods for interaction with <see cref="Image" /> objects.
/// </summary>
public static class ImageExtensions
{
extension(Bitmap bitmap)
{
/// <summary>
/// Returns a managed <see cref="BitmapSource" />, based on the provided <see cref="Bitmap" />.
/// </summary>
/// <returns>
/// The created <see cref="BitmapSource" />.
/// </returns>
public BitmapSource ToBitmapSource()
{
Check.ArgumentNull(bitmap);
nint hBitmap = bitmap.GetHbitmap();
try
{
return Imaging.CreateBitmapSourceFromHBitmap(hBitmap, 0, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
}
finally
{
Native.DeleteObject(hBitmap);
}
}
}
}
file static class Native
{
[DllImport("gdi32.dll")]
public static extern bool DeleteObject(nint obj);
}