How to get and set propertyitems for an image
Asked Answered
M

2

15

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?

Mabellemable answered 16/9, 2013 at 4:41 Comment(0)
A
43

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)

Astto answered 6/8, 2014 at 14:17 Comment(2)
One caveat if you use this technique elsewhere. This bypasses the Type's constructor and gives you back, as the method implies, just an uninitialized object -- a typed reference to some allocated memory. I still gave it a +1 because it greatly simplified my code in which I was following Microsoft's recommendation to query another PropertyItem and change its ID, etc. <grumble>So messy...</grumble>Famed
For VB.NET, you'd use Dim pi As PropertyItem = CType(FormatterServices.GetUninitializedObject(GetType(PropertyItem)), PropertyItem)Revolution
D
0

Late response and no comments allowed. I would have commented on ne1410s comment when he stated that he was unable to use System.Reflection.BindingFlags.

System.Reflection.BindingFlags flags = (System.Reflection.BindingFlags)(-20);
PropertyItem pi = (PropertyItem)typeof(PropertyItem).Assembly.CreateInstance("System.Drawing.Imaging.PropertyItem", false, flags, null, null, null, null);
Drava answered 9/7, 2023 at 21:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.