<div dir="ltr">First off, EncodeStream() is a class method so there&#39;s no need to create an instance to call it.<br><br>Second, by default a TStringList with include a BOM on any encoded output file.  I suspect this is the source of your illegal characters.  A simple diff of two files output via each mechanism should confirm that (the only difference will be in the first 3 bytes, i.e. the BOM in the UTF8 encoded file).<br><br>In which case to prevent that simply set WriteBOM := FALSE on the string list before calling SaveToFile():<br><br><span style="font-size:12.8px">         aSourceStream.Position := 0;</span><div><span style="font-size:12.8px">         result := TIdEncoderMIME.EncodeStream(</span><wbr style="font-size:12.8px"><span style="font-size:12.8px">aSourceStream);</span></div><div><br style="font-size:12.8px"><span style="font-size:12.8px">         sl := TStringList.Create;<br></span>         sl.WriteBOM := FALSE;<br style="font-size:12.8px"><div><span style="font-size:12.8px">         try</span><br style="font-size:12.8px"><span style="font-size:12.8px">             sl.Text := result;<br></span><span style="font-size:12.8px">             sl.SaveToFile(&#39;d:\d\a.txt&#39;, TEncoding.UTF8);</span><br style="font-size:12.8px"><span style="font-size:12.8px">         finally</span><br style="font-size:12.8px"><span style="font-size:12.8px">             sl.Free;</span><br style="font-size:12.8px"><span style="font-size:12.8px">         end;</span><br></div></div></div><div class="gmail_extra"><br><div class="gmail_quote">On 9 August 2016 at 17:01, Robert Martin <span dir="ltr">&lt;<a href="mailto:rob@chreos.co.nz" target="_blank">rob@chreos.co.nz</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi guys<br>
<br>
<br>
I have been struggling to get some basic Mime encoding working, I have<br>
the following code which I use to Mime64 Encode a picture contained in a<br>
TImage component....<br>
<br>
             Base64 := TMime64.create;<br>
             try<br>
                     MemoryStream := TMemoryStream.Create;<br>
                     MemoryStream.Position := 0;<br>
Image.Picture.Graphic.<wbr>SaveToStream(MemoryStream);<br>
<br>
                     ReportImage.ImageMime   :=<br>
Base64.Encode_New(<wbr>MemoryStream);<br>
             .....<br>
<br>
Function shown below...<br>
<br>
<br>
<br>
function TMime64.Encode_New(<wbr>aSourceStream: TMemoryStream): String;<br>
var<br>
     IdEncoderMIME       : TIdEncoderMIME;<br>
     Sl                  : TStringList;<br>
begin<br>
     Result := &#39;&#39;;<br>
     try<br>
<br>
         IdEncoderMIME := TIdEncoderMIME.Create(nil);<br>
         sl := TStringList.Create;<br>
         try<br>
             aSourceStream.Position := 0;<br>
             Result := IdEncoderMIME.EncodeStream(<wbr>aSourceStream);<br>
<br>
             sl.Text := Result;<br>
             sl.SaveToFile(&#39;d:\d\a.txt&#39;, TEncoding.UTF8);<br>
         finally<br>
             IdEncoderMIME.Free;<br>
             sl.Free;<br>
         end;<br>
     except<br>
         on E : Exception do begin<br>
             raise EMimeError.Create(E.Message);<br>
         end;<br>
     end;<br>
end;<br>
<br>
The issue is that when I try to save the results in a UTF8 formatted<br>
file (the destination is to be a UTF-8 formatted XML file), there are<br>
&#39;bad&#39; characters in the file which are invisible in Notepad++ but are<br>
present.<br>
<br>
If I save without specifying the file encoding (<br>
sl.SaveToFile(&#39;d:\d\a.txt&#39;) instead of sl.SaveToFile(&#39;d:\d\a.txt&#39;,<br>
TEncoding.UTF8)   ) I have what appears to be a clean ASCII file. My<br>
understanding is that ASCII characters have the same byte value (0-127)<br>
in an ASCII formatted file or a UTF-8 formatted file so I don&#39;t<br>
understand why the values would change.<br>
<br>
Any suggestions.<br>
<br>
<br>
p.s. I have to be able to save the file as UTF-8 because that is what<br>
the destination XML is encoded in.  Currently it is &#39;corrupt&#39; because of<br>
the &#39;bad&#39; characters.<br>
<br>
p.p.s TIdEncoderMIME.EncodeStream returns a String.  I am using Delphi Xe2.<br>
<br>
p.p.p.s I know it is something stupid I am doing !<br>
<br>
<br>
<br>
<br>
Thanks<br>
Rob<br>
<br>
<br>
______________________________<wbr>_________________<br>
NZ Borland Developers Group - Delphi mailing list<br>
Post: <a href="mailto:delphi@listserver.123.net.nz">delphi@listserver.123.net.nz</a><br>
Admin: <a href="http://delphi.org.nz/mailman/listinfo/delphi" rel="noreferrer" target="_blank">http://delphi.org.nz/mailman/<wbr>listinfo/delphi</a><br>
Unsubscribe: send an email to <a href="mailto:delphi-request@listserver.123.net.nz">delphi-request@listserver.123.<wbr>net.nz</a> with Subject: unsubscribe<br>
<br>
<br>
</blockquote></div><br></div>