提取图像文件元数据 [英] Extract image file metadata

查看:40
本文介绍了提取图像文件元数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够在不打开文件的情况下提取图像的元数据和扩展属性.换句话说,当您在 Windows 中右键单击文件并选择详细信息"时,我如何以编程方式访问显示的信息?标签?

I want to be able to extract an image's metadata and extended properties without opening the file. In other words, how do I programatically access the information shown when you right-click a file in windows and select the "Details" tab?

使用 .Net Framework 4.5 及更高版本执行此操作的正确方法是什么?我找到了各种方法来做到这一点,但这些文章都参考了 .Net Framework 2,至少可以说是乏味的,可能已经过时了.具体来说,是否有任何库可以轻松实现这一点?

What is the correct way to do this using .Net Framework 4.5 and higher? I have found various ways of doing this, but the articles all refer to .Net Framework 2 and is, to say the least, tedious and probably outdated. Specifically, are there any libraries which makes this easy to do?

我需要能够获取以下格式的图像的高度和宽度尺寸:png、jpg、jpeg、gif、bmp 和 pdf(假设 pdf 仅包含 1 页是安全的).

I need to be able to get the height and width dimensions for images in the following formats: png, jpg, jpeg, gif, bmp and pdf (it is safe to assume that the pdf consists of 1 page only).

推荐答案

我一直在努力实现这一目标.以下是我的发现.

I was playing around to accomplish this. Below are my findings.

  1. 您可以使用 System.Drawing 和 System.Drawing.Imaging 命名空间
  2. 后者是通过 using 语句导入并执行 Add reference => Assemblies => Framework => System.Drawing
  3. 找出存储在 PropertyItems Id 值中的十六进制值的含义.
  4. 使用 ASCII 转换器将它们转换为人类可读的文本.*
  5. 打印到屏幕,或其他.
  1. You can use System.Drawing and System.Drawing.Imaging namespace
  2. The latter is imported by using statement and doing Add reference => Assemblies => Framework => System.Drawing
  3. Find out what the hexadecimal values mean that are stored in PropertyItems Id value.
  4. Convert them to human readable text with ASCII converter.*
  5. Print to screen, or whatever.

*请注意:ASCII 转换器打印出一堆奇怪的字符(例如,♦ ☺ ☺ ☻ ♥ ♣ ☺ ☺ ☺ ☻ ☺ ☺ ☻ ♥ ♣ ☺ ☻ ♥ ♦ ♀ ☻ ♥ ♥ ♥ ♥ ♀ ☻一些属性.您可能需要其他编码.如果有人知道哪个,请告诉我.

*Please note: ASCII converter prints a bunch of strange characters (e.g., "♦ ☺ ☺ ☻ ♥ ♣ ☺ ☺ ☺ ☻ ☺ ☺ ☻ ♥ ♣ ☺ ☻ ♥ ♦ ♀ ☻ ♥ ♣") and my laptop starts beeping with some properties. You probably need other encoding for those. If anyone knows which, let me know.

using System;
using System.Drawing.Imaging;
using System.Drawing;
using System.Text;

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            // Add reference => Assemblies => Framework => System.Drawing

            // Set the path to your photo file.
            var path = @"[Path to your image]";

            // Create image object
            var image = new Bitmap(path);

            // Get PropertyItems
            var propItems = image.PropertyItems;

            // Remove encoding object
            var encodings = new ASCIIEncoding();

            // Display 
            foreach (var propItem in propItems)
            {

                // The values below come from http://msdn.microsoft.com/en-us/library/xddt0dz7(v=vs.110).aspx
                // Find al values available with Console.WriteLine(propItem.Id.ToString("x"));

                if (propItem.Id.ToString("x").Equals("110")) //Equipment model
                {
                    var model = encodings.GetString(propItem.Value);
                    Console.WriteLine("Model:\t" + model);
                }

                if (propItem.Id.ToString("x").Equals("9003")) //ExifDTOriginal
                {
                    var datetime = encodings.GetString(propItem.Value);
                    Console.WriteLine("Taken:\t" + datetime);
                }


            }
            Console.Read();

        }

    }
}

    // =>
    //    Model:  FinePix S9500
    //    Taken:  2012:11:30 13:58:04

希望这会有所帮助.:-)

Hope this helps. :-)

/Edit as a comment by sasha_gud 状态,使用 BitmapSource 类,它通过 Metadata 属性公开 EXIF 元数据 可能更容易.

/Edit as a comment by sasha_gud states, using the BitmapSource class, which exposes the EXIF metadata through the Metadata property is probably easier.

这篇关于提取图像文件元数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆