通过套接字发送PictureBox

时间:2013-01-04 03:58:45

标签: c# sockets 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你可以提前帮助,谢谢你。

3 个答案:

答案 0 :(得分:1)

您可以使用内存流将picturebox图像转换为字节数组:

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

答案 1 :(得分: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

答案 2 :(得分:0)

ToArray()发送并接收然后转换为image

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