Skip to content

Commit 5cc0e07

Browse files
committed
PR Feedback
1 parent d00e979 commit 5cc0e07

File tree

2 files changed

+26
-9
lines changed

2 files changed

+26
-9
lines changed

src/Essentials/src/MediaPicker/ImageProcessor.android.cs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,16 @@ internal static partial class ImageProcessor
1414
{
1515
public static partial async Task<Stream> RotateImageAsync(Stream inputStream, string? originalFileName)
1616
{
17-
if (inputStream == null)
17+
if (inputStream is null)
18+
{
1819
return new MemoryStream();
20+
}
1921

2022
// Reset stream position
2123
if (inputStream.CanSeek)
24+
{
2225
inputStream.Position = 0;
26+
}
2327

2428
// Read the input stream into a byte array
2529
byte[] bytes;
@@ -32,9 +36,11 @@ public static partial async Task<Stream> RotateImageAsync(Stream inputStream, st
3236
try
3337
{
3438
// Load the bitmap from bytes
35-
var originalBitmap = await Task.Run(() => BitmapFactory.DecodeByteArray(bytes, 0, bytes.Length));
36-
if (originalBitmap == null)
39+
using var originalBitmap = await Task.Run(() => BitmapFactory.DecodeByteArray(bytes, 0, bytes.Length));
40+
if (originalBitmap is null)
41+
{
3742
return new MemoryStream(bytes);
43+
}
3844

3945
// Get EXIF orientation
4046
int orientation = GetExifOrientation(bytes);
@@ -47,19 +53,21 @@ public static partial async Task<Stream> RotateImageAsync(Stream inputStream, st
4753

4854
// Apply EXIF orientation correction using SetRotate(0) to preserve original EXIF behavior
4955
Bitmap? rotatedBitmap = ApplyExifOrientation(originalBitmap);
50-
if (rotatedBitmap == null)
56+
if (rotatedBitmap is null)
5157
{
5258
return new MemoryStream(bytes);
5359
}
5460

5561
// Clean up the original bitmap if we created a new one
5662
if (rotatedBitmap != originalBitmap)
63+
{
5764
originalBitmap.Recycle();
65+
}
5866

5967
// Convert the rotated bitmap back to a stream
6068
var resultStream = new MemoryStream();
6169
bool usePng = !string.IsNullOrEmpty(originalFileName) &&
62-
Path.GetExtension(originalFileName).ToLowerInvariant() == ".png";
70+
Path.GetExtension(originalFileName).Equals(".png", StringComparison.OrdinalIgnoreCase);
6371

6472
var compressResult = await Task.Run(() =>
6573
{
@@ -86,7 +94,9 @@ public static partial async Task<Stream> RotateImageAsync(Stream inputStream, st
8694
});
8795

8896
if (!compressResult)
97+
{
8998
return new MemoryStream(bytes);
99+
}
90100

91101
resultStream.Position = 0;
92102
return resultStream;

src/Essentials/src/MediaPicker/ImageProcessor.windows.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public static partial async Task<Stream> RotateImageAsync(Stream inputStream, st
2323
var decoder = await BitmapDecoder.CreateAsync(randomAccessStream);
2424

2525
// Check if rotation is needed
26-
var orientation = GetImageOrientation(decoder);
26+
var orientation = await GetImageOrientation(decoder);
2727
if (orientation == BitmapRotation.None)
2828
{
2929
inputStream.Position = 0;
@@ -34,7 +34,14 @@ public static partial async Task<Stream> RotateImageAsync(Stream inputStream, st
3434
var outputStream = new InMemoryRandomAccessStream();
3535

3636
// Create encoder
37-
var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, outputStream);
37+
Guid encoderId = BitmapEncoder.JpegEncoderId;
38+
39+
if (Path.GetExtension(originalFileName).Equals(".png", StringComparison.OrdinalIgnoreCase))
40+
{
41+
encoderId = BitmapEncoder.PngEncoderId;
42+
}
43+
44+
var encoder = await BitmapEncoder.CreateAsync(encoderId, outputStream);
3845

3946
// Set the transform with rotation
4047
encoder.BitmapTransform.Rotation = orientation;
@@ -65,13 +72,13 @@ public static partial async Task<Stream> RotateImageAsync(Stream inputStream, st
6572
}
6673
}
6774

68-
static BitmapRotation GetImageOrientation(BitmapDecoder decoder)
75+
static async Task<BitmapRotation> GetImageOrientation(BitmapDecoder decoder)
6976
{
7077
try
7178
{
7279
// Try to get the EXIF orientation
7380
var properties = decoder.BitmapProperties;
74-
var orientationProperty = properties.GetPropertiesAsync(new[] { "System.Photo.Orientation" }).GetAwaiter().GetResult();
81+
var orientationProperty = await properties.GetPropertiesAsync(new[] { "System.Photo.Orientation" });
7582

7683
if (orientationProperty.TryGetValue("System.Photo.Orientation", out var orientationValue) &&
7784
orientationValue.Value is ushort orientation)

0 commit comments

Comments
 (0)