如何获取和设置图像的属性项 [英] How to get and set propertyitems for an image

查看:22
本文介绍了如何获取和设置图像的属性项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试理解 Bitmap 或 Image 的这两种方法.一个是 .SetPropertyItem(),另一个是 .GetPropertyItem().

I'm trying to understand these two methods of the Bitmap or Image. One being .SetPropertyItem() and the other being .GetPropertyItem().

我对文档说我要设置属性项的方式完全感到困惑.

I'm completely confused as to the way the documentation says that I am to set a property item.

Microsoft 文档 中指出我们应该选择通过图像中已存在的属性项的 id 来设置属性项,为该属性项指定一个新 ID,设置属性,然后使用检索到的属性项设置图像属性项.

From the Microsoft Documentation it is stating that we should choose a property item by an id of a property item that already exist in the image, give that property item a new ID, set the properties and then set the image property item with that retrieved property item.

这太奇怪了,但真正让我感到困惑的是,我们不能将属性项的 id 设置为任何 ID,我们必须将属性项 id 设置为属性项 id 列表中已存在的 id.

That is so weird, but what really gets me is that we cannot just set the id of the property item to any ID we've got to set the property item id to an id that already exist in the property item id list.

令人困惑的是,我正在使用一个已经通过其他方式设置的属性并覆盖它的属性,然后将其添加回带有属性 ID 列表中的其他一些现有 ID 的图像.

What is confusing is that I'm using a property that is already set by some other manner and overriding it's property and then adding it back to the image with some other existing ID from the property id list.

我在这里错过了什么?有没有一种方法可以简单地创建一个新的 PropertyItem,然后将具有任何给定 ID 的属性项添加到图像或位图中?

What am I missing here? Is there not a way to simply create a new PropertyItem and then add that property item with any given ID to the image or bitmap?

这是我正在谈论的一个例子.20752的ID是图片已有的属性项,当我设置属性项的ID时,20753也是图像的ID.

Here is an example of what I am talking about. The ID that is of 20752 is a propertyitem that the image already has, and when I set the ID of the PropertyItem that 20753 is an ID that the Image has as well.

这将成功设置属性项.

  private void Form1_Load(object sender, EventArgs e)
    {

        string path = Environment.CurrentDirectory + @"sample.png";
        Image image = Image.FromFile(path);

        PropertyItem pi = image.GetPropertyItem(20752);

        pi.Id = 20753;

        pi.Type = 1;
        pi.Value = Encoding.UTF8.GetBytes("MyImageInfo");
        pi.Len = pi.Value.Length;
        image.SetPropertyItem(pi);

        image.Save(Environment.CurrentDirectory + @"sample2.png");
    }

这将成功检索到 PropertyItem.

  private void Form1_Load(object sender, EventArgs e)
        {

            string path = Environment.CurrentDirectory + @"sample.png";
            Image image = Image.FromFile(path);

            PropertyItem propItem = image.GetPropertyItem(20753);
        }

有没有一种方法可以让我用我自己的 ID 创建一个新的 PropertyItem 而不必做那些奇怪的事情?还是我在这里遗漏了什么?

It there a way that I can create a new PropertyItem with my own ID without having to do all that weird stuff? Or am I missing something here?

或者,是为图像或位图设置其他类型属性的更好方法吗?我正在寻找一种基本的方法来保存信息并在以后检索它.

And or, is the a better way to set other types of property's for an image or bitmap? I am looking for a basic way to save information and retrieve it at a later date.

我在这里做错了什么?

推荐答案

这有点晚了,但是可以创建一个新的 PropertyItem 实例,而不必经历必须从另一个来源建立一个实例的痛苦:

This is somewhat late, but it IS possible to create a new PropertyItem instance, without going through the pain of having to establish one from another source:

using System.Runtime.Serialization;
...
var newItem = (PropertyItem)FormatterServices.GetUninitializedObject(typeof(PropertyItem));

这解决了缺少在 PropertyItem 上指定的构造函数的问题(这似乎阻止了我使用 System.Reflection)

This gets around the lack of constructor specified on PropertyItem (which seemed to prevent me from using System.Reflection)

这篇关于如何获取和设置图像的属性项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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