Question

I'm doing a distributed Sokoban game, where .NET Remoting technology is mandatory.

I set up the logic of the game in a server component, and the window display and keyboard control in a client counterpart. At the point from where the client component must fill the window form with a picture of the game displaying the walls, the floor and stuff, i receive an unhandled exception:

Unhandled exception: remoting communication could not find "nativeImage" field within the "System.Drawing.Image" type.

Searching through Google, i discovered this is an old issue where System.Drawing is not intended to be serialized, so, a workaround is needed.

Since i'm not too into C# (and programming in general), i ask for help:

How to send PictureBox.Image objects between software components?

All my code has [Serializable] and MarshalByRefObject tags.

Examples are welcome!

Thank you in advance.

No correct solution

OTHER TIPS

After some research, i finally got the solution. All to do is to convert all Image objects into Strings, so it can be serializable.

At the server side:

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

At the client side:

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;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top