c# - c# 从流中发送/接收图像
问题描述
我目前已经编写了以下代码。客户端和服务器端。我想按需将图像从服务器发送到客户端。该图像将是来自服务器的屏幕截图,因此我将始终使用不同的大小。
发送第一张图像是一项完成得很好的任务。但是当我发送下一个图片框时不会刷新。从调试中我可以看到从服务器到客户端的字节成功通过。但是代码似乎只接收字节而不继续其余代码
“while”循环( client )的“commented”行似乎在调试中工作,但我看不到结果,因为程序卡在 while 循环中,我无法在前台获得应用程序的窗口
我目前正在 Windows 中进行测试,但客户端将进行调整以在 android 上工作
服务器
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Socket client;
private void button2_Click(object sender, EventArgs e)
{
Bitmap bmp = TakingScreenshotEx1();
bmp.Save("1.jpeg", ImageFormat.Jpeg);
byte[] buffer = ReadImageFile("1.jpeg");
int v = client.Send(buffer, buffer.Length, SocketFlags.None);
Console.WriteLine("Image SENT!");
}
private Bitmap TakingScreenshotEx1()
{
//Create a new bitmap.
var bmpScreenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width,
Screen.PrimaryScreen.Bounds.Height,
PixelFormat.Format32bppArgb);
// Create a graphics object from the bitmap.
var g = Graphics.FromImage(bmpScreenshot);
// Take the screenshot from the upper left corner to the right bottom corner.
g.CopyFromScreen(Screen.PrimaryScreen.Bounds.X,
Screen.PrimaryScreen.Bounds.Y,
0,
0,
Screen.PrimaryScreen.Bounds.Size,
CopyPixelOperation.SourceCopy);
return bmpScreenshot;
}
private void button1_Click(object sender, EventArgs e)
{
IPEndPoint iep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9999);
Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
server.Bind(iep);
server.Listen(100);
Console.WriteLine("Waiting for client....");
client = server.Accept();
}
private static byte[] ReadImageFile(String img)
{
FileInfo fileinfo = new FileInfo(img);
byte[] buf = new byte[fileinfo.Length];
FileStream fs = new FileStream(img, FileMode.Open, FileAccess.Read);
fs.Read(buf, 0, buf.Length);
fs.Close();
GC.ReRegisterForFinalize(fileinfo);
GC.ReRegisterForFinalize(fs);
return buf;
}
}
客户
public Form1()
{
InitializeComponent();
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
}
IPEndPoint iep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9999);
Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
byte[] buffer = new byte[1000000];
private void button1_Click(object sender, EventArgs e)
{
if(client.Connected != true)
client.Connect(iep);
// while (true)
// {
int v = client.Receive(buffer, buffer.Length, SocketFlags.None);
Console.WriteLine("Data Received!");
Stream stream = new MemoryStream(buffer);
var img = Bitmap.FromStream(stream);
pictureBox1.Image = img;
// }
}
解决方案
正如所Peter Duniho
指出的那样while loop
阻塞了 UI 线程
我在这里找到了一个合适的解决方案 如何等待线程完成而不阻塞 UI
您还可以检查 Invoking the Main Thread inside Timer Method
推荐阅读
- javascript - JavaScript 数组作为 ref 传递但不能将 ref 分配给新数组?
- c# - 将 datetime2 数据类型转换为 datetime 数据类型异常,日期在 1753/1/1 之后
- python - WALS Model Tensorflow - 为新用户获取推荐
- javascript - 如何在其他浏览器中使用 WebP 格式?
- android - 使用 Android Studio 加入 Firebase
- python - pandas DataFrame 的散点图以 KeyError: 0 结尾
- jquery - 如何在段落末尾添加阅读更多内容?
- webpack - 在 Phoenix 框架中使用 webpack 提供字体
- javascript - Firestore 简单排行榜功能
- c++ - https发布失败c ++