data:
不是您可以使用TIdHTTP
(或任何其他HTTP库)请求的URL类型,也不需要,因为所有数据都直接在URL本身中编码。因此,只需提取base64部分并使用您选择的任何base64解码器对其进行解码。
由于您的代码已经在使用Indy,因此您可以在TIdDecoderMIME
单元中使用其IdCoderMIME
类将base64数据解码为二进制流,例如使用TIdDecoderMIME.DecodeStream()
类程序。然后,您可以将该流加载到适当的TGraphic
后代(TGIFImage
,TBitmap
等)中,然后最终可以将该图形加载到TImage
中。
例如:
uses
IdGlobal,IdGlobalProtocols,IdCoderMIME,IdHTTP,IdSSLOpenSSL,Graphics,GIFImg,JPEG,ClipBrd;
function GetStrFromClipbrd: string;
const
CTextFormat = {$IFDEF UNICODE}CF_UNICODETEXT{$ELSE}CF_TEXT{$ENDIF};
begin
if Clipboard.HasFormat(CTextFormat) then
Result := Clipboard.AsText
else
Result := '';
end;
procedure TForm1.Button1Click(Sender: TObject);
var
Graphic: TGraphic;
MS: TMemoryStream;
IdHTTP1: TIdHTTP;
URL,ContentType: string;
begin
URL := GetStrFromClipbrd;
if URL = '' then
begin
ShowMessage('There is no text in the Clipboard!');
Exit;
end;
Graphic := nil;
try
MS := TMemoryStream.Create;
try
if TextStartsWith(URL,'data:') then
begin
Fetch(URL,':');
ContentType := Fetch(URL,',');
if not TextEndsWith(ContentType,';base64') then
begin
ShowMessage('Data is not encoded in base64!');
Exit;
end;
SetLength(ContentType,Length(ContentType)-7);
TIdDecoderMIME.DecodeStream(URL,MS);
if ContentType = '' then
ContentType := 'text/plain;charset=US-ASCII';
end else
begin
IdHTTP1 := TIdHTTP.Create;
try
IdHTTP1.IOHandler := TIdSSLIOHandlerSocketOpenSSL.Create(IdHTTP1);
IdHTTP1.HandleRedirects := True;
IdHTTP1.Get(URL,MS);
ContentType := IdHTTP1.Response.ContentType;
finally
IdHTTP1.Free;
end;
end;
MS.Position := 0;
case PosInStrArray(ExtractHeaderItem(ContentType),['image/gif','image/jpeg','image/bmp'{,...}],False) of
0: Graphic := TGIFImage.Create;
1: Graphic := TJPEGImage.Create;
2: Graphic := TBitmap.Create;
// ...
else
ShowMessage('Unsupported image type!');
Exit;
end;
{ the 'data:' URL you provided is malformed,is says the image type
is 'image/bmp' even though it is actually a GIF and thus should
say 'image/gif'. To avoid problems with the above code determining
the wrong TGraphic class to use in that case,you can instead look
at the first few bytes of the decoded data to determinate its actual
image type,eg...
const
Signature_GIF87a: array[0..5] of Byte = ($47,$49,$46,$38,$37,$61);
Signature_GIF89a: array[0..5] of Byte = ($47,$39,$61);
Signature_JPEG: array[0..2] of Byte = ($FF,$D8,$FF);
Signature_BMP: array[0..1] of Byte = ($42,$4D);
...
if (MS.Size >= 6) and
(CompareMem(MS.Memory,@Signature_GIF87a,6) or
CompareMem(MS.Memory,@Signature_GIF89a,6)) then
begin
Graphic := TGIFImage.Create;
end
else if (MS.Size >= 3) and
CompareMem(MS.Memory,@Signature_JPEG,3) then
begin
Graphic := TJPEGImage.Create;
end
else if (MS.Size >= 2) and
CompareMem(MS.Memory,@Signature_BMP,2) then
begin
Graphic := TBitmap.Create;
end
...
else
ShowMessage('Unsupported image type!');
Exit;
end;
}
Graphic.LoadFromStream(MS);
finally
MS.Free;
end;
Image1.Picture.Assign(Graphic);
finally
Graphic.Free;
end;
if Image.Picture.Graphic is TGIFImage then
begin
TGIFImage(Image.Picture.Graphic).Animate := True;
//TGIFImage(Image.Picture.Graphic).AnimationSpeed := 500;
end;
end;
本文链接:https://www.f2er.com/3156524.html