天天看點

.NET : 如何讀取圖檔中的中繼資料資訊

如果我們希望在程式中對圖檔中包含的一些中繼資料資訊進行讀取,并作為後期分析的準備。那麼可以參考下面這個例子

http://www.codeproject.com/KB/graphics/photoproperties.aspx

我在這基礎上也做了一個範例,如下圖的效果

.NET : 如何讀取圖檔中的中繼資料資訊

代碼大緻如下

private void btSelectImageFile_Click(object sender, EventArgs e)

{

    OpenFileDialog dialog = new OpenFileDialog();

    dialog.Filter = "Image File (*.jpg)|*.jpg";

    if (dialog.ShowDialog() == DialogResult.OK)

    {

        pbImage.Image = Image.FromFile(dialog.FileName);

        JSG.PhotoPropertiesLibrary.PhotoProperties p = new JSG.PhotoPropertiesLibrary.PhotoProperties();

        p.Initialize();

        p.Analyze(dialog.FileName);

        JSG.PhotoPropertiesLibrary.ResultOptions options = new JSG.PhotoPropertiesLibrary.ResultOptions();

        MemoryStream ms = new MemoryStream();

        p.WriteXml(ms, options);

        if (ms.CanRead == false)

        {

            byte[] buffer = ms.GetBuffer();

            ms = new MemoryStream(buffer);

        }

        XDocument doc = XDocument.Load(new XmlTextReader(ms), LoadOptions.PreserveWhitespace);