简体   繁体   English

如何使用WCF和net.tcp绑定流式传输位图?

[英]How do I stream a bitmap using WCF and the net.tcp binding?

I'm trying to pass a bitmap from a client to server using wcf and net.tcp binding. 我正在尝试使用wcf和net.tcp绑定将位图从客户端传递到服务器。

Here's my code so far... please suggest what would be the best way to make it stream that bitmap? 到目前为止,这是我的代码...请提出使它流式传输位图的最佳方法是什么?

    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'.
    }

... ...

[ServiceContract(Namespace = "server",
    CallbackContract = typeof(IfaceServer2Client),
    SessionMode = SessionMode.Required)]


    public interface IfaceClient2Server           ///// what comes from the client to the server.
    {
        [OperationContract(IsOneWay = true)]
        void StartConnection(string clientName);

        [OperationContract(IsOneWay = true)]
        void Message_Cleint2Server(string msg);

        [OperationContract(IsOneWay = true)]
        void ret_listDrives(string n, List<TreeNode> nodeList);

        [OperationContract(IsOneWay = true)]
        void return_screenshot(string n, Bitmap img); /// <- here I'm trying to pass the bitmap.
    }


    public interface IfaceServer2Client          ///// what goes from the sertver, to the client.
    {
        [OperationContract(IsOneWay = true)]
        void AcceptConnection();

        [OperationContract(IsOneWay = true)]
        void RejectConnection();

        [OperationContract(IsOneWay = true)]
        void Message_Server2Client(string msg);

        [OperationContract(IsOneWay = true)]
        void cmd_listDrives();

        [OperationContract(IsOneWay = true)]
        void cmd_changeName(string n);

        [OperationContract(IsOneWay = true)]
        void cmd_screenshot();
    }

Thanks! 谢谢!

edit: 编辑:

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();
}

try serializing the bitmap to a memory stream and hand it over as a byte array ... 尝试将位图序列化到内存流,并将其作为字节数组移交给...

//edit //编辑

example: Bitmap -> byte[] -> Bitmap 示例:位图->字节[]->位图

//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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM