-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathBitmapEncoderExtensions.cs
More file actions
39 lines (35 loc) · 1.16 KB
/
Copy pathBitmapEncoderExtensions.cs
File metadata and controls
39 lines (35 loc) · 1.16 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
using System.Windows.Media.Imaging;
namespace BytecodeApi.Wpf.Extensions;
/// <summary>
/// Provides a set of <see langword="static" /> methods for interaction with <see cref="BitmapEncoder" /> objects.
/// </summary>
public static class BitmapEncoderExtensions
{
extension(BitmapEncoder bitmapEncoder)
{
/// <summary>
/// Encodes a bitmap image and saves the encoded image to a file.
/// </summary>
/// <param name="path">A <see cref="string" /> that contains the name of the file to which to save this bitmap image.</param>
public void Save(string path)
{
Check.ArgumentNull(bitmapEncoder);
Check.ArgumentNull(path);
using FileStream file = File.Create(path);
bitmapEncoder.Save(file);
}
/// <summary>
/// Encodes a bitmap image and returns a new <see cref="byte" />[] representing the encoded image.
/// </summary>
/// <returns>
/// A new <see cref="byte" />[] representing the encoded image.
/// </returns>
public byte[] ToArray()
{
Check.ArgumentNull(bitmapEncoder);
using MemoryStream memoryStream = new();
bitmapEncoder.Save(memoryStream);
return memoryStream.ToArray();
}
}
}