Difference between revisions of "Base64"
From Lazarus wiki
Jump to navigationJump to searchLine 81: | Line 81: | ||
=Convert Base64 string to picture, 2= | =Convert Base64 string to picture, 2= | ||
This procedure is from forum member '''KodeZwerg'''. | This procedure is from forum member '''KodeZwerg'''. | ||
− | |||
− | |||
− | |||
− | |||
He created a pretty easy to handle method that supports now all (?) formats that Lazarus Graphics offers. | He created a pretty easy to handle method that supports now all (?) formats that Lazarus Graphics offers. | ||
To name them: JPEG, PNG, GIF, BMP, ICO and TIFF. | To name them: JPEG, PNG, GIF, BMP, ICO and TIFF. | ||
Line 90: | Line 86: | ||
<syntaxhighlight lang="pascal"> | <syntaxhighlight lang="pascal"> | ||
− | unit | + | unit uBase64Image; |
− | {$mode | + | {$mode ObjFPC}{$H+} |
interface | interface | ||
uses | uses | ||
− | Classes , SysUtils | + | Classes, SysUtils, Graphics, ExtCtrls, Base64; |
− | |||
− | |||
− | + | function FileToBytes(const AFilename: string): TBytes; | |
− | + | function BytesToBase64(const AData: TBytes): AnsiString; | |
− | + | function Base64ToBytes(const ABase64String: AnsiString): TBytes; | |
− | + | function GetGraphicClassFromBytes(const AData: TBytes): TGraphicClass; | |
− | + | procedure AssignBase64ToImage(const ABase64String: AnsiString; const AImage: TImage); | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
implementation | implementation | ||
− | { | + | { convert any file into bytes } |
− | + | function FileToBytes(const AFilename: string): TBytes; | |
− | |||
− | function | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
var | var | ||
− | + | FileStream: TFileStream; | |
− | |||
− | |||
begin | begin | ||
− | + | FileStream := TFileStream.Create(AFileName, fmOpenRead or fmShareDenyWrite); | |
try | try | ||
− | + | SetLength(Result, FileStream.Size); | |
− | + | FileStream.ReadBuffer(Result[0], FileStream.Size); | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
finally | finally | ||
− | + | FileStream.Free; | |
end; | end; | ||
end; | end; | ||
− | + | { convert bytes to base64 } | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | function | + | function BytesToBase64(const AData: TBytes): AnsiString; |
var | var | ||
− | + | encodedStream: TStringStream; | |
encodingStream: TBase64EncodingStream; | encodingStream: TBase64EncodingStream; | ||
− | |||
begin | begin | ||
− | + | encodedStream := TStringStream.Create(''); | |
try | try | ||
− | + | encodingStream := TBase64EncodingStream.Create(encodedStream); | |
− | |||
− | |||
try | try | ||
− | encodingStream | + | encodingStream.WriteBuffer(AData[0], Length(AData)); |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
finally | finally | ||
− | + | encodingStream.Free; | |
end; | end; | ||
+ | Result := encodedStream.DataString; | ||
finally | finally | ||
− | + | encodedStream.Free; | |
end; | end; | ||
end; | end; | ||
− | + | { convert base64 to bytes } | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | function | + | function Base64ToBytes(const ABase64String: AnsiString): TBytes; |
var | var | ||
− | + | decodedStream: TBase64DecodingStream; | |
− | + | base64stream: TStringStream; | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
begin | begin | ||
− | + | base64stream := TStringStream.Create(ABase64String); | |
try | try | ||
− | + | decodedStream := TBase64DecodingStream.Create(base64stream); | |
− | |||
− | |||
try | try | ||
− | + | SetLength(Result, decodedStream.Size); | |
− | + | decodedStream.ReadBuffer(Result[0], decodedStream.Size); | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
finally | finally | ||
− | + | decodedStream.Free; | |
end; | end; | ||
finally | finally | ||
− | + | base64stream.Free; | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
end; | end; | ||
end; | end; | ||
Line 366: | Line 162: | ||
{ determine the image format } | { determine the image format } | ||
− | function | + | function GetGraphicClassFromBytes(const AData: TBytes): TGraphicClass; |
− | |||
− | |||
begin | begin | ||
− | Result := | + | Result := nil; |
− | + | // JPEG | |
− | + | if (((Length(AData) >= 8) and (CompareMem(@AData[0], @[$FF, $D8, $FF, $DB], 4) | |
− | + | or CompareMem(@AData[0], @[$FF, $D8, $FF, $E0, $00, $10, $4A, $46], 8) | |
− | + | or CompareMem(@AData[0], @[$49, $46, $00, $01], 4) | |
− | + | or CompareMem(@AData[0], @[$FF, $D8, $FF, $EE], 4) | |
− | + | or (CompareMem(@AData[0], @[$FF, $D8, $FF, $E1], 4) and CompareMem(@AData[6], @[$45, $78], 2)) | |
− | + | or CompareMem(@AData[0], @[$69, $66, $00, $00], 4) | |
− | + | or CompareMem(@AData[0], @[$FF, $D8, $FF, $E0], 4)))) then | |
− | + | Result := TJPEGImage | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | Result := | ||
else | else | ||
− | if CompareMem(@ | + | // PNG |
− | Result := | + | if ((Length(AData) >= 8) and CompareMem(@AData[0], @[$89, $50, $4E, $47, $0D, $0A, $1A, $0A], 8)) then |
+ | Result := TPortableNetworkGraphic | ||
else | else | ||
− | if CompareMem(@ | + | // GIF |
− | Result := | + | if ((Length(AData) >= 6) and (CompareMem(@AData[0], @[$47, $49, $46, $38, $37, $61], 6) |
+ | or CompareMem(@AData[0], @[$47, $49, $46, $38, $39, $61], 6))) then | ||
+ | Result := TGIFImage | ||
else | else | ||
− | if CompareMem(@ | + | // BMP |
− | Result := | + | if ((Length(AData) >= 2) and CompareMem(@AData[0], @[$42, $4D], 2)) then |
+ | Result := TBitmap | ||
else | else | ||
− | if CompareMem(@ | + | // ICON |
− | Result := | + | if ((Length(AData) >= 4) and CompareMem(@AData[0], @[$00, $00, $01, $00], 4)) then |
+ | Result := TIcon | ||
else | else | ||
− | if CompareMem(@ | + | // TIFF |
− | Result := | + | if ((Length(AData) >= 4) and (CompareMem(@AData[0], @[$49, $49, $2A, $00], 4) |
+ | or CompareMem(@AData[0], @[$4D, $4D, $00, $2A], 6))) then | ||
+ | Result := TTiffImage | ||
else | else | ||
− | + | raise Exception.Create('Unknown Graphic Type!'); | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
end; | end; | ||
− | { | + | { analyze and display a found image format into a TImage } |
− | + | procedure AssignBase64ToImage(const ABase64String: AnsiString; const AImage: TImage); | |
var | var | ||
− | stream: | + | bytes: TBytes; |
+ | graphic: TGraphic; | ||
+ | stream: TStream; | ||
begin | begin | ||
− | + | AImage.Picture.Clear; | |
− | + | bytes := Base64ToBytes(ABase64String); | |
− | + | graphic := GetGraphicClassFromBytes(bytes).Create; | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
try | try | ||
− | stream | + | stream := TBytesStream.Create(bytes); |
− | |||
− | |||
try | try | ||
− | + | graphic.LoadFromStream(stream); | |
finally | finally | ||
+ | stream.Free; | ||
end; | end; | ||
+ | AImage.Picture.Assign(graphic); | ||
finally | finally | ||
− | + | graphic.Free; | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
end; | end; | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
end; | end; | ||
Revision as of 16:43, 14 August 2023
Various code examples to handle Base64 encoded strings.
Convert Base64 string to picture, 1
Example from forum member wp.
procedure DecodeBase64ToStream(AStream: TStream; const s: string; strict:boolean=false);
var
SD : String;
Instream: TStringStream;
Decoder: TBase64DecodingStream;
begin
if Length(s)=0 then
Exit;
SD := S;
while Length(Sd) mod 4 > 0 do
SD := SD + '=';
Instream:=TStringStream.Create(SD);
try
if strict then
Decoder:=TBase64DecodingStream.Create(Instream,bdmStrict)
else
Decoder:=TBase64DecodingStream.Create(Instream,bdmMIME);
try
AStream.CopyFrom(Decoder, Decoder.Size);
AStream.Position := 0;
finally
Decoder.Free;
end;
finally
Instream.Free;
end;
end;
var
base64String: String =
'iVBORw0KGgoAAAANSUhEUgAAARIAAAAdCAYAAABmFuNCAAAFPElEQVR4Xu2cy5oiIQyFdaPP'+
'PCt1Nc9sbxyRTlcqJuFAYdvf9HGjllSAQ/grXGR/vV5vO76oABWgAhsU2BMkG9TjrVSACjwU'+
'IEjoCFSACmxWwAXJ4XDY7fe73fX6scpg1nVtNLLZWzPEzqzyM6/3+sasdkT8kHlVlVo+/wSS'+
'4/Hv7nY71XBlf7nD5M/j86zruvEimzW/Q8oSDbnMjhiZVX7m9V7fmNWOiB8yr7zvaw3diKQI'+
'WF4CEd0ZZ1y3jahtaoDcgmngEi3JS4ASlTnL6zvrxbyqU7Z0QNLM8k/mtb2Pi4bT50hKCHSP'+
'ZR72Pz6uaVRhfxSIWIDs7+Q4n8+706k+Db8K/wkUOwTrypSJqQAV2KzAVJBoiPSCpEAkikCK'+
'rQKTm5NAohPCZLMv0AAVGFZgGkgsROpkLRaRaIhE0Yet4eVyeUQpAhdvcnhYFd5IBahAlwJT'+
'QLIFIqW0Nhqx0YdEHTog8dIwKulqeyamAtMU2AySGRAptfGGNRUgF1PZU5L2eVlymlI0RAWo'+
'QKgADJIKjDKBWveWWICUaz3DGSmRXmLTpfQhIikimCzL1bbGx+Px65I35Cq/y3WdVm4qv9nr'+
'0dAty0vnU6OxJV/9vVVer0Uz217Zo3oi/SXSArmODnmjOuo2yTRE6qHTSNk9P4h8I0ub5Y+2'+
'ldaq1ye89JGNKK3NP2q7Jkg8YHgCjUCkOsGyb2UNkhqJlD0tZT+LvEsa2eti77FL1guw1qDw'+
'GsiK1Or0kaNYKLXykvTRfbYcWb7lN+3c3mcPsr2d2wOvwHaWjq1yat2ieqMw8bRvXfN8ywNb'+
'D/SzBwvaqTOfR+qEpLF1SkHyaohokJTJU728q+HhwWTZNFfXgMukq95Alzkh2mG9RtV2s4gk'+
'6sxyf+tJY5+OSIewQPI6u+5wUUfoycvaQJ5siH0UlrrjekBG8/L0jjqUbUMU9LosSETSCw7E'+
'5xFIjDxAGyBZhgNRgyzLr9gKzXNla0RSJk8FCOW9QKF+X0ck+rvYEgiNgiSCw4ig2rHRzzad'+
'dTiv83vtEUUJmf3Wb60OjXSqrFxoR880yQDaa18DBYXibJBIm2htRzSMyt9qsxG/D0HyHdFI'+
'FSwe2kQQEcBYJxkFSRT+jwjagscItFBHbT3lkAgK7XhoCO6BCq2PLQuS50gkF7WZV07kGlI/'+
'pC66XCO+2CprFMEhUctT34uOEUBBUgz27mBdP10ikJRUdRPaeo7kfL9+C1Zu+iZbrdN536Mh'+
'SBYlZE8COzmXQcw+OVqdvAUSFGKtfDI46LprjaLrSF5eNGJtbx0G6Cggm1dC6ofON3l+osth'+
'fSxKH2mIgiTKswdeUEQiE6mHw/NQZ3SSVSov/5vwJ08rTAo46qt+9peK61AommztdVimpwJU'+
'AFegCyTFrAeTLRFJpeE6KtGbzerUifxLb4GI3QGbDWtwOZiSClCBEQWgyVYddWzdgOYVUkcl'+
'6Bb5B17U/28IkpHm5z1UYI4CwD4SGc7o4cXnQKPj/zSt4mZDHAsNbUtWdzikaSnM36nA6xRo'+
'gkSGM/oMkDJHsXVuxI9MKrT8f/k+HyUgS8bo5NbrZKRlKvC7FYBA8kqJ7BFuembaA4qUxUKk'+
'dRRcBeL7j5CcVQbdJrNsUkMeIdnrS1/90Vv+7TXWm14yz49azDfDrZf7/OMhdWebdWwej1rk'+
'UYu9vvQb/PCtZ7YWgZEjEpGICLEz64g+5rX9iD5q+H9p+PYzWxFIMA0VoAI/W4G3z5H8bHlY'+
'OipABRAFCBJEJaahAlQgVYAgoYNQASqwWYF/o2XhCcA1FYIAAAAASUVORK5CYII=';
procedure TForm1.Button1Click(Sender: TObject);
var
stream: TMemoryStream;
begin
stream := TMemoryStream.Create;
try
DecodeBase64ToStream(stream, base64String);
Image1.Picture.LoadFromStream(stream);
finally
stream.Free;
end;
end;
Convert Base64 string to picture, 2
This procedure is from forum member KodeZwerg. He created a pretty easy to handle method that supports now all (?) formats that Lazarus Graphics offers. To name them: JPEG, PNG, GIF, BMP, ICO and TIFF. Full unit:
unit uBase64Image;
{$mode ObjFPC}{$H+}
interface
uses
Classes, SysUtils, Graphics, ExtCtrls, Base64;
function FileToBytes(const AFilename: string): TBytes;
function BytesToBase64(const AData: TBytes): AnsiString;
function Base64ToBytes(const ABase64String: AnsiString): TBytes;
function GetGraphicClassFromBytes(const AData: TBytes): TGraphicClass;
procedure AssignBase64ToImage(const ABase64String: AnsiString; const AImage: TImage);
implementation
{ convert any file into bytes }
function FileToBytes(const AFilename: string): TBytes;
var
FileStream: TFileStream;
begin
FileStream := TFileStream.Create(AFileName, fmOpenRead or fmShareDenyWrite);
try
SetLength(Result, FileStream.Size);
FileStream.ReadBuffer(Result[0], FileStream.Size);
finally
FileStream.Free;
end;
end;
{ convert bytes to base64 }
function BytesToBase64(const AData: TBytes): AnsiString;
var
encodedStream: TStringStream;
encodingStream: TBase64EncodingStream;
begin
encodedStream := TStringStream.Create('');
try
encodingStream := TBase64EncodingStream.Create(encodedStream);
try
encodingStream.WriteBuffer(AData[0], Length(AData));
finally
encodingStream.Free;
end;
Result := encodedStream.DataString;
finally
encodedStream.Free;
end;
end;
{ convert base64 to bytes }
function Base64ToBytes(const ABase64String: AnsiString): TBytes;
var
decodedStream: TBase64DecodingStream;
base64stream: TStringStream;
begin
base64stream := TStringStream.Create(ABase64String);
try
decodedStream := TBase64DecodingStream.Create(base64stream);
try
SetLength(Result, decodedStream.Size);
decodedStream.ReadBuffer(Result[0], decodedStream.Size);
finally
decodedStream.Free;
end;
finally
base64stream.Free;
end;
end;
{ determine the image format }
function GetGraphicClassFromBytes(const AData: TBytes): TGraphicClass;
begin
Result := nil;
// JPEG
if (((Length(AData) >= 8) and (CompareMem(@AData[0], @[$FF, $D8, $FF, $DB], 4)
or CompareMem(@AData[0], @[$FF, $D8, $FF, $E0, $00, $10, $4A, $46], 8)
or CompareMem(@AData[0], @[$49, $46, $00, $01], 4)
or CompareMem(@AData[0], @[$FF, $D8, $FF, $EE], 4)
or (CompareMem(@AData[0], @[$FF, $D8, $FF, $E1], 4) and CompareMem(@AData[6], @[$45, $78], 2))
or CompareMem(@AData[0], @[$69, $66, $00, $00], 4)
or CompareMem(@AData[0], @[$FF, $D8, $FF, $E0], 4)))) then
Result := TJPEGImage
else
// PNG
if ((Length(AData) >= 8) and CompareMem(@AData[0], @[$89, $50, $4E, $47, $0D, $0A, $1A, $0A], 8)) then
Result := TPortableNetworkGraphic
else
// GIF
if ((Length(AData) >= 6) and (CompareMem(@AData[0], @[$47, $49, $46, $38, $37, $61], 6)
or CompareMem(@AData[0], @[$47, $49, $46, $38, $39, $61], 6))) then
Result := TGIFImage
else
// BMP
if ((Length(AData) >= 2) and CompareMem(@AData[0], @[$42, $4D], 2)) then
Result := TBitmap
else
// ICON
if ((Length(AData) >= 4) and CompareMem(@AData[0], @[$00, $00, $01, $00], 4)) then
Result := TIcon
else
// TIFF
if ((Length(AData) >= 4) and (CompareMem(@AData[0], @[$49, $49, $2A, $00], 4)
or CompareMem(@AData[0], @[$4D, $4D, $00, $2A], 6))) then
Result := TTiffImage
else
raise Exception.Create('Unknown Graphic Type!');
end;
{ analyze and display a found image format into a TImage }
procedure AssignBase64ToImage(const ABase64String: AnsiString; const AImage: TImage);
var
bytes: TBytes;
graphic: TGraphic;
stream: TStream;
begin
AImage.Picture.Clear;
bytes := Base64ToBytes(ABase64String);
graphic := GetGraphicClassFromBytes(bytes).Create;
try
stream := TBytesStream.Create(bytes);
try
graphic.LoadFromStream(stream);
finally
stream.Free;
end;
AImage.Picture.Assign(graphic);
finally
graphic.Free;
end;
end;
end.
Convert Base64 string to picture, using BGRABitmap
This unit is from forum member KodeZwerg. You must install the BGRABitmap component.
unit ubase64image;
{$mode ObjFPC}{$H+}
interface
uses
Classes, SysUtils, Graphics, ExtCtrls,
Base64,
BGRABitmap, BGRABitmapTypes, BGRAGraphics, BGRASVG, BGRAOpenRaster, BGRAPhoxo, BGRAPaintNet;
function FileToStream(const AFilename: string): TStream;
function StreamToBase64(const AStream: TStream): AnsiString;
function Base64ToStream(const ABase64String: AnsiString): TStream;
procedure AssignBase64ToImage(const ABase64String: AnsiString; const AImage: TImage; const AFastMode: Boolean = False);
implementation
function FileToStream(const AFilename: string): TStream;
var
FileStream: TFileStream;
begin
FileStream := TFileStream.Create(AFileName, fmOpenRead or fmShareDenyWrite);
try
Result := TMemoryStream.Create;
try
Result.CopyFrom(FileStream, FileStream.Size);
finally
Result.Position := 0;
end;
finally
FileStream.Free;
end;
end;
function StreamToBase64(const AStream: TStream): string;
var
EncodedStream: TStringStream;
Encoder: TBase64EncodingStream;
begin
EncodedStream := TStringStream.Create('');
Encoder := TBase64EncodingStream.Create(EncodedStream);
try
AStream.Position := 0;
Encoder.CopyFrom(AStream, AStream.Size);
Encoder.Flush;
Result := EncodedStream.DataString;
AStream.Position := 0;
finally
Encoder.Free;
EncodedStream.Free;
end;
end;
function Base64ToStream(const ABase64String: AnsiString): TStream;
var
DecodedStream: TBase64DecodingStream;
Base64Stream: TStringStream;
begin
Base64Stream := TStringStream.Create(ABase64String);
try
DecodedStream := TBase64DecodingStream.Create(base64stream);
try
Result := TMemoryStream.Create;
try
Result.CopyFrom(DecodedStream, DecodedStream.Size);
finally
Result.Position := 0;
end;
finally
DecodedStream.Free;
end;
finally
Base64Stream.Free;
end;
end;
function BGRABitmapToPNGStream(const ABGRABitmap: TBGRABitmap): TStream;
begin
Result := TMemoryStream.Create;
try
ABGRABitmap.SaveToStreamAsPng(Result);
finally
Result.Position := 0;
end;
end;
function BGRABitmapToBitmap(const ABGRABitmap: TBGRABitmap): TBitmap;
begin
Result := TBitmap.Create;
try
Result.PixelFormat := ABGRABitmap.Bitmap.PixelFormat;
Result.Transparent := ABGRABitmap.HasTransparentPixels or ABGRABitmap.HasSemiTransparentPixels;
Result.SetSize(ABGRABitmap.Width, ABGRABitmap.Height);
Result.Canvas.Lock;
try
Result.Canvas.Draw(0, 0, ABGRABitmap.Bitmap);
finally
Result.Canvas.Unlock;
end;
finally
end;
end;
procedure AssignBase64ToImage(const ABase64String: AnsiString; const AImage: TImage;
const AFastMode: Boolean = False);
var
BGRABitmap: TBGRABitmap;
Stream: TStream;
bmp: TBitmap;
begin
AImage.Picture.Clear;
Stream := Base64ToStream(ABase64String);
if (DetectFileFormat(Stream) <> ifUnknown) then
begin
BGRABitmap := TBGRABitmap.Create;
try
try
BGRABitmap.LoadFromStream(Stream);
finally
Stream.Free;
end;
if AFastMode then
begin
bmp := BGRABitmapToBitmap(BGRABitmap);
try
AImage.Transparent := bmp.Transparent;
AImage.Picture.Assign(bmp);
finally
bmp.Free;
end;
end
else
begin
Stream := BGRABitmapToPNGStream(BGRABitmap);
try
AImage.Transparent := BGRABitmap.HasTransparentPixels or BGRABitmap.HasSemiTransparentPixels;
AImage.Picture.PNG.LoadFromStream(Stream);
finally
Stream.Free;
end;
end;
finally
BGRABitmap.Free;
end;
end
else
Stream.Free;
end;
initialization
BGRASVG.RegisterSvgFormat;
BGRAOpenRaster.RegisterOpenRasterFormat;
BGRAPhoxo.RegisterPhoxoFormat;
BGRAPaintNet.RegisterPaintNetFormat;
end.