Serialization of a class that can't be serialized but is needed

Started by
3 comments, last by SonicD007 12 years, 2 months ago
Language: C#
API: GDI+

Hi, so I have 3 classes: Map class, Layer class, and Tile class. All of these classes are serializable. The problem is that now I'm trying to add Color keys to the Tile class but the ImageAttributes class isn't Serializable. I need that class for the color keying and I need it to be stored in the save file (it's using binary formatter). So how can I keep the tile class in the know about the Color key if I can't use ImageAttributes and the Drawing function doesn't take any other parameter to draw color keyed images. Thanks.

P.S. The images are being loaded from a file into an Image variable.

ImageAttributes - http://msdn.microsoft.com/en-us/library/system.drawing.imaging.imageattributes.aspx
Image - http://msdn.microsoft.com/en-us/library/system.drawing.image.aspx
Advertisement
So... wrap it up in a class that can be serialized.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

Talking about this problem with a friend, I got as far as knowing I need some sort of class that can be serialized and would probably have a variable of type object that could be cast to ImageAttributes but I'm not sure how to go about doing this. ImageAttributes has a clone method that i know will be useful for me in this situation I just can't see how to do this though. How can I wrap that class into a serializable one and still have my Tile class constructor set the imageattributes to the colorkey it needs?

EDIT: If you don't understand what I mean i can post some code to help you see what I mean

EDIT2: So I "think" I figured out how to store the attributes into an object that gets saved and the object reads back when the map is loaded. Now I wanted to cast that object to ImageAttributes so I could use it in a drawing function of my layer class (which also gets serialized) but apparently casting the object to ImageAttributes throws that same exception as if I had a variable in my class called ImageAttributes myBadVariable; Can someone please help me out? I feel like I'm so close to getting this problem solved =/

Example code:
excerpt from Tile Class

object cloneOfImageAttributes;

public object CloneOfImageAttributes
{
get { return cloneOfImageAttributes; }
set { cloneOfImageAttributes = value; }
}


excerpt from Layer Class

if (selectedTileSet.CloneOfImageAttributes != null)
{
//TODO can't mention Image Attributes at ALL so...figure out how to put it in a parameter
//This is what's causing the error. (casting the tileset object clone variable to ImageAttributes
//e.Graphics.DrawImage(selectedTileSet.Img, new System.Drawing.Rectangle(x * selectedTileSet.TileWidth, y * selectedTileSet.TileHeight, selectedTileSet.TileWidth, selectedTileSet.TileHeight), xTile, yTile, selectedTileSet.TileWidth, selectedTileSet.TileHeight, GraphicsUnit.Pixel, (ImageAttributes)selectedTileSet.CloneOfImageAttributes);
}
else
{
e.Graphics.DrawImage(selectedTileSet.Img, new System.Drawing.Rectangle(x * selectedTileSet.TileWidth, y * selectedTileSet.TileHeight, selectedTileSet.TileWidth, selectedTileSet.TileHeight), xTile, yTile, selectedTileSet.TileWidth, selectedTileSet.TileHeight, GraphicsUnit.Pixel);
}
You can't serialize ImageAttributes. Period. There's no magical casting trick that will make it "serializable".

If you need to serialize ImageAttributes then you will have to provide a wrapper class that holds the information you passed to the ImageAttributes class when constructing it. That wrapper/sxs class should be marked serializable. That sxs/wrapper class could then have a "GetImageAttribute" method which constructs an image attribute and then sets the appropriate values.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

Thanks for the help. I got it working by storing a Color variable to the Tiles class and making the ImageAttribute variable nonserialized and setting it to new in the declaration (so it isn't null when I set the colorkey. Also added a function to the Tile class to set its ImageAttribute to the color that's serialized.

I have to learn what wrapped classes are. Thanks.

This topic is closed to new replies.

Advertisement