0

I'm having a problem when fetching an image using Image.GetInstance. The photo is in portrait but when I did the Image.GetInstance(), my image is rotated.

string imagepath = AttachmentURL + answer.Attachment.AzureFileName;
Image image = Image.GetInstance(imagepath);

My image: https://zensyd.blob.core.windows.net/fileattachments/f9ab6a11-13c1-421a-abdd-4b4a6d701033.jpg

This means 2016 width and 1512 height.

enter image description here

eglease
  • 2,445
  • 11
  • 18
  • 28
Khen
  • 115
  • 11
  • 1
    Don't know much about iTextSharp, but that's probably because you image itself is not rotated. Instead it has EXIF Orientation tag set, which defines that rotation (so image itself is 2016x1512, and there is a tag which says "when displayed - rotate it 90 degrees). Probably iTextSharp ignores this EXIF tag. – Evk Oct 19 '21 at 12:32
  • Thanks to your idea @Evk!, Fixed mine by using System.Drawing Image and get the exif data. Then convert the System.Drawing image to ITextSharp – Khen Oct 19 '21 at 14:10

1 Answers1

0

I found this answer regarding image orientation rotation via exif but I need to convert the uri first to a memory stream. I then found this solution that converts URI to a MemoryStream. Then MemoryStream to System.Drawing Image.

Here's my solution

BaseColor color = null;
string imagepath = AttachmentURL + answer.Attachment.AzureFileName;
var imgStream = GetStreamFromUrl(imagepath);
var tempImg = System.Drawing.Image.FromStream(imgStream);
ExifRotate(tempImg); 
Image image = Image.GetInstance(tempImg, color);

GetStreamFromUrl is balexandre's answer here

ExifRotate is saucecontrol's answer here.

Khen
  • 115
  • 11