Link to home
Start Free TrialLog in
Avatar of Qisco
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.
Avatar of pcsentinel
pcsentinel

I haven't done it from client to server, but I have done it from Server to Client. The code is below, it might help.


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(FileName: string): boolean;
//core function that retrieves the required image from the Agent
var
      lBinStream : TMemoryStream;
      lPtr : Pointer;
      lv: OLEVariant;
begin
      if eaAgent.GetImage(pChar(FileName),lv) then
      begin
            lBinStream  := TMemoryStream.Create;
            lBinStream.SetSize(VarArrayHighBound(lv,1));
            lPtr:= VarArrayLock(lv);
            Move( lPtr^ , lBinStream.Memory^, lBinStream.Size);
            VarArrayUnLock(lv);
            try
                  imPicture.LoadFromStream(lBinStream);
                  Result:=true;
            except
                  Result:=false;
            end;
            FreeAndNil(lBinStream);
            Application.ProcessMessages;
      end
      else
            Result:=false;
end;



regards
Avatar of Qisco

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
Avatar of Qisco

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(varArrayHighBound(FileName,1));
      lPtr := VarArrayLock(FileName);
      Move(lPtr^,lBinStream.Memory^,lBinStream.Size);
      VarArrayUnlock(FileName);
      try
        imPicture := TBitmap.Create;
        try
            imPicture.LoadFromStream(lBinStream);
            imPicture.SaveToFile('NewBitmap.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(FileName);
            Picture := VarArrayCreate([0,lBinStream.Size - 1],varByte);
            lPtr := VarArrayLock(Picture);
            Move(lBinStream.Memory^,lPtr^,lBinStream.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(ImageToOLEVariant(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.
ASKER CERTIFIED SOLUTION
Avatar of pcsentinel
pcsentinel

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Qisco

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

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(lBinStream);
            Picture := VarArrayCreate([0,lBinStream.Size - 1],varByte);
            lPtr := VarArrayLock(Picture);
            Move(lBinStream.Memory^,lPtr^,lBinStream.Size);
            VarArrayUnlock(Picture);
            EDNAAgent1.SaveImage(Picture);
            FreeAndNil(lBmp);
        except
            on E:Exception do
            begin
                ShowMessage('An Error ocurred');
            end;
        end;
    finally
      lBinStream.Free;
    end;
end;


*************************SERVER CODE************************
procedure TAgent.SaveImage(var BmpFile: OleVariant);
var
    lBinStream : TMemoryStream;
    lPtr : Pointer;
    imPicture:TBitmap;
begin
      lBinStream := TMemoryStream.Create;

      lBinStream.SetSize(varArrayHighBound(BmpFile,1));
      lPtr := VarArrayLock(BmpFile);
      Move(lPtr^,lBinStream.Memory^,lBinStream.Size);
      VarArrayUnlock(BmpFile);
      try
        imPicture := TBitmap.Create;
        try
            imPicture.LoadFromStream(lBinStream);
            imPicture.SaveToFile('c:\NewBitmap.bmp');
        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

Avatar of Qisco

ASKER

Yep sorted the question out and thanx alot