Recent

Author Topic: Read a jpeg/png and save it into a string  (Read 7088 times)

Shebuka

  • Sr. Member
  • ****
  • Posts: 429
Read a jpeg/png and save it into a string
« on: December 06, 2011, 06:47:57 pm »
Hi all,
i need to load a jpeg/png image into a string to be then passed to another function that encode it in base64 and send via the tcp, but i can't figure out of how to made it, this my current code:

Code: [Select]
  MyImage: String;
  MyImageStream: TFileStream;
  TheSize: Integer;
begin
  try
      if FileExists(TrimFilename(FAppPath + ROOT_SKIN + 'image.jpg')) then
      begin
        MyImageStream := TFileStream.Create(FAppPath + ROOT_SKIN + 'image.jpg', fmOpenRead);
        SetLength(MyImage, MyImageStream.Size);
        //TheSize := MyImageStream.Size;
        MyImageStream.Read(MyImage, MyImageStream.Size);
      end;

what's wrong? after the .Read whatever code is crashing...

seba22

  • Full Member
  • ***
  • Posts: 136
Re: Read a jpeg/png and save it into a string
« Reply #1 on: December 07, 2011, 12:55:00 pm »
Code: [Select]
strum: TFileStream;
tmp:string;
BytesRead:integer;

strum := TFileStream.Create('file.jpg', fmOpenRead);
SetLength(tmp, strum.size);
BytesRead := strum.Read(tmp[1], strum.size);
SetLength(tmp, BytesRead);
strum.Free;           

showmessage(tmp);

Try this one.

file.jpg

is Your file.
Inside (tmp) you have string with picture, or whatever.

To encode that string to base64
Code: [Select]
showmessage(IDEncoderMIME1.Encode (tmp));
You have to put have indy encoder

TIdEncoderMIME

Shebuka

  • Sr. Member
  • ****
  • Posts: 429
Re: Read a jpeg/png and save it into a string
« Reply #2 on: December 07, 2011, 06:35:55 pm »
Thankyou seba22, putting TheSize := MyFileStream.Read(MyImage[1], MyFileStream.Size); solved all crashes and it now works like a charm ))))

for encoding i'll use the EncodeStringBase64 function from base64 unit, all base64 encodings (like mentioned by you from TIdEncoderMIME) must produce same result, or not?

seba22

  • Full Member
  • ***
  • Posts: 136
Re: Read a jpeg/png and save it into a string
« Reply #3 on: December 07, 2011, 09:28:43 pm »
You are welcome

In my opinion every base64 encode / decode should return same result, so feel free to use what you want.

regards

 

TinyPortal © 2005-2018