Error(Bitmap size Too big) when using Timage in thread firemonkey android

Asked

Viewed 217 times

1

Use this function to generate images in the system:

procedure converte_jpg(Bitmap: TBitmap; Stream: TMemoryStream);
var
  surf: TBitmapSurface;
  saveParams : TBitmapCodecSaveParams;
begin
  surf := TBitmapSurface.Create;

  try
    surf.Assign(Bitmap);
    saveParams.Quality:= 20;
    if not TBitmapCodecManager.SaveToStream(Stream, surf, '.jpg',@saveParams) 
    then
      raise EBitmapSavingFailed.Create(SBitmapSavingFailed);
  finally
    Surf.Free;
  end;
end;

I call her that

img.Bitmap.LoadFromStream(imagem);
converte_jpg(img.Bitmap,thumb_img);

the result of the function is a Tmemorystream that I saved in the database, the function works normally in the main thread, but when I put it inside an anonymous thread, the following error

Bitmap size Too big.

1 answer

1

Try to line up the thread then:

TThread.Queue(nil,
  procedure
  begin
    img.Bitmap.LoadFromStream(imagem);
    converte_jpg(img.Bitmap,thumb_img);
  end);

A simple alternative that solves Blobs' memory overflow problem would be to convert this image to a Base64 string instead of generating a Tmemorystream and saving this string in the database, then when reading the image, just convert from Base64 to bitmap, when recording, just convert bitmap to Base64.

David Hefferman’s code, available at: https://stackoverflow.com/questions/21909096/convert-base64-to-bitmap

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

function BitmapFromBase64(const base64: string): TBitmap;
var
  Input: TStringStream;
  Output: TBytesStream;
begin
  Input := TStringStream.Create(base64, TEncoding.ASCII);
  try
    Output := TBytesStream.Create;
    try
      Soap.EncdDecd.DecodeStream(Input, Output);
      Output.Position := 0;
      Result := TBitmap.Create;
      try
        Result.LoadFromStream(Output);
      except
        Result.Free;
        raise;
      end;
    finally
      Output.Free;
    end;
  finally
    Input.Free;
  end;
end;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.