Skip to content

Commit 5daafde

Browse files
Update correcting-upside-down-image-imageeditor-dotnet-maui.md
1 parent e9eb135 commit 5daafde

File tree

1 file changed

+68
-68
lines changed

1 file changed

+68
-68
lines changed

knowledge-base/correcting-upside-down-image-imageeditor-dotnet-maui.md

Lines changed: 68 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ ticketid: 1691149
2020
</tr>
2121
<tr>
2222
<td> Version </td>
23-
<td> 8.0.0 </td>
23+
<td> 11.0.0 </td>
2424
</tr>
2525
</tbody>
2626
</table>
@@ -43,77 +43,77 @@ To ensure proper image orientation, handle EXIF metadata before loading the imag
4343
1. **Add Helper Methods**
4444
Implement the following methods to handle image loading and orientation:
4545

46-
```csharp
47-
public SKBitmap LoadBitmapWithOrigin(Stream imageStream, out SKEncodedOrigin origin)
48-
{
49-
if (imageStream.CanSeek)
50-
imageStream.Seek(0, SeekOrigin.Begin);
51-
52-
using (var codec = SKCodec.Create(imageStream))
53-
{
54-
if (codec == null)
55-
{
56-
origin = SKEncodedOrigin.Default;
57-
return null;
58-
}
59-
60-
origin = codec.EncodedOrigin;
61-
var info = new SKImageInfo(codec.Info.Width, codec.Info.Height);
62-
var bitmap = SKBitmap.Decode(codec, info);
63-
return bitmap ?? throw new InvalidOperationException("Failed to decode the bitmap.");
64-
}
65-
}
66-
67-
public static SKBitmap? AutoOrient(SKBitmap bitmap, SKEncodedOrigin origin)
68-
{
69-
switch (origin)
70-
{
71-
case SKEncodedOrigin.BottomRight:
72-
var rotated180 = new SKBitmap(bitmap.Width, bitmap.Height);
73-
using (var surface = new SKCanvas(rotated180))
74-
{
75-
surface.RotateDegrees(180, bitmap.Width / 2, bitmap.Height / 2);
76-
surface.DrawBitmap(bitmap, 0, 0);
77-
}
78-
return rotated180;
79-
case SKEncodedOrigin.RightTop:
80-
var rotated90 = new SKBitmap(bitmap.Height, bitmap.Width);
81-
using (var surface = new SKCanvas(rotated90))
82-
{
83-
surface.Translate(rotated90.Width, 0);
84-
surface.RotateDegrees(90);
85-
surface.DrawBitmap(bitmap, 0, 0);
86-
}
87-
return rotated90;
88-
case SKEncodedOrigin.LeftBottom:
89-
var rotated270 = new SKBitmap(bitmap.Height, bitmap.Width);
90-
using (var surface = new SKCanvas(rotated270))
91-
{
92-
surface.Translate(0, rotated270.Height);
93-
surface.RotateDegrees(270);
94-
surface.DrawBitmap(bitmap, 0, 0);
95-
}
96-
return rotated270;
97-
default:
98-
return bitmap;
99-
}
100-
}
101-
```
46+
```csharp
47+
public SKBitmap LoadBitmapWithOrigin(Stream imageStream, out SKEncodedOrigin origin)
48+
{
49+
if (imageStream.CanSeek)
50+
imageStream.Seek(0, SeekOrigin.Begin);
51+
52+
using (var codec = SKCodec.Create(imageStream))
53+
{
54+
if (codec == null)
55+
{
56+
origin = SKEncodedOrigin.Default;
57+
return null;
58+
}
59+
60+
origin = codec.EncodedOrigin;
61+
var info = new SKImageInfo(codec.Info.Width, codec.Info.Height);
62+
var bitmap = SKBitmap.Decode(codec, info);
63+
return bitmap ?? throw new InvalidOperationException("Failed to decode the bitmap.");
64+
}
65+
}
66+
67+
public static SKBitmap? AutoOrient(SKBitmap bitmap, SKEncodedOrigin origin)
68+
{
69+
switch (origin)
70+
{
71+
case SKEncodedOrigin.BottomRight:
72+
var rotated180 = new SKBitmap(bitmap.Width, bitmap.Height);
73+
using (var surface = new SKCanvas(rotated180))
74+
{
75+
surface.RotateDegrees(180, bitmap.Width / 2, bitmap.Height / 2);
76+
surface.DrawBitmap(bitmap, 0, 0);
77+
}
78+
return rotated180;
79+
case SKEncodedOrigin.RightTop:
80+
var rotated90 = new SKBitmap(bitmap.Height, bitmap.Width);
81+
using (var surface = new SKCanvas(rotated90))
82+
{
83+
surface.Translate(rotated90.Width, 0);
84+
surface.RotateDegrees(90);
85+
surface.DrawBitmap(bitmap, 0, 0);
86+
}
87+
return rotated90;
88+
case SKEncodedOrigin.LeftBottom:
89+
var rotated270 = new SKBitmap(bitmap.Height, bitmap.Width);
90+
using (var surface = new SKCanvas(rotated270))
91+
{
92+
surface.Translate(0, rotated270.Height);
93+
surface.RotateDegrees(270);
94+
surface.DrawBitmap(bitmap, 0, 0);
95+
}
96+
return rotated270;
97+
default:
98+
return bitmap;
99+
}
100+
}
101+
```
102102

103103
2. **Handle Image Metadata Before Loading**
104104
Use the helper methods to load and correct the image orientation.
105105

106-
```csharp
107-
private void LoadImage_Clicked(object sender, EventArgs e)
108-
{
109-
using var imageStream = File.OpenRead("MyImage.png");
110-
var bitmap = LoadBitmapWithOrigin(imageStream, out var origin);
111-
var rotatedBitmap = AutoOrient(bitmap, origin);
112-
113-
SKImage image = SKImage.FromPixels(rotatedBitmap.PeekPixels());
114-
imageEditor.Source = ImageSource.FromStream(() => image.Encode().AsStream());
115-
}
116-
```
106+
```csharp
107+
private void LoadImage_Clicked(object sender, EventArgs e)
108+
{
109+
using var imageStream = File.OpenRead("MyImage.png");
110+
var bitmap = LoadBitmapWithOrigin(imageStream, out var origin);
111+
var rotatedBitmap = AutoOrient(bitmap, origin);
112+
113+
SKImage image = SKImage.FromPixels(rotatedBitmap.PeekPixels());
114+
imageEditor.Source = ImageSource.FromStream(() => image.Encode().AsStream());
115+
}
116+
```
117117

118118
Replace `"MyImage.png"` with the appropriate file path. Use the corrected bitmap when loading the image into the ImageEditor.
119119

0 commit comments

Comments
 (0)