BitmapMetadata: some properties clearing?

880 views Asked by At

I'm dealing with this code: inside method call the metadata properties are all filled; but in user code most of them are nulled. Why this behaviour?

public static BitmapFrame GetImageIPTC(string imageFile)
{
    BitmapFrame meta;
    using (var fs = new FileStream(imageFile, FileMode.Open))
    {
        BitmapDecoder decoder = new JpegBitmapDecoder(fs, BitmapCreateOptions.None, BitmapCacheOption.None);
        meta = decoder.Frames[0];
        var a = ((BitmapMetadata) meta.Metadata).CameraModel;

    }
    return meta;

}

Inside the method the metadata properties are:

        ApplicationName "Adobe Photoshop CS5 Windows"   string
-       Author  Count = 1   System.Collections.ObjectModel.ReadOnlyCollection<string>
        [0] "Michele Virgilio"  string
        CameraManufacturer  "Canon" string
        CameraModel "Canon EOS 450D"    string
        Comment null    string
        Copyright   "© Michele Virgilio"    string
        DateTaken   "16/04/2011 10:30:48"   string
        Format  "jpg"   string
        IsFixedSize false   bool
        IsReadOnly  false   bool
+       Keywords    Count = 96  System.Collections.ObjectModel.ReadOnlyCollection<string>
        Location    "/" string
        Rating  0   int
        Subject "Subject description"   string
        Title   "Title description" string

User code:

var actual = ImageInfo.GetImageIPTC(filespec);
var bitmapMetadata = (BitmapMetadata)actual.Metadata;
var a = bitmapMetadata.CameraModel;

As you can see all properties are there but Subject:

        ApplicationName "Adobe Photoshop CS5 Windows"   string
-       Author  Count = 1   System.Collections.ObjectModel.ReadOnlyCollection<string>
        [0] "Michele Virgilio"  string
        CameraManufacturer  "Canon" string
        CameraModel "Canon EOS 450D"    string
        Comment null    string
        Copyright   "© Michele Virgilio"    string
        DateTaken   "16/04/2011 10:30:48"   string
        Format  "jpg"   string
        IsFixedSize false   bool
        IsReadOnly  true    bool
+       Keywords    Count = 96  System.Collections.ObjectModel.ReadOnlyCollection<string>
        Location    "/" string
        Rating  0   int
        Subject null    string
        Title   "Title description" string
1

There are 1 answers

3
Fischermaen On

When going out of the using-scope the BitmapDecoder will be disposed. Check the meta in debugger on line return meta and you will see.