2

我有一个 WCF 服务,我希望客户端通过调用client.GetStream();应该返回一个从该服务接收图像MemoryStream

    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);
            img = Image.FromStream(ms);
            ms.Position = 0;
            OperationContext.Current.OutgoingMessageHeaders.Action = "image/jpeg";
            return ms;
        }
    }

IScreenShot 界面:

[ServiceContract]
public interface IScreenShot
{
    [OperationContract]
    Stream GetStream();
}

我得到的第一个例外是关于MaxReceivedMessageSize,然后我将其增加到 2MB。
现在我有一个新的例外是:

Multiple headers with name 'Action' and namespace 'http://www.w3.org/2005/08/addressing' found.

请注意,我遵循了此示例的步骤:

来自 MSDN 的示例示例(将图像作为流返回) 但我使用 NetTcpBinding 的区别。

代码有问题吗?或者它不适用于nettcpbinding?

这是客户:

public partial class ScreenImage : Form
{
    ScreenShotClient client;

    public ScreenImage(string baseAddress)
    {
        InitializeComponent();
        NetTcpBinding binding = new NetTcpBinding(SecurityMode.None);
        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());
    }
}
4

0 回答 0