C# 如何使用WCF和net.tcp绑定流式传输位图?
我正在尝试使用wcf和net.tcp绑定将位图从客户端传递到服务器 这是我到目前为止的代码。。。请建议什么是最好的方式,使它流的位图C# 如何使用WCF和net.tcp绑定流式传输位图?,c#,wcf,C#,Wcf,我正在尝试使用wcf和net.tcp绑定将位图从客户端传递到服务器 这是我到目前为止的代码。。。请建议什么是最好的方式,使它流的位图 public void ScreenShot() { ... System.Drawing.Bitmap gdiBitmap = new System.Drawing.Bitmap(pictureBox1.Image); apc.server.return_screenshot(name, g
public void ScreenShot()
{
...
System.Drawing.Bitmap gdiBitmap = new System.Drawing.Bitmap(pictureBox1.Image);
apc.server.return_screenshot(name, gdiBitmap);
///The socket connection was aborted.
///This could be caused by an error processing your message or a receive timeout
///being exceeded by the remote host, or an underlying network resource issue.
///Local socket timeout was '00:00:59.9700000'.
}
尝试将位图序列化为内存流,并将其作为字节数组移交 //编辑 示例:位图->字节[]->位图
//lets get a dummy bitmap ...
Bitmap bmp=new Bitmap(Width,Height);
//... and paint the current form
this.DrawToBitmap(bmp,new Rectangle(0,0,Width,Height));
//we want to get a byte[] representation ... a MemoryStreams buffer will do
MemoryStream ms = new MemoryStream();
//save image to stream ... the stream will write it into the buffer
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
//get the buffer
byte[] bitmapBytes = ms.GetBuffer();
//now you can transfer this byte array ...
//on the receiver end you want to get your bitmap back...
//create a new memorystream around the byte array
ms = new MemoryStream(bitmapBytes);
//read the Bitmap back
bmp = (Bitmap)Bitmap.FromStream(ms);
//use it ...
pictureBox1.Image = bmp;
我对这东西有点陌生。。。你能详细解释一下吗?:)谢谢隐马尔可夫模型。。。但它仍然显示相同的错误,当我试图传递字节[]bmpBytes时///套接字连接已中止///这可能是由于处理消息时出错、远程主机超过接收超时时间、///或基础网络资源问题造成的。本地套接字超时为“00:00:59.9950000”。mhh。。。我认为你的问题不在于你提供的代码。。。您是否可以检查另一方是否正在侦听、可访问和响应(在死锁或耗时操作方面)?是的,调用空函数、传递字符串甚至列表都可以正常工作。可能会出现错误,因为此代码将整个消息传递,而不是流式传递消息?您可以发布绑定配置吗?
private void Form1_Load(object sender, EventArgs e)
{
duplex = new ServiceHost(typeof(ServerClass));
NetTcpBinding tcp = new NetTcpBinding();
duplex.AddServiceEndpoint(typeof(IfaceClient2Server), tcp, "net.tcp://localhost:9080/service");
duplex.Open();
}
//lets get a dummy bitmap ...
Bitmap bmp=new Bitmap(Width,Height);
//... and paint the current form
this.DrawToBitmap(bmp,new Rectangle(0,0,Width,Height));
//we want to get a byte[] representation ... a MemoryStreams buffer will do
MemoryStream ms = new MemoryStream();
//save image to stream ... the stream will write it into the buffer
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
//get the buffer
byte[] bitmapBytes = ms.GetBuffer();
//now you can transfer this byte array ...
//on the receiver end you want to get your bitmap back...
//create a new memorystream around the byte array
ms = new MemoryStream(bitmapBytes);
//read the Bitmap back
bmp = (Bitmap)Bitmap.FromStream(ms);
//use it ...
pictureBox1.Image = bmp;