|
1 | 1 | // Copyright (c) 2023 Files Community |
2 | 2 | // Licensed under the MIT License. See the LICENSE. |
3 | 3 |
|
| 4 | +using Microsoft.UI.Xaml.Controls; |
4 | 5 | using Microsoft.UI.Xaml.Media.Imaging; |
5 | 6 | using System.IO; |
| 7 | +using Windows.Foundation.Metadata; |
6 | 8 | using Windows.Graphics.Imaging; |
7 | 9 | using Windows.Storage; |
8 | 10 | using Windows.Storage.Streams; |
@@ -47,43 +49,60 @@ internal static class BitmapHelper |
47 | 49 | /// </remarks> |
48 | 50 | public static async Task RotateAsync(string filePath, BitmapRotation rotation) |
49 | 51 | { |
50 | | - if (string.IsNullOrEmpty(filePath)) |
| 52 | + try |
51 | 53 | { |
52 | | - return; |
53 | | - } |
| 54 | + if (string.IsNullOrEmpty(filePath)) |
| 55 | + { |
| 56 | + return; |
| 57 | + } |
54 | 58 |
|
55 | | - var file = await StorageHelpers.ToStorageItem<IStorageFile>(filePath); |
56 | | - if (file is null) |
57 | | - { |
58 | | - return; |
59 | | - } |
| 59 | + var file = await StorageHelpers.ToStorageItem<IStorageFile>(filePath); |
| 60 | + if (file is null) |
| 61 | + { |
| 62 | + return; |
| 63 | + } |
60 | 64 |
|
61 | | - var fileStreamRes = await FilesystemTasks.Wrap(() => file.OpenAsync(FileAccessMode.ReadWrite).AsTask()); |
62 | | - using IRandomAccessStream fileStream = fileStreamRes.Result; |
63 | | - if (fileStream is null) |
64 | | - { |
65 | | - return; |
66 | | - } |
| 65 | + var fileStreamRes = await FilesystemTasks.Wrap(() => file.OpenAsync(FileAccessMode.ReadWrite).AsTask()); |
| 66 | + using IRandomAccessStream fileStream = fileStreamRes.Result; |
| 67 | + if (fileStream is null) |
| 68 | + { |
| 69 | + return; |
| 70 | + } |
67 | 71 |
|
68 | | - BitmapDecoder decoder = await BitmapDecoder.CreateAsync(fileStream); |
69 | | - using var memStream = new InMemoryRandomAccessStream(); |
70 | | - BitmapEncoder encoder = await BitmapEncoder.CreateForTranscodingAsync(memStream, decoder); |
| 72 | + BitmapDecoder decoder = await BitmapDecoder.CreateAsync(fileStream); |
| 73 | + using var memStream = new InMemoryRandomAccessStream(); |
| 74 | + BitmapEncoder encoder = await BitmapEncoder.CreateForTranscodingAsync(memStream, decoder); |
| 75 | + |
| 76 | + for (int i = 0; i < decoder.FrameCount - 1; i++) |
| 77 | + { |
| 78 | + encoder.BitmapTransform.Rotation = rotation; |
| 79 | + await encoder.GoToNextFrameAsync(); |
| 80 | + } |
71 | 81 |
|
72 | | - for (int i = 0; i < decoder.FrameCount - 1; i++) |
73 | | - { |
74 | 82 | encoder.BitmapTransform.Rotation = rotation; |
75 | | - await encoder.GoToNextFrameAsync(); |
76 | | - } |
77 | 83 |
|
78 | | - encoder.BitmapTransform.Rotation = rotation; |
| 84 | + await encoder.FlushAsync(); |
79 | 85 |
|
80 | | - await encoder.FlushAsync(); |
| 86 | + memStream.Seek(0); |
| 87 | + fileStream.Seek(0); |
| 88 | + fileStream.Size = 0; |
81 | 89 |
|
82 | | - memStream.Seek(0); |
83 | | - fileStream.Seek(0); |
84 | | - fileStream.Size = 0; |
| 90 | + await RandomAccessStream.CopyAsync(memStream, fileStream); |
| 91 | + } |
| 92 | + catch (Exception ex) |
| 93 | + { |
| 94 | + var errorDialog = new ContentDialog() |
| 95 | + { |
| 96 | + Title = "FailedToRotateImage".GetLocalizedResource(), |
| 97 | + Content = ex.Message, |
| 98 | + PrimaryButtonText = "OK".GetLocalizedResource(), |
| 99 | + }; |
85 | 100 |
|
86 | | - await RandomAccessStream.CopyAsync(memStream, fileStream); |
| 101 | + if (ApiInformation.IsApiContractPresent("Windows.Foundation.UniversalApiContract", 8)) |
| 102 | + errorDialog.XamlRoot = MainWindow.Instance.Content.XamlRoot; |
| 103 | + |
| 104 | + await errorDialog.TryShowAsync(); |
| 105 | + } |
87 | 106 | } |
88 | 107 |
|
89 | 108 | /// <summary> |
|
0 commit comments