Convert Image String to Bitmap Delphi

Asked

Viewed 5,666 times

5

I have an application that sends photos, these photos are stored in the database (SQL Server) in a varchar(max) as a text. So far so good.

Now I need to convert all this text into Bitmap and show this image in an Image of Delphi (Delphi version 7)

Part of the "Text" image is something like this;

/9j/4AQSkZJRgABAQAAQABAAD/2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEB AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/2wBDAQEBAQEBAQEBAQEBAQEBAQEBAQEB AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/wAARCAGYATIDASIA AhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQA Aaf9aqidaaqrbrihmuege1fhbyjxfdkbkaeii0kxwrvs0fakm2jyggkkfhcygroljicokso0nty3 Odk6q0rfrkdisuptvfvwv1hzwmnkzwznaglqc3r1dnd4exqdhiwgh4ijipktljwwl5izmqkjpkwm p6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vp09fb3+Pn6/8QAHwEA AwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSEx Bhjbuqdhcrmimoeifekrobhbcsmzuvavynlrchyknoel8rcygromjygpkju2nzg5okneruzhselk U1rvvldywvpjzgvmz2hpann0dxz3ehl6gooehyahiimkkpoulzaxmjmaoqokpaanqkmqsro0tba3 uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwD8gdIu bW4RWtrmCZSQwaKRHDMS+Avzbjnhhzmmdgybxdwuiyywstthi4iyx+8Dkk7W2+5HICEn5G0XUHs8 Pbko25rmpaac7hgx+Ydk2nbgcqqsxofunj8y6jgp+Pliowsg2kzby5zuigmdr7at/E3zsy2i9ltp X031atfnbrrtvpdte80e/wCfn3fl+Wt3d/Sdhgsvbi28qpbviz4i2ngmkrngdaxjiyhrrol8wwbw pbaS3DDJVeRjPHQnLkgivn7SvHtwgzMYWXPzD5VYksVIUEsdpPzL1A4O4AZr0HSfiDbA5khAAVs SAlWy2GbLEbgcZJ+UMWw2eDm4tN3etur7N2lo7P0V3a2qdkNNPZr+rrZu/2X/m938W/8FONE+0/B vwdrSrk6X4ztoPNwdyxX+m6nGwZyTzvgh+U8hdutnya/Nj9kv4nefpjj8qtd8gekbq/tseC9e1XR X0+VI5H1jT/srwCYFXMkIjMzvGuGfagDYBNfqj+3ZqreOfgXN4Z0Dw3retarPq2l6jA2nWbXUViN Pugad7jyizos0lyilervpmfp5gar81/2LNf074ZftFaBq/jm8HhjRodK8R2OqXOrLLYxwreaPeQ

1 answer

5

What is saved is apparently in Base64.

If it is Base64 then you can use these functions (credits David Heffernan in this question in the Soen )

uses
  System.SysUtils,
  System.Classes,
  Vcl.Graphics,
  Soap.EncdDecd;

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;

var
  Bitmap: TBitmap;
  s: string;

begin
  Bitmap := TBitmap.Create;
  Bitmap.SetSize(100,100);
  Bitmap.Canvas.Brush.Color := clRed;
  Bitmap.Canvas.FillRect(Rect(20, 20, 80, 80));
  s := Base64FromBitmap(Bitmap);
  Bitmap.Free;
  Bitmap := BitmapFromBase64(s);
  Bitmap.SaveToFile('C:\desktop\temp.bmp');
end.
  • This, the form the image is in is Base64. Using these functions, an error occurs in Delphi 7 in the variable declaration: Output: Tbytesstream, don’t know what use I use for this situation? : Tencoding.ASCII Thank you

  • Does anyone have any more tips to solve this?

  • TByteStream comes from System.Classes or Classes depending on the version of Delphi http://docwiki.embarcadero.com/Libraries/XE6/en/System.Classes.TBytesStream already the TEnconding comes from sysutils http://docs.embarcadero.com/products/rad_studio/delphiAndcpp2009/HelpUpdate2/EN/html/delphivclwin32/SysUtils_TEncoding.html

  • @user11890 Using the you use cited worked?

Browser other questions tagged

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