服务器端图片回传客户端显示,客户端怎么写啊! ( 积分: 50 )

  • 主题发起人 主题发起人 mnbsg
  • 开始时间 开始时间
M

mnbsg

Unregistered / Unconfirmed
GUEST, unregistred user!
procedure TTserver.GetPicture(out vPicture: OleVariant);
begin
......这里已经处理好了,而且可以保持为jpg图片的,转换成了并且将流转换成了Variant
vPicture:=streamtovar(TMPPicture);
end;
//服务器端已经做了这样的处理;
我只知道框架是这个样子的,请问客户端要显示这个图片该怎么写啊!
procedure TForm1.BitBtn1Click(Sender: TObject);
var p:TMemorystream ;
vPicture:variant ;
begin
???
SocketConnection.AppServer.getp(vPicture) ;
p:= TMemorystream.Create;
vartostream(vPicture,p);
image1.Picture..........????
end;
 
找到了一个,先试验一下
来自: 老屯, 时间: 2000-07-07 1:53:23, ID: 277936
假设服务器端有两个方法,SetBinary and GetBinary, 并且在服务程序的主窗口有两个TImage和一个Memo. 下面给出两个方法:
procedure TBinaryDataObject.GetBinary(out BinaryData: OleVariant);
var MemoryStream: TMemoryStream;
begin

MemoryStream := TMemoryStream.Create;
try
Form1.Image1.Picture.Bitmap.SaveToStream(MemoryStream);
BinaryData := MemoryStreamToOleVariant(MemoryStream);
Form1.Memo1.Lines.Add(format('Bitmap is send out, size=%d',[MemoryStream.size]));
finally
MemoryStream.Free;
end;

end;

procedure TBinaryDataObject.SetBinary(BinaryData: OleVariant);
var MemoryStream: TMemoryStream;
begin

try
MemoryStream := OleVariantToMemoryStream(BinaryData);
MemoryStream.Position := 0;
Form1.Image2.Picture.Bitmap.LoadFromStream(MemoryStream);
Form1.Memo1.Lines.Add(format('Bitmap is recieved, size=%d',[MemoryStream.size]));
finally
MemoryStream.free;
end;

end;

与服务器相对应在客户端,同样有两个图像,一个用来发送给服务器,一个用来接受显示的图像,有下列两段程序:
TForm1 = ....
....
public
{ Public declarations }
Binary: IBinaryDataObject;
//IBinaryDataObject是服务器的COMObject
end;

implementation
procedure TForm1.ButtonPutPictureClick(Sender: TObject);
var
OV: OleVariant;
Stream: TMemoryStream;
begin

if Binary = nil then

Binary := CoBinaryDataObject.CreateRemote(你的服务器机器名);
Stream := TMemoryStream.Create;
try
Image1.Picture.Bitmap.SaveToStream(Stream);
OV := MemoryStreamToOleVariant(Stream);
Binary.SetBinary(OV);
finally
Stream.free;
end;

end;

procedure TForm1.ButtonGetPictureClick(Sender: TObject);
var
OV: OleVariant;
Stream: TMemoryStream;
begin

if Binary = nil then

Binary := CoBinaryDataObject.CreateRemote(你的服务器机器名);

Stream := TMemoryStream.Create;
try
Binary.GetBinary(OV);
Stream := OleVariantToMemoryStream(OV);
Stream.Position := 0;
Image2.Picture.Bitmap.LoadFromStream(Stream);
finally
Stream.free;
end;

end;
 
我这里有一个通过文件名取服务器上文件的图标的例程,可以改动一下用来取图片数据.
服务器端代码:
//向客户端返回数据
procedure TXFileSystem.GetExeFileIcon(const AFileName: WideString;
out AIconData: OleVariant);
var
mm: TMemoryStream;
MyBuffer: Pointer;
begin
mm := TMemoryStream.Create;
try
try
Self.ExtractExeFileIcon(AFileName, mm);
mm.Position := 0;
AIconData := VarArrayCreate([0, mm.Size - 1], VarByte);
MyBuffer := VarArrayLock(AIconData);
mm.Position := 0;
mm.ReadBuffer(MyBuffer^, mm.Size);
VarArrayUnlock(AIconData);
except
on e: Exceptiondo
raise Exception.Create('取Exe文件的图标出错: ' + e.Message);
end;
finally
mm.Free;
end;
end;

//服务器端通过文件名取图标,存入内存流
procedure TXFileSystem.ExtractExeFileIcon(AFileName: string;
mm: TMemoryStream);
var
aIcon: TAdvancedIcon;
IconExe: TIconLibrary;
fpath: string;
ico: TIcon;
sm: TMemoryStream;
begin
aIcon := TAdvancedIcon.Create;
IconExe := TIconLibrary.Create;
try
if SameText(ExtractFileExt(AFileName), '.exe') then
begin
Iconexe.LoadFromFile(AFileName);
if IconExe.Icons.Count >= 1 then
Aicon := TAdvancedIcon(Iconexe.Icons.Objects[0])
else
begin
ico := Ticon.Create;
sm := TMemoryStream.Create;
ImageList1.GetIcon(0, ico);
ico.SaveToStream(sm);
sm.Position := 0;
aicon.LoadFromStream(sm);
ico.Free;
sm.Free;

end;
aIcon.SaveToStream(mm);
end;
if SameText(ExtractFileExt(AFileName), '.lnk') then
begin
lnk.FILENAME := AFileName;
if ExtractFileExt(lnk.LinkTarget) <> '' then
fpath := lnk.LinkTarget
else
fpath := lnk.IconLocation;

if (fpath <> '') and (SameText(ExtractFileExt(fpath), '.ico') <> True)
then
begin
try
IconExe.LoadFromFile(fpath);
aIcon :=
TAdvancedIcon(IconExe.Icons.Objects[lnk.IconIndex]);
aIcon.SaveToStream(mm);
except
ico := Ticon.Create;
ImageList1.GetIcon(0, ico);
ico.SaveToStream(mm);
ico.Free;
end;
end else
begin
if SameText(ExtractFileExt(fpath), '.ico') then
mm.LoadFromFile(fpath)
else
begin
ico := Ticon.Create;
ImageList1.GetIcon(0, ico);
ico.SaveToStream(mm);
ico.Free;
end;
end;
end;

finally
aIcon.Free;
IconExe.Free;
end;
end;

//客户端代码
//与服务器相连的接口
function TRmFSysForm.GetFileSysData: IXFileSystemDisp;
begin
if not cnnFileSys.Connected then
begin
cnnFileSys.Host := Self.Host;
cnnFileSys.Open;
end;
Result := IXFileSystemDisp(IDispatch(cnnFileSys.AppServer));
end;

//通过接口取得图标数据
function TRmFSysForm.GetFileIcon: TIcon;
var
ov: OleVariant;
ms: TStream;
p: Pointer;
begin
if (Trim(Self.FileName) = '') then
begin
Result := nil;
Exit;
end;
if SameText(ExtractFileExt(Self.FileName), '.exe') then
GetFileSysData.GetExeFileIcon(Self.FileName, ov)
else
GetFileSysData.GetFileIcon(Self.FileName, ov);
if VarIsEmpty(ov) then
begin
Result := nil;
end else
begin
ms := TMemoryStream.Create;
try
p := VarArrayLock(ov);
ms.Write(p^, VarArrayHighBound(ov, 1));
VarArrayUnlock(ov);
ms.Position := 0;
FFileIcon.LoadFromStream(ms);
Result := FFileIcon;
finally
ms.Free;
end;
end;

end;
 
这是我按照老屯的修改的
服务器:
procedure TTserver.GetPicture(out A: OleVariant);
begin
try
//Form1.Image1.Picture.Bitmap.SaveToStream(MemoryStream);
//另外的过程已经将图片已经转换MemoryStream并保存到全局变量“APicture”
A := MemoryStreamToOleVariant(APicture);
FrmSvrMain.Memo.Lines.Add(format('Bitmap is send out, size=%d',APicture.size]));
finally
APicture.Free;
end;

end;
//当有客户端调用,服务器窗体的Memo显示“'Bitmap is send out, size=XXXX”,说明这边应该正常啊!XXXX=图片大小;
----
procedure TForm1.BitBtn1Click(Sender: TObject);
var mPicture:TMemorystream ;
vPicture:OleVarian ;
begin
SocketConnection.Connected:=true;
mPicture := TMemoryStream.Create;
try
SocketConnection.AppServer.GetPicture(vPicture) ;//这里提示灾难性故障
mPicture:= TMemorystream.Create;
mPicture:= OleVariantToMemoryStream(vPicture);
mPicture.Position := 0;
Image1.Picture.Bitmap.LoadFromStream(mPicture);
finally
mPicture.free;
end;
------请指点一下!
Binary := CoBinaryDataObject.CreateRemote(你的服务器机器名);
另外这句没有看明白,是DCOM方式吗?我用的是SocketConnection连接
 
哈哈!搞好了搞好了!
谢谢热心的xxj0828,当然也要感谢以前发帖回帖的高人;
procedure TForm1.BitBtn1Click(Sender: TObject);
var mPicture:TMemorystream ;
vPicture:Variant ;
jpg :TJpegImage;
bmp :TBitmap;
begin
SocketConnection.Connected:=true;
jpg:=TJpegImage.Create ;
bmp:=TBitmap.Create ;
mPicture := TMemoryStream.Create;
try
SocketConnection.AppServer.GetPicture(vPicture) ;//这里原来出错是因为服务器端用了个定时器抓图,可能时间不同步,后修改为发送请求才立即抓图就对了!
mPicture:= TMemorystream.Create;
mPicture:= OleVariantToMemoryStream(vPicture);
mPicture.Position := 0;
//Image1.Picture.Bitmap.LoadFromStream(mPicture);//这个传过来的还是jpg,还要先转换一下才行!
jpg.LoadFromStream(mPicture);
bmp.Assign(jpg);
Image1.Picture.Bitmap :=nil;
Image1.Picture.Bitmap :=bmp;
finally
mPicture.free;
jpg.Free ;
bmp.Free ;
end;
 
如果你上面try 失败的话,那下面 finally 里 free 的就会出错
 
谢谢!白河愁,
我也注意到这个问题了,刚修改了也改了贴子!
结帐咯
 
后退
顶部