通过套接字发送PictureBox
本文关键字:PictureBox 套接字 | 更新日期: 2023-09-27 17:50:42
在我的项目中,我使用用户之间的套接字进行通信,并且我必须将一个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);
但我不知道如何发送图片框1。希望你能帮忙,提前谢谢。
您可以使用内存流将picturebox图像转换为字节数组:
MemoryStream ms = new MemoryStream();
pictureBox1.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
s.Send(ms.ToArray());
`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
ToArray((发送并接收然后转换为image
public static Image ByteArrayToImage(byte[] byteArrayIn)
{
var ms = new MemoryStream(byteArrayIn);
var returnImage = Image.FromStream(ms);
return returnImage;
}