简体   繁体   English

使用 C# 读取时图像宽度和高度错误

[英]Wrong image width and height when read it with C#

I've got a strange problem.我有一个奇怪的问题。 There is an image .有一个图像 Unfortunately it's too big to show it in question.不幸的是,它太大了,无法显示它。 But you can download it.但是你可以下载它。 If you can't do it from OneDrive this is another way.如果您不能从 OneDrive 做到这一点,是另一种方法。

This image seems to be ordinary, but it's not.这个形象看似普通,实则不然。

When we open properties we will see this:当我们打开属性时,我们将看到:

Windows 中的图像属性

We need to keep in mind dimensions of this picture: Width is 3000px and Height is 4000px.我们需要记住这张图片的尺寸:宽度是 3000px,高度是 4000px。 It looks correct because image is portret.它看起来是正确的,因为图像是 portret。

Then lets try to read it with C#:然后让我们尝试用 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();
}

And in results we see some magic!!!结果我们看到了一些神奇的东西!!!

神奇的结果

So I've got completely wrong values.所以我的价值观完全错误。 Values are switched between properties.值在属性之间切换。 Does someone know why it can happens and how to detect/fix this behavior?有人知道为什么会发生这种情况以及如何检测/修复这种行为吗?

The problem is the EXIF version.问题是EXIF版本。 You can use this site to get the real data https://exif.tools/meta/Exif-Version/0231 and you will see您可以使用此站点获取真实数据https://exif.tools/meta/Exif-Version/0231您将看到

在此处输入图像描述

Also according to this Post you can get your image's orientation by另外根据这篇文章,您可以通过以下方式获取图片的方向

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

the value 6 means rotate 90 degrees.6表示旋转 90 度。

There is the reference of the value 6. https://exiftool.org/TagNames/EXIF.html有值6的参考。https://exiftool.org/TagNames/EXIF.html

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM