2012-12-21 134 views
2

我有一個自定義的PropertyItem,我已經「創建」並在圖像中存儲了一些值。我肯定它正確地存儲它,因爲新圖像的大小會增加(取決於我製作字符串的大小)。爲什麼我的自定義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是中保存的圖像中(至少它似乎是,因爲文件的尺寸越大,我把串越長),爲什麼它沒有被訪問我的方法,(getStringOut),當我回去嘗試從保存的圖像檢索PropertyItem?當我初始化新的位圖時,是不是由於某種原因拉扯EXIF數據?

回答

0

查看鏈接here。原來,我的代碼行與評論:// is this the problem code?的問題代碼,因爲這是剝離出元數據。

0

您是否試過this method - 本質上來說,在設置它之前複製一個屬性項並更改它的ID?

此外,你可能想看看this question。作者不嘗試(或者沒有描述自己試圖)檢索他的財產物品,但是他使用了一些不同的技術和PropertyItem屬性值(在他的回答中)。

+0

我試過這種方法 - 我想我明確地使用Image.SetPropertyItem()。 我已經看到了這個問題及其答案,並且您將看到我的程序中使用了他的方法(例如,使用CreatePropertyItem())。他確實使用了不同的技術和PropertyItem屬性值,但正如我在評論中所說的那樣,這不是我要問的問題。 –

+0

@ arthur.e.anderson啊,你說你沒有顯示你的真實數據和長度。在我看來,如果你沒有投入你認爲自己投入的東西,那麼你可能很難得到你期望的結果。因此提出了不同的價值觀。 –

+0

**我的問題仍然沒有得到解答。** 由於PropertyItem位於已保存的圖像中,因此一旦我返回並嘗試從該圖像檢索PropertyItem時,爲什麼它不能被我的方法訪問? –

相關問題