0

In this code Base64FromBitmap() converts an image to a string, then, the result will appear in a RichEdit. Sometimes, this process takes a few seconds (based on the size of an image). I want to add a Progressbar that shows the progress until complete the process. There is no For loop in the Base64FromBitmap() function.

function Base64FromBitmap(Bitmap: TBitmap): string;
var
  Input: TBytesStream;
  Output: TStringStream;
  Encoding: TBase64Encoding;
begin
  Input := TBytesStream.Create;
  try
    Bitmap.SaveToStream(Input);
    Input.Position := 0;
    Output := TStringStream.Create('', TEncoding.ASCII);
    try
      Encoding := TBase64Encoding.Create(0);
      try
        Encoding.Encode(Input, Output);
        Result := Output.DataString;
      finally
        Encoding.Free;
      end;
    finally
      Output.Free;
    end;
  finally
    Input.Free;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  s: string;
begin
  s := Base64FromBitmap(Image1.Picture.Bitmap);
  RichEdit.Text := s;
end;
shdotcom
  • 157
  • 1
  • 11
  • You might want to notice that this question actually has nothing to do with `TRichEdit`, assuming `Base64FromBitmap` is the slow part. – Andreas Rejbrand Nov 06 '20 at 18:18
  • @Andreas Rejbrand Yes, the process is in this line: s := Base64FromBitmap(Image1.Picture.Bitmap); – shdotcom Nov 06 '20 at 18:21
  • 1
    You have to use another function to convert to base64, a function which has a loop where you may include the update for the progress bar. There are a lot of base64 function on the internet. Use one of them, reading the bitmap file block by block , encore it to base64 and update the progress bar. – fpiette Nov 06 '20 at 19:23
  • And don't forget to put the computation in a separate thread. And don't forget that you mustn't interact with VCL controls directly from that non-GUI thread. – Andreas Rejbrand Nov 06 '20 at 19:44
  • Base64 is fairly easy to implement by hand, so if you can't find an existing implementation that exposes status, I would suggest simply writing your own loop that encodes the image bytes manually and reports status to the UI along the way. – Remy Lebeau Nov 06 '20 at 19:49
  • I already have Base64FromBitmap() function. Please check the question. – shdotcom Nov 07 '20 at 03:35
  • The function you have (`Base64FromBitmap`) will not allow you to report progress, because there's nowhere in that function where it can be done. You're going to need to implement it differently so that there's a loop during the encoding process where you can report the progress, and your existing function does not contain such a loop. You're going to need to do something other than using `Encoding.Encode` to do the work. – Ken White Nov 07 '20 at 03:48
  • @Ken White I did mention in the question there is no loop in the Base64FromBitmap function. Base64FromBitmap is just an example, I have many other similar functions that also require a progress bar. Changing all these functions does not make sense. – shdotcom Nov 07 '20 at 05:27
  • Changing all those functions are going to be necessary if you want to show the progress. You can't have it both ways. You either want them to work as is, or you want to change them to allow you to report the progress. Make up your mind. :-) You can say all you want that you want the performance of a Ferrari, but you're not going to get it if you own a moped. Likewise, you're not going to be able to do what you ask if you're not willing to modify the code to make it possible. – Ken White Nov 07 '20 at 05:32
  • Depending on the way the Base64Encoding read the source stream, you can add progress to it by using a proxy stream, which reports the actual data being read. Then compare that to the size of the source stream and voila, you have something to you can use to show the progres. And please use a separate thread to do the conversion, so the ui wont lock up... – R. Hoek Nov 07 '20 at 09:38

0 Answers0