@@ -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 ;
0 commit comments