• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 804
  • Last Modified:

how to send a *.bmp picture over tcp/ip

i want to send a *.bmp picture over ip.

1. i think the best is to save the picture to a TStream variable.
But how to do that?

2. if i can do that (number one). how should i send the picture to the clients? should i use sendbuf?

3. if i can do number two, how can the client receive the picture. should he/she use the receivebuf? if yes, how to convert it to TStream?

Full source codes in Delphi 4.0 is very appreciated.
0
ichen
Asked:
ichen
1 Solution
 
MadshiCommented:
Can't give you full sources (not enough time), but some hints:

procedure BitmapToBuffer(bmp: TBitmap; var buffer: pointer; var size: integer);
var ms : TMemoryStream;
begin
  ms := TMemoryStream.Create;
  try
    bmp.SaveToStream(ms);
    size := ms.size;
    GetMem(buffer, size);
    Move(ms.memory^, buffer^, size);
  finally ms.Free end;
end;

Don't forget to free the buffer with FreeMem(buffer) after you've sent it!

procedure BufferToBitmap(buffer: pointer; size: integer) : TBitmap;
var ms : TMemoryStream;
begin
  result := TBitmap.Create;
  ms := TMemoryStream.Create;
  try
    ms.size := size;
    Move(buffer^, ms.memory^, size);
    result.ReadFromStream(ms);
  finally ms.Free end;
end;

About the network stuff: Look at this page:

http://www.rtfm.be/fpiette/indexuk.htm

There you'll find very good (and free) components with sources and some demos.

Does this help?

Regards, Madshi.
0
 
EpsylonCommented:
I can e-mail you a bare bone example how to send a buffer using the Delphi tcp-ip components.

Epsylon.
0
 
HamidHossainCommented:
Epsylon

Please Mail it to me

hamidhossain@hotmail.com
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
ichenAuthor Commented:
can anyone put all the sources given by madshi into one procedure?

i mean, for examples when i click the "send" button, inside the onclick, it creates a buffer to store the bitmap and then send it through the server's sendbuf.
and the client's onclick handles the receipt and also the showing of the image.
0
 
EpsylonCommented:
Sorry, but it is not possible to put everything in a single procedure because you need some eventhandlers.
0
 
ichenAuthor Commented:
yes, epsilon, but what i mean is whether someone can put the codes for buffering the pict and perhaps the sound in the sned's "onclick" eventhandler, while inside it it calls other eventhandlers. it also applies to the client's "onread" it changes the buffer into bitmap [and also plays the sound]. thank you.

if you can reply it immediately in 24  hours, i will likely increase the points :) -- to anyone
0
 
nicola65Commented:
Client
procedure TForm1.ClientSocket1Read(Sender: TObject;
  Socket: TCustomWinSocket);
var
  strIn: string;
  Stream: TMemoryStream;
  nReceived: Integer;
  Time1: Cardinal;
begin
 cont:=cont+1;
 label3.Caption:=inttostr(cont);
 listbox2.Items.Add('cont: ' +inttostr(cont) +  ' - ' + inttostr(GetTickCount) + ' - '  + 'c2: ' + inttostr(c2));

  case CliStatus of
    // look for data to receive
    csIdle:
    begin
        else if strIn = 'BITM!' then
        CliStatus := csBitmap
      else if strIn = 'ERROR' then
        CliStatus := csError;
    end;
    // show the messages (might actually not be an error)
    csError:
    begin
      ShowMessage (Socket.ReceiveText);
      cliStatus := csIdle;
    end;
    // get a directory listing
    // read a bitmap file
    csBitmap:
      with TFormBmp.Create (Application) do
      begin
        Stream := TMemoryStream.Create;
        Screen.Cursor := crHourglass;
        try
          while True do
          begin
            nReceived := Socket.ReceiveBuf (Buffer, sizeof (Buffer));
            if nReceived <= 0 then
              Break
            else
              Stream.Write (Buffer, nReceived);
            tot:=tot+nReceived;

            // delay (200 milliseconds)
            Time1 := GetTickCount;
            while GetTickCount - Time1 < 200 do;
          end;
          // reset and load the temporary file
          Stream.Position := 0;
          Image1.Picture.Bitmap.LoadFromStream (Stream);
        finally
          Stream.Free;
          Screen.Cursor := crDefault;
        end;
        Show;
      cliStatus := csIdle;
      socket.Close;
      end;
  end; // case
end;


Server
procedure TForm1.ServerSocket1ClientRead(Sender: TObject;
  Socket: TCustomWinSocket);
var
  strCommand, strFile, strFeedback: string;
begin
  // read from the client
  strCommand := Socket.ReceiveText;
  // extract the file name (all commands have 5 characters)
  strFile := Copy (strCommand, 6, Length (strCommand) - 5);

  // execute program

  // send back a bitmap file
  if Pos ('BITM!', strCommand) = 1 then
  begin
    if FileExists (strFile) then
    begin
      strFeedback := 'BITM!';
      Socket.SendText (strFeedback);
      Socket.SendStream (TFileStream.Create (
        strFile, fmOpenRead or fmShareDenyWrite));
    end
    else
    begin
      strFeedback := 'ERROR' + strFile + ' not found';
      Socket.SendText (strFeedback);
    end;
  end
end;

0
 
ichenAuthor Commented:
I know you got this source from Mastering Delphi :) I already try this program and it's work in the LAN but if i try it in internet .. it doesn't work properly..
0

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now