【问题标题】:Remoting communication could not find nativeImage within type System.Drawing.Image远程通信在 System.Drawing.Image 类型中找不到 nativeImage
【发布时间】:2014-02-20 14:28:14
【问题描述】:

我正在做一个分布式推箱子游戏,其中 .NET Remoting 技术是强制性的。

我在服务器组件中设置游戏逻辑,在客户端对应的窗口显示和键盘控制。在客户端组件必须用显示墙壁、地板和其他东西的游戏图片填充窗口表单时,我收到一个未处理的异常:

未处理的异常:远程通信在“System.Drawing.Image”类型中找不到“nativeImage”字段。

通过谷歌搜索,我发现这是一个老问题,System.Drawing 不打算被序列化,因此需要一种解决方法。

由于我不太喜欢 C#(和一般编程),所以我寻求帮助:

如何在软件组件之间发送PictureBox.Image对象?

我所有的代码都有[Serializable]MarshalByRefObject 标签。

欢迎举例!

提前谢谢你。

【问题讨论】:

    标签: c# remoting system.drawing.imaging


    【解决方案1】:

    经过一番研究,我终于找到了解决方案。 要做的就是把所有的Image对象都转换成String,这样就可以序列化了。

    在服务器端:

    public Bitmap img;
    
    public String ImageToString(Bitmap img)
    {
        byte[] byteArray = new byte[0];
        using (MemoryStream stream = new MemoryStream())
        {
            img.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
            stream.Close();
            byteArray = stream.ToArray();
        }
        return Convert.ToBase64String(byteArray);
    }
    

    在客户端:

    public Bitmap img;
    
    public Bitmap StringToImage(string imageString)
    {
        if (imageString == null) throw new ArgumentNullException("imageString");
        byte[] array = Convert.FromBase64String(imageString);
        Bitmap image = (Bitmap)Bitmap.FromStream(new MemoryStream(array));
        return image;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-10
      • 2020-08-28
      • 1970-01-01
      相关资源
      最近更新 更多