After dealing with Base64 images now I have a function that can convert PNG

After dealing with Base64 images now I have a function that can convert PNG,
last lines are:

Img.Picture.LoadFromStream(MemoryStream);
Bmp.Assign(Img.Picture.Graphic);

Where the MemoryStream var is the decoded data from string. Now make it work for sure I have to save it as file Bmp.SaveToFile(); But why I cannot simply return:

Result := Bmp ???

If I do that, I get no error, but the image won't show.

Function returning is a TBitmap type.

Comments

  1. Does calls that measure size / bit'edness / encoding / etc of the bmp - work ok?

    ReplyDelete
  2. Lars Fosdal No. My lack of understanding says that if I am loading from the memory stream, assign to a TBitmap object (Bmp) it is already a valid one, or should. Really don't know how to solve that, seems to be very easy but it is not

    ReplyDelete
  3. Reset the original stream's position to zero. It's caught me off guard so many times.

    ReplyDelete
  4. Achim Kalwa Here code, I don't want to need to save and load it:

    function ImgBase64ToBitmap(FileName, sBase64: AnsiString): TImage;
    var
    MS: TMemoryStream;
    Decode: TIdDecoderMIME;
    Img: TImage;
    begin
    Img := TImage.Create(nil);
    MS := TMemoryStream.Create;

    try
    Decode := TIdDecoderMIME.Create;
    try
    Decode.DecodeBegin(MS);
    Decode.Decode(sBase64);
    Decode.Decode(MS);
    Decode.DecodeEnd();

    MS.Position := 0;
    Img.Picture.LoadFromStream(MS);
    MS.Position := 0; // dahh
    Result := Img;
    // ** I DON'T want it...
    MS.SaveToFile(Concat(FileName, '.png'));


    finally
    Decode.Free;
    end;
    finally
    MS.Free;
    Img.Free;
    end;
    end;

    ReplyDelete
  5. Andrea Raimondi I tried, but maybe at the incorrect place...

    ReplyDelete
  6. I was able to do it by changing the function to procedure and doing that:
    ImgBase64ToBitmap(FileName, sBase64: AnsiString; var MemoryStream: TMemoryStream);
    So, now I get a new parameter (MemoryStream) and the main code is like:
    MS := TMemoryStream.Create;
    ImgBase64ToBitmap('imageBase64', sImg, MS);
    ImageCode64.Picture.LoadFromStream(MS);
    MS.Free;
    But I don't know if it is a good approach, at least it's working.

    ReplyDelete
  7. Magno Lima the reason why you function does not work is in the finally block: You free Img, but it is the function result. Better rename the function to CreateImageFromBase64 and do not free Img in the finally block.
    The calling code is responsible for destroying the image object.

    ReplyDelete
  8. Achim Kalwa So my newest version works because of that, the Img object I am passing as var parameter. Thank you for the answer.

    ReplyDelete

Post a Comment