-1

Im programming mobil app with WebSevices at Delphi Xe7 FireMonkey. I have a webmethod on webservice. That webmethod is post Base64 string to my database. Delphi Side is create this Base64 string from Bitmap. Im use this algorithm

Uses ....EncdDecd;
function Tfrm_yenikayit.BitmapToBase64(Bitmap: TBitmap): string;
var
  Input : TMemoryStream;
  Output: TStringStream;
begin
  Input := TMemoryStream.Create;
  try
    Bitmap.SaveToStream(Input);
    Input.Position := 0;
    Output := TStringStream.Create('');
    try
      EncdDecd.EncodeStream(Input, Output);
      Result := Output.DataString;
    finally
      Output.Free;
    end;
  finally
    Input.Free;
  end;
end;

But A Photo that size's 1920x1280 is giving approximately 3 million charecter to result. How to i making image to short string and fastest than this algorithm?

1 Answers1

0

I found solution that algortihym

Function BitmapToBase64(Bitmap:Tbitmap):string;
var
  BS: TBitmapSurface;
  AStream: TMemoryStream;
  AStringStream : TStringStream;
  AResult : AnsiString;
begin
  BS := TBitmapSurface.Create;
  BS.Assign(Bitmap);
  BS.SetSize(600,400);///Solution this
  AStream := TMemoryStream.Create;
  try
    TBitmapCodecManager.SaveToStream(AStream, BS, '.jpg');
    AStringStream := TStringStream.Create(EncodeBase64(AStream, AStream.Size));
    Result:=AStringStream.DataString;     
  finally
    AStream.Free;
    AStringStream.Free;
    BS.Free;
  end;
end;

I found solution BS.SetSize(600,400) that its not important me quality for me

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • I added initialization of Result. – LU RD Mar 27 '15 at 12:38
  • @LURD I removed it again. It's not needed. – David Heffernan Mar 27 '15 at 13:18
  • Use `System.NetEncoding` for base64. – David Heffernan Mar 27 '15 at 13:21
  • @DavidHeffernan, is this answer not valid anymore, http://stackoverflow.com/a/5336885/576719 ? – LU RD Mar 27 '15 at 13:56
  • @LURD That answer is as valid as ever. However the function in this answer assigns to Result unconditionally. – David Heffernan Mar 27 '15 at 14:17
  • @DavidHeffernan, yes sorry. I try to treat everything between try and finally as a condition, but here if an exception is raised, the result is irrelevant. – LU RD Mar 27 '15 at 14:32
  • 2
    The `TStringStream` is redundant and a waste of processing (converting a string to bytes back to a string. This code: `AStringStream := TStringStream.Create(EncodeBase64(AStream, AStream.Size)); Result:=AStringStream.DataString;` is effectively the same as this: `Result := EncodeBase64(AStream, AStream.Size);`. – Remy Lebeau Mar 27 '15 at 16:20