Qisco
asked on
Transfer Pictures Using DCOM
Hello,
I would like to move images from a client PC running my DCOM client to a DCOM server and then save them to a Database connected to the server. I am using Delphi 2005. All i would require assistance on is how to transfer the images from the client to the server.
I would like to move images from a client PC running my DCOM client to a DCOM server and then save them to a Database connected to the server. I am using Delphi 2005. All i would require assistance on is how to transfer the images from the client to the server.
ASKER
Thanx let me test this and see how it works would this also work for bitmap images.
Yes its should work for any file
ASKER
Hello pcsentinel,
I modified your code a bit to look like this
//Here is the TLB Declaration
procedure GetImage(FileName: OleVariant);safecall;
//Here is the server code
procedure TServer.GetImage(FileName: OleVariant);
var
lBinStream : TMemoryStream;
lPtr : Pointer;
imPicture:TBitmap;
begin
lBinStream := TMemoryStream.Create;
lBinStream.SetSize(varArra yHighBound (FileName, 1));
lPtr := VarArrayLock(FileName);
Move(lPtr^,lBinStream.Memo ry^,lBinSt ream.Size) ;
VarArrayUnlock(FileName);
try
imPicture := TBitmap.Create;
try
imPicture.LoadFromStream(l BinStream) ;
imPicture.SaveToFile('NewB itmap.bmp' );
finally
imPicture.Free;
end;
except
ShowMessage('An error has occured');
end;
FreeAndNil(lBinStream);
end;
//On the client side
function TClient.ImageToOLEVariant( FileName: string): OleVariant;
var
lBinStream : TMemoryStream;
lPtr : Pointer;
Picture:OleVariant;
begin
lBinStream := TMemoryStream.Create;
try
try
lBinStream.LoadFromFile(Fi leName);
Picture := VarArrayCreate([0,lBinStre am.Size - 1],varByte);
lPtr := VarArrayLock(Picture);
Move(lBinStream.Memory^,lP tr^,lBinSt ream.Size) ;
VarArrayUnlock(Picture);
Result := Picture;
except
on E:Exception do
begin
ShowMessage('An Error ocurred');
end;
end;
finally
lBinStream.Free;
end;
end;
//I use an openpicturedialog to get the filename and place a call like this
AppServer.GetImage(ImageTo OLEVariant (FileName) );
the image saved on the server has the same size as the as that on the client but it gives an error that it is not a valid bitmap, I do not understand what is wrong.
I modified your code a bit to look like this
//Here is the TLB Declaration
procedure GetImage(FileName: OleVariant);safecall;
//Here is the server code
procedure TServer.GetImage(FileName:
var
lBinStream : TMemoryStream;
lPtr : Pointer;
imPicture:TBitmap;
begin
lBinStream := TMemoryStream.Create;
lBinStream.SetSize(varArra
lPtr := VarArrayLock(FileName);
Move(lPtr^,lBinStream.Memo
VarArrayUnlock(FileName);
try
imPicture := TBitmap.Create;
try
imPicture.LoadFromStream(l
imPicture.SaveToFile('NewB
finally
imPicture.Free;
end;
except
ShowMessage('An error has occured');
end;
FreeAndNil(lBinStream);
end;
//On the client side
function TClient.ImageToOLEVariant(
var
lBinStream : TMemoryStream;
lPtr : Pointer;
Picture:OleVariant;
begin
lBinStream := TMemoryStream.Create;
try
try
lBinStream.LoadFromFile(Fi
Picture := VarArrayCreate([0,lBinStre
lPtr := VarArrayLock(Picture);
Move(lBinStream.Memory^,lP
VarArrayUnlock(Picture);
Result := Picture;
except
on E:Exception do
begin
ShowMessage('An Error ocurred');
end;
end;
finally
lBinStream.Free;
end;
end;
//I use an openpicturedialog to get the filename and place a call like this
AppServer.GetImage(ImageTo
the image saved on the server has the same size as the as that on the client but it gives an error that it is not a valid bitmap, I do not understand what is wrong.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hello PcSentinel,
Yep this works but after the image is moved it brings an error which says
"Variant or Safe Array is locked" is there any extra handling that I require to do
Yep this works but after the image is moved it brings an error which says
"Variant or Safe Array is locked" is there any extra handling that I require to do
Not sure why this would be
I have repeated the client and server code below, check your code against it, if still no joy then post your version so I can check it out
****************CLIENT CODE*********************
procedure TForm1.SaveImage(FileName: string);
var
lBinStream : TMemoryStream;
lPtr : Pointer;
Picture:OleVariant;
lBmp: TBitmap;
begin
lBinStream := TMemoryStream.Create;
try
try
lBmp:=TBitmap.Create;
lBmp.LoadFromFile(FileName );
lBmp.SaveToStream(lBinStre am);
Picture := VarArrayCreate([0,lBinStre am.Size - 1],varByte);
lPtr := VarArrayLock(Picture);
Move(lBinStream.Memory^,lP tr^,lBinSt ream.Size) ;
VarArrayUnlock(Picture);
EDNAAgent1.SaveImage(Pictu re);
FreeAndNil(lBmp);
except
on E:Exception do
begin
ShowMessage('An Error ocurred');
end;
end;
finally
lBinStream.Free;
end;
end;
*************************S ERVER CODE********************** **
procedure TAgent.SaveImage(var BmpFile: OleVariant);
var
lBinStream : TMemoryStream;
lPtr : Pointer;
imPicture:TBitmap;
begin
lBinStream := TMemoryStream.Create;
lBinStream.SetSize(varArra yHighBound (BmpFile,1 ));
lPtr := VarArrayLock(BmpFile);
Move(lPtr^,lBinStream.Memo ry^,lBinSt ream.Size) ;
VarArrayUnlock(BmpFile);
try
imPicture := TBitmap.Create;
try
imPicture.LoadFromStream(l BinStream) ;
imPicture.SaveToFile('c:\N ewBitmap.b mp');
finally
imPicture.Free;
end;
except
ShowMessage('An error has occured');
end;
FreeAndNil(lBinStream);
end;
I have repeated the client and server code below, check your code against it, if still no joy then post your version so I can check it out
****************CLIENT CODE*********************
procedure TForm1.SaveImage(FileName:
var
lBinStream : TMemoryStream;
lPtr : Pointer;
Picture:OleVariant;
lBmp: TBitmap;
begin
lBinStream := TMemoryStream.Create;
try
try
lBmp:=TBitmap.Create;
lBmp.LoadFromFile(FileName
lBmp.SaveToStream(lBinStre
Picture := VarArrayCreate([0,lBinStre
lPtr := VarArrayLock(Picture);
Move(lBinStream.Memory^,lP
VarArrayUnlock(Picture);
EDNAAgent1.SaveImage(Pictu
FreeAndNil(lBmp);
except
on E:Exception do
begin
ShowMessage('An Error ocurred');
end;
end;
finally
lBinStream.Free;
end;
end;
*************************S
procedure TAgent.SaveImage(var BmpFile: OleVariant);
var
lBinStream : TMemoryStream;
lPtr : Pointer;
imPicture:TBitmap;
begin
lBinStream := TMemoryStream.Create;
lBinStream.SetSize(varArra
lPtr := VarArrayLock(BmpFile);
Move(lPtr^,lBinStream.Memo
VarArrayUnlock(BmpFile);
try
imPicture := TBitmap.Create;
try
imPicture.LoadFromStream(l
imPicture.SaveToFile('c:\N
finally
imPicture.Free;
end;
except
ShowMessage('An error has occured');
end;
FreeAndNil(lBinStream);
end;
Qisco, did you sort the bug out? if so can you close the question please
regards
regards
ASKER
Yep sorted the question out and thanx alot
in DCOM Server
function GetImage(const FileName: WideString;
var Picture: OleVariant): WordBool; safecall;
function TAgent.GetImage(const FileName: WideString;
var Picture: OleVariant): WordBool;
var
lBinStream : TMemoryStream;
lPtr : Pointer;
begin
lBinStream := TMemoryStream.Create;
try
try
lBinStream.LoadFromFile( Filename );
Picture := VarArrayCreate( [0, lBinStream.Size-1], varByte );
lPtr := VarArrayLock(Picture);
Move( lBinStream.Memory^, lPtr^ , lBinStream.Size);
VarArrayUnLock(Picture);
Result:=true;
except
on E:Exception do
begin
fLastError:='GetImage '+E.Message;
WriteErrorLog;
Result := false;
end;
end;
finally
lBinStream.Free;
end;
end;
Client Code
function TfrmMain.GetTempImage(File
//core function that retrieves the required image from the Agent
var
lBinStream : TMemoryStream;
lPtr : Pointer;
lv: OLEVariant;
begin
if eaAgent.GetImage(pChar(Fil
begin
lBinStream := TMemoryStream.Create;
lBinStream.SetSize(VarArra
lPtr:= VarArrayLock(lv);
Move( lPtr^ , lBinStream.Memory^, lBinStream.Size);
VarArrayUnLock(lv);
try
imPicture.LoadFromStream(l
Result:=true;
except
Result:=false;
end;
FreeAndNil(lBinStream);
Application.ProcessMessage
end
else
Result:=false;
end;
regards