我有下面的代碼段,它打印使用WPF成像API的圖像的元數據:元數據爲空在Windows XP SP3 GIF圖像(正常工作在Windows 7)
void Main()
{
Uri imageUri = new Uri(@"E:\tmp\bomb.gif");
var decoder = BitmapDecoder.Create(imageUri, BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
var f = decoder.Frames[0];
var m = f.Metadata as BitmapMetadata;
DumpMetadata(m);
}
void DumpMetadata(BitmapMetadata metadata)
{
DumpMetadata(metadata, 0);
}
void DumpMetadata(BitmapMetadata metadata, int indentLevel)
{
if (metadata == null)
{
Console.WriteLine ("(No metadata)");
return;
}
string indent = new string('\t', indentLevel);
foreach (var propertyName in metadata)
{
object value = metadata.GetQuery(propertyName);
BitmapMetadata complexProperty = value as BitmapMetadata;
if (complexProperty != null)
{
Console.WriteLine("{0}{1} =", indent, propertyName);
DumpMetadata(complexProperty, indentLevel + 1);
}
else
{
Type type = null;
if (value != null)
type = value.GetType();
Console.WriteLine("{0}{1} = {2} ({3})", indent, propertyName, value, type);
}
}
}
在Windows 7,它給出正確的結果:
/imgdesc =
/Left = 0 (System.UInt16)
/Top = 0 (System.UInt16)
/Width = 78 (System.UInt16)
/Height = 121 (System.UInt16)
/LocalColorTableFlag = False (System.Boolean)
/InterlaceFlag = False (System.Boolean)
/SortFlag = False (System.Boolean)
/LocalColorTableSize = 7 (System.Byte)
/grctlext =
/Disposal = 2 (System.Byte)
/UserInputFlag = False (System.Boolean)
/TransparencyFlag = True (System.Boolean)
/Delay = 10 (System.UInt16)
/TransparentColorIndex = 0 (System.Byte)
但在Windows XP(SP3),該Metadata
屬性始終是空的GIF圖像(動畫與否)。
我認爲這個問題可能與Windows Imaging Component有關,但WIC應該與SP3一起安裝(也是.NET 3.0的一部分;我在該PC上安裝了3.5 SP1和4.0),所以它應該可以正常工作在XP上也是如此。
不知道這是怎麼回事...上難道只是缺少編解碼器?有沒有辦法讓這個代碼在XP上工作?
編輯:我所需要的信息是所述Delay
場(幀的持續時間);如果我不能得到BitmapMetadata
,有另一種方式來提取這些信息?理想情況下,我寧願只使用WPF映像API(即不使用GDI/System.Drawing)。
更新:我貼出了同樣的問題on the MSDN forums,並根據我收到的答案,GIF元數據僅支持Windows 7和Vista上使用平臺更新(see here和there)。所以我想這是不可能使它使用WPF圖像API在XP上工作的。我可能會最終使用System.Drawing中,但我接受更好的建議...
謝謝,但我寧願不使用System.Drawing,並且我已經有一個自定義解碼器可以完成這項工作 – 2014-05-29 07:45:06
因爲你喜歡......我只是指出它,因爲有一種方法只是使用可用的.Net框架。不過,當然,如果你使用的解碼器已經是你的DLL部署的一部分,那麼我們沒有理由改變現在。 – 2014-05-29 09:41:34