forked from TensorStack-AI/TensorStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageService.cs
More file actions
91 lines (82 loc) · 3.53 KB
/
ImageService.cs
File metadata and controls
91 lines (82 loc) · 3.53 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
using System;
using System.IO;
using System.Threading.Tasks;
using System.Windows.Media.Imaging;
namespace TensorStack.Image
{
public static class ImageService
{
const string RotationQuery = "System.Photo.Orientation";
/// <summary>
/// Loads image from file.
/// </summary>
/// <param name="fileName">Name of the file.</param>
/// <returns>BitmapSource.</returns>
public static BitmapSource LoadFromFile(string fileName, int decodePixelWidth = 0, int decodePixelHeight = 0)
{
return Load(fileName, decodePixelWidth, decodePixelHeight);
}
/// <summary>
/// Load as an asynchronous operation.
/// </summary>
/// <param name="filePath">The file path.</param>
public static async Task<WriteableBitmap> LoadFromFileAsync(string filePath, int decodePixelWidth = 0, int decodePixelHeight = 0)
{
return await Task.Run(() => Load(filePath, decodePixelWidth, decodePixelHeight));
}
/// <summary>
/// Loads the specified imagefile.
/// </summary>
/// <param name="filePath">The file path.</param>
/// <returns>WriteableBitmap.</returns>
/// <exception cref="System.ArgumentNullException">filePath</exception>
/// <exception cref="System.IO.FileNotFoundException">The file '{filePath}' does not exist.</exception>
internal static WriteableBitmap Load(string filePath, int decodePixelWidth = 0, int decodePixelHeight = 0)
{
if (string.IsNullOrEmpty(filePath))
throw new ArgumentNullException(nameof(filePath));
if (!File.Exists(filePath))
throw new FileNotFoundException($"The file '{filePath}' does not exist.", filePath);
var imageUri = new Uri(filePath, UriKind.RelativeOrAbsolute);
var rotation = GetRotation(imageUri);
var bitmapSource = new BitmapImage();
bitmapSource.BeginInit();
if (decodePixelWidth > 0)
bitmapSource.DecodePixelWidth = decodePixelWidth;
if (decodePixelHeight > 0)
bitmapSource.DecodePixelHeight = decodePixelHeight;
bitmapSource.Rotation = rotation;
bitmapSource.UriSource = imageUri;
bitmapSource.CacheOption = BitmapCacheOption.OnLoad;
bitmapSource.EndInit();
bitmapSource.Freeze();
return bitmapSource.ToWriteableBitmap();
}
/// <summary>
/// Gets the rotation of an image file.
/// </summary>
/// <param name="imageUri">The image URI.</param>
/// <returns>Rotation.</returns>
private static Rotation GetRotation(Uri imageUri)
{
var bitmapFrame = BitmapFrame.Create(imageUri, BitmapCreateOptions.DelayCreation, BitmapCacheOption.None);
if (bitmapFrame.Metadata is BitmapMetadata bitmapMetadata && bitmapMetadata.ContainsQuery(RotationQuery))
{
var queryResult = bitmapMetadata.GetQuery(RotationQuery);
if (queryResult is ushort orientation)
{
switch (orientation)
{
case 6:
return Rotation.Rotate90;
case 3:
return Rotation.Rotate180;
case 8:
return Rotation.Rotate270;
}
}
}
return Rotation.Rotate0;
}
}
}