C# 为什么我的自定义属性项无法从映像中拉出?

C# 为什么我的自定义属性项无法从映像中拉出?,c#,image,bitmap,C#,Image,Bitmap,我有一个自定义PropertyItem,我已经“创建”了它,并在图像中存储了一些值。我确信它正确地存储了它,因为新图像的大小将增加(取决于我制作的字符串的大小) --[问题在底部]-- 下面是我将PropertyItem放入图像中的代码 Bitmap bmp = new Bitmap(image); //image is of type Image string s = "Hello there"; var pi = createPropertyItem(); pi.Id = 40091; pi

我有一个自定义PropertyItem,我已经“创建”了它,并在图像中存储了一些值。我确信它正确地存储了它,因为新图像的大小将增加(取决于我制作的字符串的大小)

--[问题在底部]--

下面是我将PropertyItem放入图像中的代码

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"?
无论如何,我在其他地方遇到了麻烦-我需要从图像的PropertyItem中提取值。然而:

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;
}
我的问题是:既然PropertyItem在保存的图像中(至少看起来是这样,因为文件的大小随着我输入的字符串的长度而变大),那么当我返回并尝试从保存的图像中检索PropertyItem时,为什么我的方法(getStringOut)不能访问它?当我初始化新位图时,它不是出于某种原因拉取EXIF数据吗?

您是否尝试过——基本上,在设置属性项之前复制属性项并更改其ID


另外,你可能想看看。作者没有尝试(或没有描述自己正在尝试)检索他的属性项,但他使用了一些与您不同的技术和PropertyItem属性值(在他的回答中)

参见链接。结果是我的代码行带有注释:
//这是问题代码吗?
是问题代码,因为这是剥离元数据。

我尝试过这种方法-我认为我非常明确地使用了Image.SetPropertyItem()。我已经看到了这个问题及其答案,您将看到我在程序中使用了他的方法(例如使用CreatePropertyItem()。他确实使用了不同的技术和属性项目属性值,但正如我在评论中所说,这不是我要问的问题。@arthur.e.anderson啊,你说你没有显示你的真实数据和长度。在我看来,如果你没有投入你认为投入的东西,你可能很难得到你期望的东西。我的问题至今仍未得到回答。既然PropertyItem在保存的映像中,为什么在我返回并尝试从该映像中检索PropertyItem后,我的方法无法访问它?