encode, decode base64 image stream delphi

4.9k views Asked by At

I'm trying to encode a stream of image using EncodeBase64 in encddecd.pass that save to the database in a blob field. And retrieve this using decodebase64 in a bytestream that put into a component for showing. The process good done but image don't show in component.

Here's digest code:

procedure TProcessSave.Execute;
var
  i : Integer;
  fileStm : TStream;
  memStm : TMemoryStream;
  encode_plc : string;
begin
  for i := 0 to StrList.Count - 1 do
  begin
    DM.idb_tbl.Append;

    DM.idb_tbl.FieldByName('name').AsString := ExtractFileName(StrList.Strings[i]);  

try
  fileStm := TFileStream.Create(StrList.Strings[i], fmOpenReadWrite);

  memStm := TMemoryStream.Create;

  filestm.Seek(0, soFromBeginning);

  memStm.LoadFromStream(fileStm);

  DM.idb_tbl.FieldByName('file').Value := EncodeBase64(memStm.Memory, memStm.Size);

finally
  fileStm.Free;
  memStm.Free;
end;

DM.idb_tbl.Post;

end;
end;

and for loading:

procedure TLoadBar.Execute;
var
  imgStm : TStream;
begin
  with DM.idb_qry do
  begin
    DatabaseName := DM.idb_db.DatabaseName;

    SQL.Text := 'SELECT * FROM pics_tbl';
    Open;

   First;

   while not Eof do
   begin

     try

      imgStm :=TBytesStream.Create(DecodeBase64(FieldByName('file').Value));

      idx := imgBar_iemv.AppendImage;
      imgBar_iemv.SetImageFromStream(idx, imgStm);
      imgBar_iemv.ImageID[idx] := id;

    finally
      imgStm.Free;
    end;

  Next;
end;
end;
end;

and then I use imgStm for showing picture in a component but images not showing. I'm sure from component. What are your thoughts for encode and decode in my methods? Is another way that is sure for encode and decode for this problem?

0

There are 0 answers