【问题标题】:ITextSharp Image getInstance rotates my imageITextSharp Image getInstance 旋转我的图像
【发布时间】:2021-10-19 12:20:09
【问题描述】:

我在使用Image.GetInstance 获取图像时遇到问题。这张照片是纵向的,但是当我做Image.GetInstance() 时,我的图像被旋转了。

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

我的形象: https://zensyd.blob.core.windows.net/fileattachments/f9ab6a11-13c1-421a-abdd-4b4a6d701033.jpg

这意味着 2016 宽度和 1512 高度。

【问题讨论】:

  • 对iTextSharp不太了解,但这可能是因为您的图像本身没有旋转。相反,它有 EXIF Orientation 标签集,它定义了旋转(所以图像本身是 2016x1512,并且有一个标签说“显示时 - 旋转 90 度)。iTextSharp 可能会忽略这个 EXIF 标签。
  • 感谢您的想法@Evk!,通过使用 System.Drawing Image 修复了我的问题并获取 exif 数据。然后将 System.Drawing 图像转换为 ITextSharp

标签: c# image itext


【解决方案1】:

我发现这个answer 关于通过 exif 旋转图像方向,但我需要先将 uri 转换为内存流。然后我发现了这个solution,它将URI 转换为MemoryStream。然后 MemoryStream 到 System.Drawing Image。

这是我的解决方案

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 是 balexandre 的回答 here

ExifRotate 是 saucecontrol 的回答 here

【讨论】:

    猜你喜欢
    • 2015-06-18
    • 2018-12-27
    • 2021-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-06
    • 1970-01-01
    相关资源
    最近更新 更多