Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
213 views
in Technique[技术] by (71.8m points)

c# - How to get and set propertyitems for an image

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...