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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With