/
BitmapSourceBitmap.cs
57 lines (49 loc) · 1.78 KB
/
BitmapSourceBitmap.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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Copyright (c) 2019 .NET Foundation and Contributors. All rights reserved.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for full license information.
using System.IO;
using System.Threading.Tasks;
using System.Windows.Media.Imaging;
namespace Splat
{
/// <summary>
/// A bitmap that wraps a <see cref="BitmapSourceBitmap"/>.
/// </summary>
internal sealed class BitmapSourceBitmap : IBitmap
{
/// <summary>
/// Initializes a new instance of the <see cref="BitmapSourceBitmap"/> class.
/// </summary>
/// <param name="bitmap">The platform native bitmap we are wrapping.</param>
public BitmapSourceBitmap(BitmapSource bitmap)
{
Inner = bitmap;
}
/// <inheritdoc />
public float Width => (float)Inner.Width;
/// <inheritdoc />
public float Height => (float)Inner.Height;
/// <summary>
/// Gets the platform <see cref="BitmapSource"/>.
/// </summary>
public BitmapSource Inner { get; private set; }
/// <inheritdoc />
public Task Save(CompressedBitmapFormat format, float quality, Stream target)
{
return Task.Run(() =>
{
var encoder = format == CompressedBitmapFormat.Jpeg ?
new JpegBitmapEncoder() { QualityLevel = (int)(quality * 100.0f) } :
(BitmapEncoder)new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(Inner));
encoder.Save(target);
});
}
/// <inheritdoc />
public void Dispose()
{
Inner = null;
}
}
}