Q:

How do I dispose Bitmap / free memory after I got the requried data from BitmapMetadata?

I’m working on a solution,  which needs to support the query of many jpeg-files metadata to get the Comment- and DateTaken-Property. But I always get an OutOfMemoryException after about 20 files. So I need to know how I free the memory after I assigned the Comment- and DateTaken-values to the two strings. Currently I’m using a JpegBitmapDecoder and set the BitmapCacheOption by instantiation of the decoder to OnLoad. I think, that’s why I get the exception, but I don’t know how to make it better, since I work the first time with Images / Bitmaps.
In a separate form I have a BackgroundWorker, in which a method from the mainform is called. In that method two methods are called, to get the Comment and the DateTaken from the file. To get the metadata of the file in that two methods another method is called, in which I get the exception in the line, I instantiate the JpegBitmapDecoder.
Here are the methods:
In formPleaseWait:
private void bgwLoadPhotos_DoWork(object sender, DoWorkEventArgs e)
 //BackgroundWorker in formPleaseWait
    {
        int filesCount;
        FileInfo[] files;

        FolderBrowserDialog browseFolder = (FolderBrowserDialog)e.Argument;

        files = selectedFolder.GetFiles("*.jpg");
        filesCount = files.Count();

        foreach (FileInfo file in selectedFolder.GetFiles("*.jpg"))
        {
            try
            {
                Photo photo = formMain.CreatePhotoObject(file);
        //the method in the mainform is called

                loadedPhotos.Add(photo);
                loadProgress++;

                if (loadProgress == filesCount)
                {
                    loadProgress = 100;
                }
            }
            catch (OutOfMemoryException)
            {
                MessageBox.Show(
                     MessageBoxButtons.OK, MessageBoxIcon.Information);

                loadProgress = 100;
                break;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }

In formMain:
    public Photo CreatePhotoObject(FileInfo file)
    {   

        int year = 0;
        string comment = GetPhotoComment(file);
        string dateString = GetPhotoDate(file);
        //the two methods to get comment and date are called
        string filename = file.Name;
        string path = selectedFolder + @"\" + filename;            
        string photoname = Path.GetFileNameWithoutExtension(file.FullName);
        DateTime date = new DateTime();

        if(dateString != null)
        {
            date = DateTime.Parse(dateString).Date;
            dateString = date.ToShortDateString();
            year = date.Year;
        }

        return new Photo(year, comment, filename, photoname, dateString);
    }

    private string GetPhotoComment(FileInfo file)
    {
        BitmapMetadata metadata = GetMetadata(file);

        return metadata.Comment;
    }

    private string GetPhotoDate(FileInfo file)
    {
        BitmapMetadata metadata = GetMetadata(file);

        return metadata.DateTaken;
    }

    private BitmapMetadata GetMetadata(FileInfo file)
    {
        BitmapFrame frame;
        BitmapMetadata metadata;
        JpegBitmapDecoder decoder;

        string filepath = file.FullName;

        using (Stream fileStreamIn = File.Open(filepath, FileMode.Open,  
               FileAccess.Read, FileShare.Read))
        {
            decoder = new JpegBitmapDecoder(fileStreamIn, 
                             BitmapCreateOptions.PreservePixelFormat,
                             BitmapCacheOption.OnLoad);
            //Here I get the exception
        }

        frame = decoder.Frames[0];

        return metadata = (BitmapMetadata)frame.Metadata;
    }

A:

I found a solution.
I've created a class, which contains the needed methods:
 public class PhotoFactory
{
    [...]

    public Photo CreatePhotoObject(FileInfo file)
    {
        string comment;
        string dateString;
        DateTime date;

        int year = 0;
        string filename = file.FullName;
        string path = selectedFolder + @"\" + filename;
        string photoname = file.Name;

        GetCommentDate(file.FullName, out comment, out dateString);

        if (dateString != null)
        {
            date = DateTime.Parse(dateString).Date;
            dateString = date.ToShortDateString();
            year = date.Year;
        }

        return new Photo(year, comment, filename, photoname, dateString);
    }

    private void GetCommentDate(string filepath, out string comment, out 
                                string dateString)
    {
        using (FileStream fileIn = new FileStream(filepath, FileMode.Open, 
               FileAccess.Read, FileShare.Read))
        {
            BitmapSource source = BitmapFrame.Create(fileIn);
            BitmapMetadata metadata = (BitmapMetadata)source.Metadata;

            comment = metadata.Comment;
            dateString = metadata.DateTaken;
        }
    }
}

That way I don't get an OutOfMemoryException, when I load 100 files.
If anyone has improvement propasals or other annotations, I would appreciate to hear them.

