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.
LVL 1
ichenAsked:
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.

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
Cloud Class® Course: Amazon Web Services - Basic

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.

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

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
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
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.