Why is my custom PropertyItem not able to be pulle

2019-07-19 02:30发布

I have a custom PropertyItem that I've "created" and stored some values in within an image. I'm positive that it's correctly storing it because the new image will increase in size, (depending on how big I make the string).

--[Question is at the bottom]--

Here's the code in which I put the PropertyItem into the image.

Bitmap bmp = new Bitmap(image); //image is of type Image
string s = "Hello there";
var pi = createPropertyItem();
pi.Id = 40091;
pi.Len = s.Length+1; //don't worry too much about this - this is a test case,
                     //not the real types and lengths. If I put an error in here,
                     //it's not what's making it error out elsewhere. :)
pi.Type = 2;
pi.Value = s;
bmp.SetPropertyItem(pi);

bmp.Save("image.jpg", bmp.RawFormat);//is this actually saving the PropertyItems
                                     //into the file, "image.jpg"?

Anyways, I'm having trouble elsewhere - I need to extract the value from the PropertyItem of the image. However:

Bitmap thing = new Bitmap("image.jpg"); // is this the problem code?
String newString = getStringOut(thing);

/////////////////further down in the program//////////////////////////

public String getStringOut(Image image)
{
    var x = image.GetPropertyItem(40091); //this fails because the PropertyItem
                                          //is not in this image.
    string s = x.Value;
}

My question is: since the PropertyItem is in the image that's saved (at least it appears to be, since the file's size gets larger the longer the string I put in), why is it NOT being accessed by my method, (getStringOut), when I go back and try and retrieve the PropertyItem from the saved image? When I initialize the new Bitmap, is it not pulling the EXIF data for some reason?

标签: c# image bitmap
2条回答
等我变得足够好
2楼-- · 2019-07-19 03:23

Have you tried this method - essentially, copying a property item and changing its ID before setting it?

Also, you might want to look at this question. The author doesn't attempt (or doesn't describe himself as attempting) to retrieve his property item, but he uses some different techniques and PropertyItem property values than you do (in his answer).

查看更多
神经病院院长
3楼-- · 2019-07-19 03:25

See link here. Turns out that my line of code with the comment: // is this the problem code? is the problem code, because this is stripping out the metadata.

查看更多
登录 后发表回答