我试图从WCF服务获取一个Image
我有一个OperationContract函数,它返回一个Image到客户端,
但是当我从客户端调用它时,我得到这个异常: 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.9619978'.
客户:
private void btnNew_Click(object sender, EventArgs e)
{
picBox.Picture = client.GetScreenShot();
}
Run Code Online (Sandbox Code Playgroud)
Service.cs:
public Image GetScreenShot()
{
Rectangle bounds = Screen.GetBounds(Point.Empty);
using (Bitmap bmp = new Bitmap(bounds.Width,bounds.Height))
{
using (Graphics g = Graphics.FromImage(bmp))
{
g.CopyFromScreen(Point.Empty, Point.Empty, bounds.Size);
}
using (MemoryStream ms = new MemoryStream())
{
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
return Image.FromStream(ms);
}
}
}
Run Code Online (Sandbox Code Playgroud)
IScreenShot界面:
[ServiceContract]
public interface IScreenShot
{
[OperationContract]
System.Drawing.Image GetScreenShot();
}
Run Code Online (Sandbox Code Playgroud)
那么为什么会发生这种情况,我该如何解决呢?
我已经明白了.
Stream.Postion = 0所以从开始读取流.在服务中:
public Stream GetStream()
{
Rectangle bounds = Screen.GetBounds(Point.Empty);
using (Bitmap bmp = new Bitmap(bounds.Width, bounds.Height))
{
using (Graphics g = Graphics.FromImage(bmp))
{
g.CopyFromScreen(Point.Empty, Point.Empty, bounds.Size);
}
MemoryStream ms = new MemoryStream();
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
ms.Position = 0; //This's a very important
return ms;
}
}
Run Code Online (Sandbox Code Playgroud)
界面:
[ServiceContract]
public interface IScreenShot
{
[OperationContract]
Stream GetStream();
}
Run Code Online (Sandbox Code Playgroud)
在客户端:
public partial class ScreenImage : Form
{
ScreenShotClient client;
public ScreenImage(string baseAddress)
{
InitializeComponent();
NetTcpBinding binding = new NetTcpBinding(SecurityMode.None);
binding.TransferMode = TransferMode.StreamedResponse;
binding.MaxReceivedMessageSize = 1024 * 1024 * 2;
client = new ScreenShotClient(binding, new EndpointAddress(baseAddress));
}
private void btnNew_Click(object sender, EventArgs e)
{
picBox.Image = Image.FromStream(client.GetStream());
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10872 次 |
| 最近记录: |