title: How do I dispose Bitmap / free memory after I got the requried data from BitmapMetadata?
body: <p>I’m working on a solution,  which needs to support the query of many jpeg-files metadata to get the <code>Comment</code>- and <code>DateTaken</code>-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 <code>Comment</code>- and <code>DateTaken</code>-values to the two strings. Currently I’m using a <code>JpegBitmapDecoder</code> and set the <code>BitmapCacheOption</code> by instantiation of the decoder to <code>OnLoad</code>. 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.</p>
  
  <p>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 <code>JpegBitmapDecoder</code>.</p>
  
  <p>Here are the methods:</p>
  
  <p>In <code>formPleaseWait</code>:</p>
  
  <pre><code>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);
              }
          }
      }
  </code></pre>
  
  <p>In <code>formMain</code>:</p>
  
  <pre><code>    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;
      }
  </code></pre>