serialize a graphicspath?
-
Hi, I want to save a few arraylist into a file. One method that i found out is to use serializable. However, below is the exception error when i try to save the window form. Additional information: The type System.Drawing.Drawing2D.GraphicsPath in Assembly System.Drawing, Version=1.0.5000.0, Culture=neutral, PublicKeyToken.... is not marked as serializable. The program is able to draw shapes into a panel and it has shape classes(e.g. rect class,polygon class...) in those classes it uses graphicspath to add the shapes onto the panel. besides that, one of the arraylist also contains Graphicspath. I'm able to serialize the arraylist other than those that use GraphicsPath. So, can anybody help me to explain what i should do to solve this problem? Thanks in advance to anybody who is willing to lend a helping hand:)
-
Hi, I want to save a few arraylist into a file. One method that i found out is to use serializable. However, below is the exception error when i try to save the window form. Additional information: The type System.Drawing.Drawing2D.GraphicsPath in Assembly System.Drawing, Version=1.0.5000.0, Culture=neutral, PublicKeyToken.... is not marked as serializable. The program is able to draw shapes into a panel and it has shape classes(e.g. rect class,polygon class...) in those classes it uses graphicspath to add the shapes onto the panel. besides that, one of the arraylist also contains Graphicspath. I'm able to serialize the arraylist other than those that use GraphicsPath. So, can anybody help me to explain what i should do to solve this problem? Thanks in advance to anybody who is willing to lend a helping hand:)
everything is going to be serialized must have Serializable Attribute in .Net but GraphicsPath class does not have it so you can't direcly Serialize it however there are some ways to do that I saw a constructor for GraphicsPath Takes 3 arguments (PointF[] points ,byte[] types ,FillMode fillMode) and of course three Property from GraphicPath instance FillPoints:pointF[] ,PathTypes:byte[] ,FillMode:FillModeEnum all of these three type are marked as serializable so you can serialize these data and then desrialize them and create a new GraphicsPath instance identical to the Serialized one. Code some like this
[Serializable] class GraphicsPathData { byte[] types; PointF[] points; FillMode fillMode; public GraphicsPathData(GraphicsPath gp) { this.types = gp.PathTypes; this.points = gp.PathPoints; this.fillMode = gp.FillMode; } public static System.IO.Stream Serialize(GraphicsPathData gpd) { System.IO.MemoryStream ms=new System.IO.MemoryStream(); System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); bf.Serialize(ms, gpd); ms.Flush(); return ms; } public static GraphicsPathData Deserialize(System.IO.Stream stream) { System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); object obj=bf.Deserialize(stream); GraphicsPathData gpd =obj as GraphicsPathData; return gpd; } public static GraphicsPath GetGraphicsPath(GraphicsPathData gpd) { return new GraphicsPath(gpd.points, gpd.types, gpd.fillMode); } public static GraphicsPath GetGraphicsPath(System.IO.Stream gpdStream) { return GetGraphicsPath(Deserialize(gpdStream)); } }
and I tested that likeGraphicsPath gp = new GraphicsPath(); gp.AddEllipse(10, 10, 100, 100); GraphicsPathData gpd = new GraphicsPathData(gp); System.IO.Stream gpdStream = GraphicsPathData.Serialize(gpd); //Just create a new stream for showing exchange of data!!! System.IO.Stream newStream = new System.IO.MemoryStream(((System.IO.MemoryStream)gpdStream).ToArray()); GraphicsPath gpDeserialized =
-
everything is going to be serialized must have Serializable Attribute in .Net but GraphicsPath class does not have it so you can't direcly Serialize it however there are some ways to do that I saw a constructor for GraphicsPath Takes 3 arguments (PointF[] points ,byte[] types ,FillMode fillMode) and of course three Property from GraphicPath instance FillPoints:pointF[] ,PathTypes:byte[] ,FillMode:FillModeEnum all of these three type are marked as serializable so you can serialize these data and then desrialize them and create a new GraphicsPath instance identical to the Serialized one. Code some like this
[Serializable] class GraphicsPathData { byte[] types; PointF[] points; FillMode fillMode; public GraphicsPathData(GraphicsPath gp) { this.types = gp.PathTypes; this.points = gp.PathPoints; this.fillMode = gp.FillMode; } public static System.IO.Stream Serialize(GraphicsPathData gpd) { System.IO.MemoryStream ms=new System.IO.MemoryStream(); System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); bf.Serialize(ms, gpd); ms.Flush(); return ms; } public static GraphicsPathData Deserialize(System.IO.Stream stream) { System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); object obj=bf.Deserialize(stream); GraphicsPathData gpd =obj as GraphicsPathData; return gpd; } public static GraphicsPath GetGraphicsPath(GraphicsPathData gpd) { return new GraphicsPath(gpd.points, gpd.types, gpd.fillMode); } public static GraphicsPath GetGraphicsPath(System.IO.Stream gpdStream) { return GetGraphicsPath(Deserialize(gpdStream)); } }
and I tested that likeGraphicsPath gp = new GraphicsPath(); gp.AddEllipse(10, 10, 100, 100); GraphicsPathData gpd = new GraphicsPathData(gp); System.IO.Stream gpdStream = GraphicsPathData.Serialize(gpd); //Just create a new stream for showing exchange of data!!! System.IO.Stream newStream = new System.IO.MemoryStream(((System.IO.MemoryStream)gpdStream).ToArray()); GraphicsPath gpDeserialized =
Hey, thanks for the great reply. However if i got an 3 arraylist to be serialize, one of them is an arraylist containing graphicspath, how do i deserialize them back to the 3 different arraylist. Should i save the number of items in each of the arraylist? Below is how i put all the arraylist into a single arraylist
ArrayList arr = new ArrayList(); arr.Add(Arraylist1);//arraylist containing Point[] for(int i=0;i What other method do you suggest? Thanks in advance:)
-
Hey, thanks for the great reply. However if i got an 3 arraylist to be serialize, one of them is an arraylist containing graphicspath, how do i deserialize them back to the 3 different arraylist. Should i save the number of items in each of the arraylist? Below is how i put all the arraylist into a single arraylist
ArrayList arr = new ArrayList(); arr.Add(Arraylist1);//arraylist containing Point[] for(int i=0;i What other method do you suggest? Thanks in advance:)
The thing you are doing is going to work and is ok and Yes if you are using arrays you must know what data is where for preventing this you can use HashTable and use Enum or string names for keys these are wrote down here are just suggestions how about to create a GraphicsPathDataCollection Class and pass those GraphicsPaths to it and Serialize/ Deserialize the GraphicsPathDataCollection and take back your array of GraphicsPath after Deserializing. this needs some coding but is the correct way because you have a collection and maybe some other time you simply modify it for other use (like import and export...) there is an easier way too just like you did, but you can put all your GraphicsPathData inside a ArrayList not their serialized streams then Serialize the Array because as you know serializing is not such a fast job exeptionaly if you use SoapFormatter and in this way lots of resources going to be wasted. and one another thing if you are using .Net2.0 why we don't use List<> instead of ArrayList if all of our data are the same!. enum myPhases {gpData /*and other things you need to serialize */}; HashTable tableMustbeSerialized=new HashTable(); List gpList; //your Graphicspaths List gpdList=new List(); foreach(GraphicsPath gp in gpList) gpdList.Add(new GraphicsPathData(gp)); . . . //other things must be Serialized tableMustbeSerialized.Add(myPhases.gpData ,gpdList); . . . //add other Data too formatter.Serialize(myStream, tableMustbeSerialized); myStream.Close(); and of course after deserializing it can be easier retrieved object deserializedObject; HashTable myData=deserializedObject as HashTable; List gpdList=myData[myPhases.gpData] as List; if you need help I'm here :) good luck