Discussion:
How to convert a bitmap to Base64 string with Indy?
(too old to reply)
Ma Xiaoming
2007-12-14 03:17:05 UTC
Permalink
Dear ladies and gentlemen,

I need to convert a bitmap to Base64 string. I have Delphi 6, and found
that there is a Indy components suite for Delphi 6. I think that maybe the
IDBase64Encoder component could do the job. But I don't know how to achieve
this functionality with the component.

Is it possible to convert a bitmap t0 Base64 string with Indy? If it
could, how?

Help me, please. Thank you very much.

Best regards.

Xiaoming
Ma Xiaoming
2007-12-14 05:59:53 UTC
Permalink
Hi,

I have tried to convert a bitmap to Base64 string as following:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

procedure TForm1.Button1Click(Sender: TObject);
var
bmp: TBitmap;
mStream: TMemoryStream;
bArray: array of Integer;
i: Integer;
s: string;
s64: string;
begin
bmp := TBitmap.Create;
mStream := TMemoryStream.Create;
try
bmp.Width := 100;
bmp.Height := 100;
bmp.PixelFormat := pf24bit;
bmp.SaveToStream(mStream);
mStream.Position := 0;

SetLength( bArray, Integer(mStream.Size) );
for i := 0 to High(bArray) do
begin
mStream.Read(bArray[i], 1);
end;

for i := 0 to High(bArray) do
begin
s := s + IntToStr(bArray[i]);
end;

s64 := IDBase64Encoder1.CodeString(s);

for i := 1 to 1000 do
begin
ListBox1.Items.Add(s64[i]);
end;
finally
bmp.Free;
mStream.Free;
end;
end;

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

It seems work, am I right?

Best regards.

Xiaoming
Dennis Passmore
2007-12-14 11:40:02 UTC
Permalink
without using INDY you can do it directly in code as follows


uses
EncdDecd, zlib;

function CompressAndEncode(const input: string): string;
var
fCompressedStream: TMemoryStream;
fStreamCompressor: TCompressionStream;
fStringStream: TStringStream;
begin
fCompressedStream := TMemoryStream.Create;
try
fStreamCompressor := TCompressionStream.Create(clDefault, fCompressedStream);
try
fStreamCompressor.Write(input[1], length(input));
finally
FreeAndNil(fStreamCompressor);
end;
fStringStream := TStringStream.create('');
try
fCompressedStream.Position := 0;
EncodeStream(fCompressedStream, fStringStream);
Result := fStringStream.datastring;
finally
FreeAndNil(fStringStream);
end;
finally
FreeAndNil(fCompressedStream);
end;
end;

function DecodeAndUnCompress(const input: string): string;
var
fWorkStream: TMemoryStream;
fEncodedStream: TMemoryStream;
fStreamDeCompressor: TDeCompressionStream;
fBufLen: integer;
fBuffer: array[0..16383] of byte;
begin

fWorkStream := TMemoryStream.create;
try
fWorkStream.size := length(input);
move(input[1], fWorkStream.memory^, fWorkStream.size);
fWorkStream.position := 0;
fEncodedStream := TMemoryStream.create;
try
DecodeStream(fWorkStream, fEncodedStream);
fWorkStream.size := 0;
fEncodedStream.position := 0;
fStreamDeCompressor := TDeCompressionStream.create(fEncodedStream);
try
fBufLen := fStreamDeCompressor.Read(fBuffer, SizeOf(fBuffer));
while fBufLen > 0 do
begin
fWorkStream.Write(fBuffer, fBufLen);
fBufLen := fStreamDeCompressor.Read(fBuffer, SizeOf(fBuffer));
end;
finally
FreeAndNil(fStreamDeCompressor);
end;
finally
FreeAndnil(fEncodedStream);
end;
fWorkStream.position := 0;
SetLength(Result, fWorkStream.size);
move(fWorkStream.memory^, Result[1], fWorkStream.size);
finally
FreeAndNil(fWorkStream);
end;

end;

procedure TForm1.Button1Click(Sender: TObject);
var
bmp: TBitmap;
mStream: TMemoryStream;
i: Integer;
s, s64: string;
b: byte;
ts: TStringlist;
begin
if OpenPictureDialog1.execute then
begin
bmp := TBitmap.Create;
bmp.LoadFromFile(OpenPictureDialog1.FileName);
mStream := TMemoryStream.Create;
try
bmp.SaveToStream(mStream);
mStream.Position := 0;
for i := 0 to mStream.size do
begin
mStream.read(b,1);
s64 := s64 + char(b);
end;

s := CompressAndEncode(s64);
ts := TStringlist.create;
ts.add(s);
ts.SaveToFile('test.b64');
ts.free;

finally
bmp.Free;
mStream.Free;
end;
end;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
bmp: TBitmap;
mStream: TMemoryStream;
s, s64: string;
begin
with TStringlist.create do
begin
loadfromfile('test.b64');
s64 := Text;
free;
end;
s := DecodeAndUnCompress(s64);
mStream := TMemoryStream.Create;
mStream.WriteBuffer(s[1],length(s));
bmp := TBitmap.Create;
mstream.Position := 0;
bmp.LoadFromStream(mstream);
// bmp.SaveToFile('test.bmp');
mstream.free;
Image1.Picture.Bitmap := bmp;
bmp.free;
end;
Ma Xiaoming
2007-12-15 01:07:34 UTC
Permalink
Hi Dennis,
Post by Dennis Passmore
without using INDY you can do it directly in code as follows
Many thanks for your help. I will try your solution.

Best regards.

Xiaoming
Ma Xiaoming
2007-12-15 02:38:54 UTC
Permalink
Hi Dennis,

Your code works well. I have eliminated the Compression/Decompression
part of the code, and it still works.

Thanks a lot, again.

Best regards.

Xiaoming
m***@gmail.com
2013-11-27 07:43:25 UTC
Permalink
Post by Dennis Passmore
without using INDY you can do it directly in code as follows
uses
EncdDecd, zlib;
function CompressAndEncode(const input: string): string;
var
fCompressedStream: TMemoryStream;
fStreamCompressor: TCompressionStream;
fStringStream: TStringStream;
begin
fCompressedStream := TMemoryStream.Create;
try
fStreamCompressor := TCompressionStream.Create(clDefault, fCompressedStream);
try
fStreamCompressor.Write(input[1], length(input));
finally
FreeAndNil(fStreamCompressor);
end;
fStringStream := TStringStream.create('');
try
fCompressedStream.Position := 0;
EncodeStream(fCompressedStream, fStringStream);
Result := fStringStream.datastring;
finally
FreeAndNil(fStringStream);
end;
finally
FreeAndNil(fCompressedStream);
end;
end;
function DecodeAndUnCompress(const input: string): string;
var
fWorkStream: TMemoryStream;
fEncodedStream: TMemoryStream;
fStreamDeCompressor: TDeCompressionStream;
fBufLen: integer;
fBuffer: array[0..16383] of byte;
begin
fWorkStream := TMemoryStream.create;
try
fWorkStream.size := length(input);
move(input[1], fWorkStream.memory^, fWorkStream.size);
fWorkStream.position := 0;
fEncodedStream := TMemoryStream.create;
try
DecodeStream(fWorkStream, fEncodedStream);
fWorkStream.size := 0;
fEncodedStream.position := 0;
fStreamDeCompressor := TDeCompressionStream.create(fEncodedStream);
try
fBufLen := fStreamDeCompressor.Read(fBuffer, SizeOf(fBuffer));
while fBufLen > 0 do
begin
fWorkStream.Write(fBuffer, fBufLen);
fBufLen := fStreamDeCompressor.Read(fBuffer, SizeOf(fBuffer));
end;
finally
FreeAndNil(fStreamDeCompressor);
end;
finally
FreeAndnil(fEncodedStream);
end;
fWorkStream.position := 0;
SetLength(Result, fWorkStream.size);
move(fWorkStream.memory^, Result[1], fWorkStream.size);
finally
FreeAndNil(fWorkStream);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
bmp: TBitmap;
mStream: TMemoryStream;
i: Integer;
s, s64: string;
b: byte;
ts: TStringlist;
begin
if OpenPictureDialog1.execute then
begin
bmp := TBitmap.Create;
bmp.LoadFromFile(OpenPictureDialog1.FileName);
mStream := TMemoryStream.Create;
try
bmp.SaveToStream(mStream);
mStream.Position := 0;
for i := 0 to mStream.size do
begin
mStream.read(b,1);
s64 := s64 + char(b);
end;
s := CompressAndEncode(s64);
ts := TStringlist.create;
ts.add(s);
ts.SaveToFile('test.b64');
ts.free;
finally
bmp.Free;
mStream.Free;
end;
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
bmp: TBitmap;
mStream: TMemoryStream;
s, s64: string;
begin
with TStringlist.create do
begin
loadfromfile('test.b64');
s64 := Text;
free;
end;
s := DecodeAndUnCompress(s64);
mStream := TMemoryStream.Create;
mStream.WriteBuffer(s[1],length(s));
bmp := TBitmap.Create;
mstream.Position := 0;
bmp.LoadFromStream(mstream);
// bmp.SaveToFile('test.bmp');
mstream.free;
Image1.Picture.Bitmap := bmp;
bmp.free;
end;
Thanks Denis... Solution works well i am now embedding it reading text in a blob stream
z***@gmail.com
2015-09-25 03:12:22 UTC
Permalink
you can try this free online service to convert bitmap to base64 string online(http://www.online-code.net/base64-image.html).
Loading...