Jump to content
  • Advertisement
Sign in to follow this  
SonicD007

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

This topic is 2502 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
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);
}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!