c# - 用C#读取图像宽度和高度错误

标签 c# image height width

我遇到了一个奇怪的问题。有一个image 。不幸的是它太大了,无法显示它。但你可以下载它。如果您无法从 OneDrive 执行此操作 this是另一种方式。

这张图片看似普通,其实不然。

当我们打开属性时,我们将看到:

Image properties in Windows

我们需要记住这张图片的尺寸:宽度为 3000 像素,高度为 4000 像素。它看起来是正确的,因为图像是 portret。

然后我们尝试用 C# 来读取它:

private static void TestImage()
{
    using (FileStream file1 = new FileStream("DSC_2446.JPG", FileMode.Open))
    {                
        Console.WriteLine("DSC_2446.JPG :");
        using (var img1 = System.Drawing.Image.FromStream(file1))
        {
            Console.WriteLine($" Width = {img1.Width}");
            Console.WriteLine($" Height = {img1.Height}");
        } 
    }                

    Console.Read();
}

在结果中我们看到了一些魔法!!!

Magic results

所以我的值(value)观完全错误。值在属性之间切换。 有人知道为什么会发生这种情况以及如何检测/修复这种行为吗?

最佳答案

问题出在 EXIF 版本上。您可以通过本站获取真实数据https://exif.tools/meta/Exif-Version/0231你会看到

enter image description here

也按照这个Post您可以通过以下方式获取图像的方向

var orientation = (int)img1.GetPropertyItem(274).Value[0];
//orientation = 6

6表示旋转90度。

有值 6 的引用。https://exiftool.org/TagNames/EXIF.html

enter image description here

关于c# - 用C#读取图像宽度和高度错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74948926/

相关文章:

c# - 控制 WCF XML 输出中的命名空间前缀

C#:在 C# 4.5 中等待请求完成

java - 为什么 ImageIO.write() 方法修改像素值?

安卓开发 : How do I reduce app size?

css - 空 Div 需要是父级的 100%

c# - 在 Stream Read 上实现超时属性

java - 最佳实践问题 : How to save a collection of images and a java object in a single file? 读取文件以进行呈现

javascript - 当域和协议(protocol)匹配时获取 iframe 内容高度,但子域不匹配

html - 无论分辨率如何,如何使 div 延伸到屏幕底部?

c# - 在任何子方法之前(或之后)调用父方法