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.
QiscoAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

pcsentinelCommented:
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
QiscoAuthor Commented:
Thanx let me test this and see how it works would this also work for bitmap images.
pcsentinelCommented:
Yes its should work for any file
Amazon Web Services

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

QiscoAuthor Commented:
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.
pcsentinelCommented:
Ok I think this should sort you out

on the client do

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);
            Agent1.SaveImage(Picture);
            VarArrayUnlock(Picture);
            FreeAndNil(lBmp);
        except
            on E:Exception do
            begin
                ShowMessage('An Error ocurred');
            end;
        end;
    finally
      lBinStream.Free;
    end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
      SaveImage('c:\test1.bmp');
end;

The server code I copied directly from yours

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
QiscoAuthor Commented:
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

pcsentinelCommented:
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;
pcsentinelCommented:
Qisco, did you sort the bug out? if so can you close the question please

regards

QiscoAuthor Commented:
Yep sorted the question out and thanx alot
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.