zoukankan      html  css  js  c++  java
  • 使用.Net Remoting传送Image对象

    Image实现了 ISerializable接口, 理论上是可以被序列化并且通过Remoting传输的, 但事实上如果使用Remoting获取远程Image对象会抛出异常, 使用BinaryFormatter序列化Image对象得到的居然是一个全部是0的byte array, .Net Framework欺骗了我们..我觉得这是个Bug, 用反光器(Reflector..)看Image的GetObjectData方法, 其实它内部也是用一个MemoryStream来得到序列化的byte array. 但返回的竟然个全0的数组..那实现ISerializable有什么用啊?

    直接获取Image不行..但还是有解决方案的, 而且很简单, 在服务器端(Image对象的宿主)加上一个属性, 返回Image的byte array, 再在客户端把byte array转换成Image就OK了..

    示例代码:

    服务器端:

     
            public byte[] ImageData
            {
                get
                {
                    using (MemoryStream ms = new MemoryStream())
                    {
                        _image.Save(ms, ImageFormat.Png);
                        return ms.ToArray();
                    }
                }
            }

    客户端:

     
            Server s = (Server)Activator.GetObject(typeof(Server), "tcp://localhost:10101/WhiteBoardNetRemoting");
            byte[] data = s.ImageData;
            using (MemoryStream ms = new MemoryStream(data))
            {
                Bitmap bm = new Bitmap(ms);
                pictureBox1.Image = bm;
            }
  • 相关阅读:
    bzoj3293 分金币
    考前模板整理
    CF785D Anton and School
    容斥法解决错排问题
    CF1248F Catowice City
    CF1248E Queue in the Train
    CF1244F Chips
    CF1244C The Football Season
    Noip2016Day1T2 天天爱跑步
    Noip2015Day2T3 运输计划
  • 原文地址:https://www.cnblogs.com/Dah/p/503614.html
Copyright © 2011-2022 走看看