0

In my project i am using sockets between users to communicate and i have to send a picturebox one to another.

Here is how i use picturebox:

 PictureBox pictureBox1 = new PictureBox();
        ScreenCapture sc = new ScreenCapture();
        // capture entire screen, and save it to a file
        Image img = sc.CaptureScreen();
        // display image in a Picture control named pictureBox1
        pictureBox1.Image = img;

And i use my sockets to send like this:

byte[] buffer = Encoding.ASCII.GetBytes(textBox1.Text);
            s.Send(buffer);

But i couldn't figure out how i can send pictureBox1.Hope you can help, thanks in advance.

user1902018
  • 1
  • 1
  • 3

3 Answers3

1

You can convert the picturebox image to a byte array using a memory stream:

MemoryStream ms = new MemoryStream();
pictureBox1.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
s.Send(ms.ToArray());
John Koerner
  • 37,428
  • 8
  • 84
  • 134
  • Thanks but how can i show this picture to the receiver?As receiver gets by it s.receive()?Because i want the receiver to see sender's screenshot – user1902018 Jan 04 '13 at 04:11
  • @user1902018 What have you tried? You can read the bytes into a memory stream and then set it to a picturebox, basically reversing the process of sending it. – John Koerner Jan 04 '13 at 04:17
  • Actually i dont know how to do it.Can you give me an example? – user1902018 Jan 04 '13 at 04:29
0
`public byte[] PictureBoxImageToBytes(PictureBox picBox) 
{
     if ((picBox != null) && (picBox.Image != null))
    {
         Bitmap bmp = new Bitmap(picBox.Image);
         System.IO.MemoryStream ms = new System.IO.MemoryStream();

         bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);

           byte[] buff = ms.ToArray();

         ms.Close();
         ms.Dispose();
         return buff;
    }
     else
    {
         return null;
    }
}`

from http://www.codyx.org/snippet_transformer-image-picturebox-tableau-bytes_496.aspx

Vahid Farahmand
  • 2,528
  • 2
  • 14
  • 20
  • Thanks but how can i show this picture to the receiver?As receiver gets by it s.receive()?Because i want the receiver to see sender's screenshot – user1902018 Jan 04 '13 at 04:09
0

Sent by ToArray() and receive then Convert to image

   public static Image ByteArrayToImage(byte[] byteArrayIn)
    {
        var ms = new MemoryStream(byteArrayIn);
        var returnImage = Image.FromStream(ms);
        return returnImage;
    }
Community
  • 1
  • 1
spajce
  • 7,044
  • 5
  • 29
  • 44