2013-01-04 148 views
0

在我的项目中,我使用用户之间的套接字进行通信,并且我必须将一个picturebox发送给另一个。将PictureBox发送到套接字上

这是我如何利用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; 

我用我的套接字来发送这样的:

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

,但我无法弄清楚如何我可以给你pictureBox1.Hope可以提前帮助,谢谢。

回答

1

可以在PictureBox图像使用存储器流转换为一个字节数组:

MemoryStream ms = new MemoryStream(); 
pictureBox1.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Png); 
s.Send(ms.ToArray()); 
+0

谢谢,但我怎么能显示这张照片给接收器?当接收器通过它s.rec eive()?因为我想让接收者看到发件人的屏幕截图 – user1902018

+0

@ user1902018你试过了什么?您可以将字节读入内存流,然后将其设置为一个picturebox,基本上颠倒了发送它的过程。 –

+0

其实我不知道该怎么做。你能举个例子吗? – user1902018

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; 
    } 
}` 

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

+0

谢谢,但我怎么能显示这个图片到接收器?由于接收器获取s.receive()?因为我想接收器看到发件人的屏幕截图 – user1902018

0

通过ToArray()发送和接收然后转换为image

public static Image ByteArrayToImage(byte[] byteArrayIn) 
    { 
     var ms = new MemoryStream(byteArrayIn); 
     var returnImage = Image.FromStream(ms); 
     return returnImage; 
    }