Convert String Image to Bitmap Delphi

3

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 this entire text to Bitmap and show this image in a Delphi Image (Delphi Version 7)

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

     AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQE / 2wBDAQEBAQEBAQEBAQEBAQEBAQEBAQEB   THE   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   pbaS3DDJVeRjPHQnLkgivn7SvHtwgzMYWXPzD5VYksVIUEsdpPzL1A4O4AZr0HSfiDbA5khAAAVs   SAlWy2GbLEbgcZJ + UMWw2eDm4tN3etur7N2lo7P0V3a2qdkNNPZr + rrZu / 2X / m938W / 8FONE + 0 / B   vwdrSrk6X4ztoPNwdyxX + m6nGwZyTzvgh + U8hduTnYa / Nj9kv4NeFPjj8Qtd8GeKbq / tseC9e1XR   X0 + VI5H1jT / srwCYFXMkIjMzvGuGfagDYBNfqj + 3ZqreOfgXN4Z0Dw3retarPq2l6jA2nWbXUViN   PuGad7jyizos0LyiLERVpMfP5gAr81 / 2LNf074ZftFaBq / jm8HhjRodK8R2OqXOrLLYxwreaPeQQ

    
asked by anonymous 10.07.2014 / 21:21

1 answer

3

What is saved is, apparently on base64.

If it's even base64 then you can use these functions (credits David Heffernan in this question in 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.
    
10.07.2014 / 21:45