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

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

问题描述

我试图理解位图或图像的这两种方法。一个是 .SetPropertyItem(),另一个是 .GetPropertyItem()



我对文档说我要设置一个属性项目的方式感到十分困惑。



Microsoft文档它声明我们应该选择属性项目的id属性项目已经在图像中存在,给该属性项目一个新的ID,设置属性,然后设置图像属性项目与该检索属性项目。



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



令人困惑的是,我正在使用已经通过某种其他方式设置的属性并覆盖它的属性,然后用s将它添加回图像从属性ID列表中找到其他现有的ID。



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



这是我的一个例子谈论。 20752的ID是图像已有的属性项,并且当我设置20753是Image所具有的ID的PropertyItem的ID时。



这将成功设置属性项。

  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必须做所有那些怪异的东西?或者我在这里错过了什么?

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



我在这里做错了什么?

b
$ b

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

这样可以避免在PropertyItem上指定构造函数(这似乎阻止我使用System.Reflection )

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.

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.

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.

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.

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?

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.

This will successfully set the property item.

  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");
    }

This will successfully retrieve the 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);
        }

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.

What am I doing wrong here?

解决方案

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));

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

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

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