
 /**/
/////////////////////////////////////////////////////////////////////////
 ////////////////////Web?Service?
 unit?Unit3;


interface
 uses?InvokeRegistry,Graphics,Classes,Unit2,Types;

 type
 ??TQueryCapital?
=
?
class
(TInvokableClass,?IQueryCapital)
 ??
public
 ????function?GetPic:TByteDynArray;?stdcall;
 ????function?GetPicSize:LongInt;?stdcall;
 ??end;

 implementation

 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;


//
TByteDynArray?做返回值?是重点

function?TQueryCapital.GetPic:TByteDynArray;
 var
 ??tfs?:?TMemorystream;
 ??outByAr?:?TByteDynArray;
 begin
 ??tfs:
=
TMemorystream.Create;
 ??tfs.LoadFromFile(
'
1.jpg
'
);
 ??tfs.Position???:
=
???
0
;

 ??Result?:
=
?ByteArrayFromStream(tfs);
 end;

 function?TQueryCapital.GetPicSize:?LongInt;
 var
 ??Pic?:?TPicture;
 ??tfs?:?TMemorystream;
 ??ss?:
string
;
 begin
 ??ss?:
=
''
;
 ??tfs:
=
TMemorystream.Create;
 ??tfs.LoadFromFile(
'
1.jpg
'
);
 ??tfs.Position???:
=
???
0
;

 ??SetLength(ss,???tfs.Size);
 ??tfs.Read(ss[
1
],???tfs.Size);
 ??Result?:
=
?Length(ss);
 end;

 initialization

 ??InvRegistry.RegisterInvokableClass(TQueryCapital
...
{,?ShakespeareFactory}
);
 end.



/**/
/////////////////////////////////////////////////////////////////////////////
 //////////Client
 unit?Unit1;


interface

 uses
 ??Windows,?Messages,?SysUtils,?Variants,?Classes,?Graphics,?Controls,?Forms,
 ??Dialogs,iquerycapital1,Types,?ExtCtrls,?StdCtrls,?Rio,?SOAPHTTPClient,jpeg;

 type
 ??TForm1?
=
?
class
(TForm)
 ????HTTPRIO1:?THTTPRIO;
 ????Button1:?TButton;
 ????Image1:?TImage;
 ????procedure?Button1Click(Sender:?TObject);
 ??
private
 ????procedure?ByteArrayCompressedToImage(
const
?ByteArray?:
 ??????TByteDynArray);

 ????
...
{?Private?declarations?}
 ??
public

 ????
...
{?Public?declarations?}
 ??end;

 var
 ??Form1:?TForm1;

 implementation



...
{$R?*.dfm}


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


 procedure?TForm1.ByteArrayCompressedToImage(?
const
?ByteArray?:?TByteDynArray);
 var?MStream?:?TMemoryStream;

 ????Jpg?:?TJpegImage;
 begin
 ??MStream?:
=
?TMemoryStream.Create;
 ??CopyToStream(?ByteArray,?MStream?);
 ??MStream.Position?:
=
?
0
;
 ??Jpg?:
=
TJpegImage.Create;
 ??
try
 ????Image1.Picture.Graphic:
=
Jpg;???????
//
设置图片框的格式

????Image1.Picture.Graphic.LoadFromStream(MStream);???????
//
将地址下的文件通过内存流写入图片框中

????Image1.Repaint;


 ??
finally
 ????MStream.Free;

 ??end;
 end;

 procedure?TForm1.Button1Click(Sender:?TObject);
 var
 ??ByteArray?:?TByteDynArray;
 begin
 ??ByteArray?:
=
?(HTTPRIO1?
as
?IQueryCapital).GetPic;
 ??ByteArrayCompressedToImage(ByteArray);
 end;

 end.


/**/
/////////////////////////////////////////////////////////////////////////////////////////////////////
 //
为一个朋友面试做的小程序程序:

//
通过WebServices?访问服务器上的一张图片

//
开始的时候通过?服务器上加载?图片文件,然后通过WebService?直接传TPicture?对象到Client。发现这条路不行;

//
后来想到用?WebService传String到Client;?图片文件转成String成功。Client段接收WebService传过来的String,数据出现丢失 

//
用?TByteDynArray?传图片文件成功!!!!!!!!

 ??






(编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|