Search code examples
delphisocketstcpindy10

delphi, send image with indy10 from client to server


How I can send from a Timage (clientside) to another Timage(serverside)? I'm using delphi XE3 with idtcpclient1, idtcpserver1 (indy10 component). I already tried to do something but I had some trouble.

Server side:

FileStream := TFileStream.Create('ciao.jpg', fmCreate);
AContext.Connection.IOHandler.LargeStream := True;
AContext.Connection.IOHandler.ReadStream(FileStream); FileStream.Free;
image1.Picture.LoadFromFile(sname);

Client side:

idTCPClient1.IOHandler.LargeStream := True;
FileStream := TFileStream.Create('hello.jpg', fmOpenRead);
IdTCPClient1.IOHandler.Write(FileStream,0,true);
filestream.Free;

Solution

  • Your question is unclear, but it seems that you're trying to transfer the content of one 'TImage' (on the client) to a TImage on the server. It's unclear whether you mean an image file or an actual TImage, though. I'm going to go with "the picture being displayed in a TImage on the client" being sent to the server. You can use TMemoryStream instead of TFileStream. If you really mean to send the image displayed in a TImage.Picture, you can do something like this (untested):

    // Server side
    var
      Jpg: TJpegImage;
    begin
      Strm := TMemoryStream.Create;
      try
        Strm.Position := 0;
        AContext.Connection.IOHandler.LargeStream := True;
        AContext.Connection.IOHandler.ReadStream(Strm);
        Strm.Position := 0;
        Jpg := TJpegImage.Create;
        try
          Jpg.LoadFromStream(Strm);
          Image1.Picture.Assign(Jpg);
        finally
          Jpg.Free;
        end;
      finally
        Strm.Free;
      end;
    end;
    
    // Client side
    IdTCPClient1.IOHandler.LargeStream := True;
    Strm := TMemoryStream.Create;
    try
      Image1.Picture.Graphic.SaveToStream(Strm);
      Strm.Position := 0;
      IdTCPClient1.IOHandler.Write(Strm, 0, True);
    finally
      Strm.Free;
    end;
    

    If that's not what you want, edit your question so we can understand what you're trying to do. (Don't tell us in comments, but actually edit your question to make it more clear.)