gpt4 book ai didi

Delphi将base64字符串转换为PNG图像

转载 作者:行者123 更新时间:2023-12-05 00:44:23 25 4
gpt4 key购买 nike

我收到一个 json,其中包含一个带有验证码图像的 base64 字符串,以便用户可以查看和输入验证码内容。我无法在 TImage 中显示此图像的问题。出现以下错误:

class EPNGInvalidFileHeader the file being read is not a valid Portable Network Graphics" image because it contains an invalid header.

字符串 base64

/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAgGBgcGBQg
HBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICI
sIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQw
LDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyM
jIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAARCAAyALQDASIAAh
EBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAw
QFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDA
AQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2Jyggk
KFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZ
WmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmq
KjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2u
Hi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQE
BAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDB
AcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRo
bHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5Ok
NERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOE
hYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6ws
PExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6
/9oADAMBAAIRAxEAPwD3O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n7y/maK15o9yLM9jPSoWoor42J9GxYOr/
hUp6UUUS3BbET1NRRQwQHpTV6miikMdRRRSAieoHoorW
JDIGqBqKK2iZMgeoHooreJlIgeoHooroiZMgeoHooreJkyBqg
eiitomTIT1ooorUg//2Q==

转换函数:

var
s : TMemoryStream;
png : TPngImage;
bb : TBytes;
begin
s := TMemoryStream.Create;
bb := decodebase64(memo1.Text);
if Length(bb)>0 then
begin
s.WriteData(bb,Length(bb));
s.position:=0;
png:=TPngImage.Create;
png.LoadFromStream(s); // The error occurs here
//objImg.Picture.Assign(png);
png.Destroy;
end;
s.free;
end;

根据字符串提供者,图像是png,我错过了哪里?谢谢!

最佳答案

它解码为如下所示的 jpg 图像。所以正如评论中提到的,它不是PNG图像。

enter image description here

关于Delphi将base64字符串转换为PNG图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67077548/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com