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

Howto display a gif image returned as TByteDynArray

608 views
Skip to first unread message

Stig Johansen

unread,
Nov 9, 2002, 11:10:56 PM11/9/02
to
Hi.

"steve blake" <steb...@hotmail.com> wrote in message
news:3dcd2c2f$1...@newsgroups.borland.com...
> Being a Dilbert fan, I was glad to find a webservice which lets me:
> 1: Get a path of a daily Dilbert gif file. (function
DailyDilbertImagePath:
> WideString; stdcall;)
> 2: Get the actual image as TByteDynArray (function DailyDilbertImage:
> TByteDynArray; stdcall;)
> I created a unit the standard way in D7E by importing the wsdl
> http://www.esynaps.com/WebServices/DailyDiblert.asmx?WSDL
> In the interface, the first function's result is a Widestring, so that's
no
> problem, it simply returns a path, like
>
http://www.dilbert.com/comics/dilbert/archive/images/dilbert2002111108309.gi
> f
> But what I would like to do is to show the image directly in my
application.
> My approach was to use a TImage, and then the Picture property. My graphic
> programming skill is poor, so I would appreciate help on this issue.
> My approach in the code was something like:
> ...
> var
> i: Integer;
> ba: TByteDynArray; //Actually array of byte
> begin
> ba := GetDailyDilbertSoap.DailyDilbertImage;
> MyLabel.Caption := IntToStr(Length(ba)); //returns a value of about
15000
> ... to be continued
>
> But how do I get this array of bytes into an image???

Well, it is a few years since I last programmed graphics, but I think you
should do the following:
Look for TGifImage on google/Jedi.
Move the Bytearray into a stream, and look for .loadfromstream method in
TgifImage.

You may get further assistance in the .graphics group.

--

Med venlig hilsen/Best regards
Stig Johansen
Stig.J...@udvikling.it.dk
(remove dot dk)

steve blake

unread,
Nov 10, 2002, 3:50:46 PM11/10/02
to
Hi Stig,
I will download that component. Actually I thought that I could use
MyImage.Picture.Bitmap.LoadFromStream(MyStream).
The problem is I do not know how to move the data from the bytearray to the
stream.
Any idea?
/Steve

"Stig Johansen" <stig.j...@udvikling.it> wrote in message
news:3dcd...@newsgroups.borland.com...
> Hi.

steve blake

unread,
Nov 10, 2002, 3:48:22 PM11/10/02
to

"Stig Johansen" <stig.j...@udvikling.it> wrote in message
news:3dcd...@newsgroups.borland.com...

Stig Johansen

unread,
Nov 10, 2002, 11:46:32 PM11/10/02
to
Hi.

"steve blake" <steb...@hotmail.com> wrote in message

news:3dcec72a$1...@newsgroups.borland.com...


> Hi Stig,
> I will download that component. Actually I thought that I could use
> MyImage.Picture.Bitmap.LoadFromStream(MyStream).
> The problem is I do not know how to move the data from the bytearray to
the
> stream.
> Any idea?

Try somthing like
MyStream.WriteBuffer(ba[0],Length(ba));

Deepak Shenoy (TeamB)

unread,
Nov 11, 2002, 6:55:38 AM11/11/02
to
> I will download that component. Actually I thought that I could use
> MyImage.Picture.Bitmap.LoadFromStream(MyStream).
> The problem is I do not know how to move the data from the bytearray to
the
> stream.
> Any idea?

I have a few functions for this:

function ByteArrayFromStream( inStream : TMemoryStream ) : TByteDynArray;

procedure ByteArrayToFIle( const ByteArray : TByteDynArray;
const FileName : string );
function FIleToByteArray( const FileName : string ) : TByteDynArray;

procedure CopyToStream( const InArray : TByteDynArray ; outStream :
TStream );
implementation

uses Math;

procedure ByteArrayToFIle( const ByteArray : TByteDynArray;
const FileName : string );
var Count : integer;
F : FIle of Byte;
pTemp : Pointer;
begin
AssignFile( F, FileName );
Rewrite(F);
try
Count := Length( ByteArray );
pTemp := @ByteArray[0];
BlockWrite(F, pTemp^, Count );
finally
CloseFile( F );
end;
end;


function FIleToByteArray( const FileName : string ) : TByteDynArray;
const BLOCK_SIZE=1024;
var BytesRead, BytesToWrite, Count : integer;
F : FIle of Byte;
pTemp : Pointer;
begin
AssignFile( F, FileName );
Reset(F);
try
Count := FileSize( F );
SetLength(Result, Count );
pTemp := @Result[0];
BytesRead := BLOCK_SIZE;
while (BytesRead = BLOCK_SIZE ) do
begin
BytesToWrite := Min(Count, BLOCK_SIZE);
BlockRead(F, pTemp^, BytesToWrite , BytesRead );
pTemp := Pointer(LongInt(pTemp) + BLOCK_SIZE);
Count := Count-BytesRead;
end;
finally
CloseFile( F );
end;
end;

function ByteArrayFromStream( inStream : TMemoryStream ) : TByteDynArray;
var pTemp : pointer;
begin
SetLength(Result, inStream.Size );
pTemp := @Result[0];
inStream.Position := 0;
inStream.Read(pTemp^, inStream.Size);
end;

procedure CopyToStream( const InArray : TByteDynArray ; outStream :
TStream );
var pTemp : Pointer;
begin
pTemp := @InArray[0];
outStream.Write( pTemp^, Length(InArray));
end;

end.

[Ok, I know, terrible names]


--
Deepak Shenoy (TeamB)
Agni Software
http://www.agnisoft.com


steve blake

unread,
Nov 11, 2002, 6:17:07 PM11/11/02
to
Thanks Deepak!
I used your CopyToStream procedure. Then I downloaded TGifImage, making use
of some functionality there.
My code now looks like:
...
procedure TForm1.btnDailyDilbertImageClick(Sender: TObject);
var
ba: TByteDynArray;
ms: TMemoryStream;
DilbertGifImage: TGIFImage;
begin
//Use the webservice to get the image into ba
ba := GetDailyDilbertSoap.DailyDilbertImage;
//Create a memorystream...
ms := TMemoryStream.Create;
//...and copy the byte array to the steam
CopyToStream(ba, ms);
//Set the position to point to the first byte
ms.Position := 0;
//Now create a gif image...
DilbertGifImage := TGIFImage.Create;
//...and load the image from the stream
DilbertGifImage.LoadFromStream(ms);
//Now copy the bitmap representation of DilbertGifImage to the image on
the form
imgDailyDilbert.Picture.Bitmap := DilbertGifImage.Bitmap;
//Finally some cleanup
DilbertGifImage.Free;
ms.Free;
...
It works!


"Deepak Shenoy (TeamB)" <sheno...@SOMETEXTagnisoft.com> wrote in message
news:3dcf...@newsgroups.borland.com...

0 new messages