【问题标题】:Sending PictureBox over a socket通过套接字发送 PictureBox
【发布时间】:2013-09-20 10:50:03
【问题描述】:

在我的项目中,我在用户之间使用套接字进行通信,我必须将图片框发送给另一个。

这是我如何使用图片框:

 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);

但我不知道如何发送图片框1。希望您能提供帮助,在此先感谢。

【问题讨论】:

    标签: c# sockets picturebox


    【解决方案1】:

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

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

    【讨论】:

    • 谢谢,但我怎样才能把这张照片给接收者看?当接收者通过它时 s.receive()?因为我希望接收者看到发送者的截图
    • @user1902018 你试过什么?您可以将字节读入内存流,然后将其设置为图片框,基本上颠倒了发送它的过程。
    • 其实我不知道怎么做,你能给我举个例子吗?
    【解决方案2】:
    `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

    【讨论】:

    • 谢谢,但是我怎样才能把这张照片给接收者看?当接收者通过它时 s.receive()?因为我想让接收者看到发送者的截图
    【解决方案3】:

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

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

    【讨论】:

      猜你喜欢
      • 2014-08-10
      • 2016-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-22
      • 2011-02-24
      相关资源
      最近更新 更多