Jump to content

Embedded image in TuniHtmlMemo


Roberto Nicchi

Recommended Posts

  • Roberto Nicchi changed the title to Embedded image in TuniHtmlMemo
5 hours ago, Sherzod said:

Hello, i have checked the post but i'm afraid it's not what i need. The first and most important problem is that the image is not embedded into the html. The image is uploaded into a directory of the HD. I need the image is embedded into the html because the html will be used as a body for an email. Email that is sent using Indy.

And, secondary, the image can't be resized after it's been displayed into the html memo

thanks

Link to comment
Share on other sites

1. Take the (uploaded) image and encode it into base64.

2. Create a data URI by replacing "[Base64EncodedString]" with the the base64 encoded image in the following html code:

<img src="data:image/jpeg;base64,[Base64EncodedString]" />

3. Insert this html code into htmlmemo.

Link to comment
Share on other sites

6 hours ago, Oliver Morsch said:

example:

 

<img src="data:image/png;base64,iVBORw0KGgoAAA
ANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4
//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU
5ErkJggg==" alt="Red dot" />

Hello,

 

i have almost done it.

I have an exception "Invalid or unexpected token" in the line UniSession.AddJS(

Look at my code below.

If i replace the encoding code with a constant encoded text (the one from your example) i see that the image (red dot) is correctly inserted into the htmlmemo

So i guess there's a mistake in how i'm encoding the image. My code is wrong ?

 

procedure TMainForm.UniFileUpload1Completed(Sender: TObject;
  AStream: TFileStream);
var
  htmltext:string;
  encodedimage: String;
  sstream:TStringStream;
begin
  sstream:=TStringStream.Create;
  try
    encodestream(astream,sstream);
    encodedimage:=sstream.DataString;
  finally
    sstream.free;
  end;

 // encodedimage:='iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==';

  htmltext:='<img src="data:image/jpeg;base64,'+encodedimage+'" />';

  UniSession.AddJS(UniHTMLMemo1.JSName + '.execCmd(''InsertHTML'', '''+htmltext+''')');
end;

 

Link to comment
Share on other sites

12 hours ago, Oliver Morsch said:

I think your base64 encoding ist wrong. Try tnetencoding.base64.encode.

 

I changed the encoding code in

 

  sstream:=TStringStream.Create;
  try
    tnetencoding.base64.encode(astream,sstream);
    encodedimage:=sstream.DataString;
  finally
    sstream.free;
  end;

 

but didn't help.

 

Seems i have found the solution anyway: have to remove line breaks in the encoded text.

encodedimage:=StringReplace(StringReplace(encodedimage, #10, ' ', [rfReplaceAll]), #13, ' ', [rfReplaceAll]);

I only miss the possibility to resize the image but i can live without it  ... for the moment.

To set in HTML the image width helps. I see that if i set the width, the height is calculated automatically and the image is not stretched.

  htmltext:='<img width="300" src="data:image/jpeg;base64,'+encodedimage+'" />';

 

thanks for your help

Roberto

Link to comment
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
×
×
  • Create New...