SRL Archive - FAQ

How can I read an image and encode it to Base64?


Patriq

2015-01-15 08:25:49

I dont really have that much experience in file management using pascal.
I saw this snippet online and Simba doesnt have the EncodeBase64 function so Im kinda lost.

function EncodeFile(const FileName: string): AnsiString;
var
stream: TMemoryStream;
begin
stream := TMemoryStream.Create;
try
stream.LoadFromFile(Filename);
result := EncodeBase64(stream.Memory, stream.Size);
finally
stream.Free;
end;
end;

It would be great if someone could help me :D
Thank you very much.

P.S: I need this because I want to be able to create an API to 9kw.eu using simba, and therefore bot in a game that asks for captchas once in a while. I would be creating and sharing a librarie that would allow to do such a thing :D

Turpinator

2015-01-15 09:13:03

http://docs.villavu.com/simba/scriptref/string.html#base64encode
Base64Encode(str);

Patriq

2015-01-15 10:35:20

I know about that one, but it encodes a String. How can I read an image as a string to encode it?

riwu

2015-01-15 10:41:24

If you want to store an image as a variable in Simba, just Tools>Bitmap conversion (converts a bitmap to a string)
check out a bitmap tutorial here for more info

Patriq

2015-01-15 10:57:48

If i turn an image to a bitmap, and then encode it, would it be possible to be encoded as png? The thing is I wanna read the file as png, but the content of it like this http://php.net/manual/en/function.file-get-contents.php, and then encoded it and be able to send using a post request.

Daniel

2015-01-16 03:34:37


var
fd: Integer;
Contents: String;

begin
fd := OpenFile('C:\path\to\file.png', True);
if(fd = -1) then
begin
Writeln('A problem has occurred while trying to open the file. Does it exist?');
Exit;
end;

if(ReadFileString(fd, Contents, FileSize(fd))) then
begin
// Do stuff (Base64Encode here)
Writeln('Contents of file:');
Writeln(Contents);
end else
Writeln('Error reading from file.');

CloseFile(fd);
end.

Patriq

2015-01-16 05:37:12

Thank you very much! :D Just what I needed..
I see how easy it was.. should have dig a little bit more Simba's documentation :D