Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to convert a bitmap to Base64 string with Indy?

1,774 views
Skip to first unread message

Ma Xiaoming

unread,
Dec 13, 2007, 10:17:05 PM12/13/07
to
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

unread,
Dec 14, 2007, 12:59:53 AM12/14/07
to
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

unread,
Dec 14, 2007, 6:40:02 AM12/14/07
to
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

unread,
Dec 14, 2007, 8:07:34 PM12/14/07
to
Hi Dennis,

> 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

unread,
Dec 14, 2007, 9:38:54 PM12/14/07
to
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


mphela...@gmail.com

unread,
Nov 27, 2013, 2:43:25 AM11/27/13
to
Thanks Denis... Solution works well i am now embedding it reading text in a blob stream

zbin...@gmail.com

unread,
Sep 24, 2015, 11:12:23 PM9/24/15
to
you can try this free online service to convert bitmap to base64 string online(http://www.online-code.net/base64-image.html).
0 new messages