title: Delphi 2010 - Decode Base64 encoded image from XML doc
body: <p>I am trying to decode a base64 encoded EMF image from an XML document in my application and render it on screen, however, it never seems to appear.</p>
  
  <p>If I copy/paste the data from the XML document into Notepad++ and use the <code>Base64 Decode</code> option and save the file as a <code>.emf</code> it opens fine in mspaint. So I think the issue is how I am decoding it.</p>
  
  <p>I have tried the following decode methods described in these articles:</p>
  
  <p><a href="http://www.delphi-zone.com/2010/03/how-to-encode-decode-base64-string/" rel="nofollow noreferrer">How to encode / decode Base 64 string</a><br/>
  <a href="http://www.swissdelphicenter.ch/torry/showcode.php?id=1223" rel="nofollow noreferrer">http://www.swissdelphicenter.ch/torry/showcode.php?id=1223</a></p>
  
  <p>I have also tried the <code>TIdDecoderMIME</code> class to no avail.</p>
  
  <p>Does anyone know the most reliable way of decoding a base64 encoded string from XML?</p>
  
  <p><b>Example</b></p>
  
  <pre><code>procedure TXmlSerializer.SaveImageFromString(const AValue: string);
  var
    StrStream: TStringStream;
    Decoder: TIdDecoderMIME;
  begin
    // AValue is base64 encoded string from XML doc
    Decoder := TIdDecoderMIME.Create(nil);
    try
      StrStream := TStringStream.Create(Decoder.DecodeString(AValue));
      try
        StrStream.SaveToFile('MyPath\Image.emf');
      finally
        StrStream.Free;
      end;
    finally
      Decoder.Free;
    end;
  end;
  </code></pre>
  
  <p>Why is it the above doesn't work but copying the raw data into Notepad++ and decoding &amp; saving as <code>.emf</code> works?</p>