Skip to content

Commit e9eb135

Browse files
author
KB Bot
committed
Added new kb article correcting-upside-down-image-imageeditor-dotnet-maui
1 parent 85867a1 commit e9eb135

File tree

1 file changed

+124
-0
lines changed

1 file changed

+124
-0
lines changed
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
---
2+
title: Correcting Upside Down Image Display in ImageEditor for .NET MAUI
3+
description: Resolve the issue of upside-down image display in ImageEditor for .NET MAUI on iOS by handling EXIF metadata.
4+
type: how-to
5+
page_title: Fixing Upside Down Image in ImageEditor for .NET MAUI on iOS
6+
meta_title: Fixing Upside Down Image in ImageEditor for .NET MAUI on iOS
7+
slug: correcting-upside-down-image-imageeditor-dotnet-maui
8+
tags: imageeditor,.net maui,ios,skia,exif,bitmap,image-orientation,image-metadata
9+
res_type: kb
10+
ticketid: 1691149
11+
---
12+
13+
## Environment
14+
15+
<table>
16+
<tbody>
17+
<tr>
18+
<td> Product </td>
19+
<td> ImageEditor for .NET MAUI </td>
20+
</tr>
21+
<tr>
22+
<td> Version </td>
23+
<td> 8.0.0 </td>
24+
</tr>
25+
</tbody>
26+
</table>
27+
28+
## Description
29+
30+
Images loaded into the [ImageEditor](https://docs.telerik.com/devtools/maui/controls/imageeditor/overview) for .NET MAUI on iOS may appear upside down due to how SkiaSharp handles EXIF metadata. This metadata instructs the renderer to apply a specific rotation to the image, causing the display issue.
31+
32+
This knowledge base article also answers the following questions:
33+
- How to fix upside-down images in ImageEditor for .NET MAUI?
34+
- Why are images rotated incorrectly in ImageEditor on iOS?
35+
- How to handle EXIF metadata for ImageEditor in .NET MAUI?
36+
37+
## Solution
38+
39+
To ensure proper image orientation, handle EXIF metadata before loading the image into the ImageEditor.
40+
41+
### Steps to Correct Image Orientation
42+
43+
1. **Add Helper Methods**
44+
Implement the following methods to handle image loading and orientation:
45+
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+
```
102+
103+
2. **Handle Image Metadata Before Loading**
104+
Use the helper methods to load and correct the image orientation.
105+
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+
```
117+
118+
Replace `"MyImage.png"` with the appropriate file path. Use the corrected bitmap when loading the image into the ImageEditor.
119+
120+
## See Also
121+
122+
- [ImageEditor Documentation](https://docs.telerik.com/devtools/maui/controls/imageeditor/overview)
123+
- [ImageEditor iOS EXIF Issue Discussion](https://feedback.telerik.com/maui/1600114-imageeditor-some-images-are-rotated-initially-when-displaying-in-the-editor)
124+
- [SkiaSharp API Reference](https://docs.microsoft.com/en-us/dotnet/api/skiasharp)

0 commit comments

Comments
 (0)