仅供参考:
http://www.delphibbs.com/delphibbs/dispq.asp?lid=2117291
在D6+Access2K中测试通过:
服务端方法如下:
procedure TTestDM.Insert_Img(var Pic: OleVariant);
var P : Pointer;
FileStream:TMemoryStream;
begin
FileStream:=TMemoryStream.Create;
//生成内存流
try
FileStream.Size := VarArrayHighBound(Pic, 1) - VarArrayLowBound(Pic, 1) + 1;
P := VarArrayLock(Pic);
FileStream.Position := 0;
FileStream.Write(P^, fileStream.Size);
//保存到流中
VarArrayUnlock (Pic);
FileStream.Position := 0;
with ADOQuery1do
begin
Close;
SQL.Clear;
SQL.Add(' insert into test(Img) values
Img_Test)');
Parameters.ParamByName('Img_Test').LoadFromStream(FileStream,ftVarBytes);
//从流中写入数据库
ExecSQL;
end;
finally
FreeAndNil(FileStream);
end;
end;
客户端调用如下:
var FileStream:TMemoryStream;
Pic: OleVariant;
P : Pointer;
begin
FileStream:=TMemoryStream.Create;
Image1.Picture.Bitmap.SaveToStream(FileStream);//图片保存到内存流
try
Pic := VarArrayCreate ([0, FileStream.Size - 1], varByte);
P := VarArrayLock (Pic);
FileStream.Position := 0;
FileStream.Read (P^, FileStream.Size);
//保存为 OleVariant
VarArrayUnlock (Pic);
try
SocketConnection1.AppServer.Insert_Img(Pic);
//调用服务器的方法
except
Application.MessageBox('保存出错啦! ','提示信息',MB_OK+MB_ICONINFORMATION);
end;
finally
FreeAndNil(fileStream);
end;
end;