-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathBitmapSourceExtensions.cs
More file actions
34 lines (29 loc) · 1.21 KB
/
Copy pathBitmapSourceExtensions.cs
File metadata and controls
34 lines (29 loc) · 1.21 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
using System.Windows;
using System.Windows.Media.Imaging;
namespace BytecodeApi.Wpf.Extensions;
/// <summary>
/// Provides a set of <see langword="static" /> methods for interaction with <see cref="BitmapSource" /> objects.
/// </summary>
public static class BitmapSourceExtensions
{
extension(BitmapSource bitmapSource)
{
/// <summary>
/// Converts this <see cref="BitmapSource" /> to a <see cref="System.Drawing.Bitmap" /> object.
/// </summary>
/// <returns>
/// A new <see cref="System.Drawing.Bitmap" /> object, which is a bitmap copy of the original <see cref="BitmapSource" />.
/// </returns>
public System.Drawing.Bitmap ToBitmap()
{
System.Drawing.Bitmap bitmap = new(bitmapSource.PixelWidth, bitmapSource.PixelHeight, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
System.Drawing.Imaging.BitmapData data = bitmap.LockBits(
new System.Drawing.Rectangle(System.Drawing.Point.Empty, bitmap.Size),
System.Drawing.Imaging.ImageLockMode.WriteOnly,
System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
bitmapSource.CopyPixels(Int32Rect.Empty, data.Scan0, data.Height * data.Stride, data.Stride);
bitmap.UnlockBits(data);
return bitmap;
}
}
}