比如一个矩形,坐标之内的很好保存,但是Pen,Brush这些对象怎么保存?只要能保存所有数据,下次能构造出完全一样的Pen,Brush就行。
我试过Serialization,XmlSerializer,说什么“System.Drawing.Pen 无法序列化,因为它没有默认的公共构造函数。”是不是我操作错误,请高手赐教!!!

解决方案 »

  1.   

    你就不要用Serialization了,Brush和Pen都没有标记为[Serializable]。你可以自己保存Pen的宽度和颜色,Brush的式样,颜色等等。
      

  2.   

    Brush是一个基类,我不知道真正传入的是什么类型,可能是一个自己写的继承与Brush的类
      

  3.   

    Pen这些,可以tostring保存,打开时候从字符串创建Pen
      

  4.   

    关注,不过本人认为pen,brush这些对象没必要保存,可以用其他方式保留这些当前状态,如果说一些关键构造
    值等等
      

  5.   

    保存6个整数值就好了:Alpha, R, G, B, Width, Style反序列化时用这几个值构造Pen
      

  6.   

     zhujiechang :我的Pen是一个公有属性,我不知道外部是用什么来构造他的,而且在构造后还可以在外部修改。 smallfz :Pen 还有很多属性啊,例如 对齐方式、笔帽、变换、刷子等属性。如果忽略这些属性那就损失了很多功能。
    如果不能保存所有的属性,那就不能把Pen作为公有属性了,而只能将对齐方式、笔帽、变换 等属性暴露出来,不然用户在用一个有笔帽的Pen画一个图然后保存,读出来却发现笔帽没有了...
      

  7.   

    不要怕麻烦,我写过这样的东西(保存在xml中),其中部分不完整,你参考一下。public static void BecomePenXml(XmlDocument xmlDoc, XmlElement elmAddNode, Pen pen)
            {
                XmlElement elmPen = xmlDoc.CreateElement("Pen");
                try
                {
                    XmlAttribute attColor = xmlDoc.CreateAttribute("Color");
                    attColor.InnerText = ColorToString(pen.Color);
                    elmPen.Attributes.Append(attColor);
                }
                catch (Exception e)
                {
                    ErrorsCollection.AddException(e, ErrorLevel.OnlySave);
                }
                XmlAttribute attAlignment = xmlDoc.CreateAttribute("Alignment");
                attAlignment.InnerText = Enum.GetName(typeof(PenAlignment), pen.Alignment);
                elmPen.Attributes.Append(attAlignment);
                XmlAttribute attWidth = xmlDoc.CreateAttribute("Width");
                attWidth.InnerText = pen.Width.ToString();
                elmPen.Attributes.Append(attWidth);
                XmlAttribute attCompoundArrays = xmlDoc.CreateAttribute("CompoundArrays");
                attCompoundArrays.InnerText = FloatArrayToString(pen.CompoundArray);
                elmPen.Attributes.Append(attCompoundArrays);
                try
                {
                    XmlAttribute attCustomStartCap = xmlDoc.CreateAttribute("CustomStartCap");
                    attCustomStartCap.InnerText = Enum.GetName(typeof(CustomLineCap), pen.CustomStartCap);
                    elmPen.Attributes.Append(attCustomStartCap);
                }
                catch
                { }
                try
                {
                    XmlAttribute attCustomEndCap = xmlDoc.CreateAttribute("CustomEndCap");
                    attCustomEndCap.InnerText = Enum.GetName(typeof(CustomLineCap), pen.CustomEndCap);
                    elmPen.Attributes.Append(attCustomEndCap);
                }
                catch { }
                XmlAttribute attDashCap = xmlDoc.CreateAttribute("DashCap");
                attDashCap.InnerText = Enum.GetName(typeof(DashCap), pen.DashCap);
                elmPen.Attributes.Append(attDashCap);
                XmlAttribute attDashOffset = xmlDoc.CreateAttribute("DashOffset");
                attDashOffset.InnerText = pen.DashOffset.ToString();
                elmPen.Attributes.Append(attDashOffset);
                try
                {
                    XmlAttribute attDashPattern = xmlDoc.CreateAttribute("DashPattern");
                    attDashPattern.InnerText = FloatArrayToString(pen.DashPattern);
                    elmPen.Attributes.Append(attDashPattern);
                }
                catch { }
                XmlAttribute attDashStyle = xmlDoc.CreateAttribute("DashStyle");
                attDashStyle.InnerText = Enum.GetName(typeof(DashStyle), pen.DashStyle);
                elmPen.Attributes.Append(attDashStyle);
                XmlAttribute attStartCap = xmlDoc.CreateAttribute("StartCap");
                attStartCap.InnerText = Enum.GetName(typeof(LineCap), pen.StartCap);
                elmPen.Attributes.Append(attStartCap);
                XmlAttribute attEndCap = xmlDoc.CreateAttribute("EndCap");
                attEndCap.InnerText = Enum.GetName(typeof(LineCap), pen.EndCap);
                elmPen.Attributes.Append(attEndCap);
                XmlAttribute attLineJoin = xmlDoc.CreateAttribute("LineJoin");
                attLineJoin.InnerText = Enum.GetName(typeof(LineJoin), pen.LineJoin);
                elmPen.Attributes.Append(attLineJoin);
                XmlAttribute attMiterLimit = xmlDoc.CreateAttribute("MiterLimit");
                attMiterLimit.InnerText = pen.MiterLimit.ToString();
                elmPen.Attributes.Append(attMiterLimit);
                XmlAttribute attPenType = xmlDoc.CreateAttribute("PenType");
                attPenType.InnerText = Enum.GetName(typeof(PenType), pen.PenType);
                elmPen.Attributes.Append(attPenType);
                XmlAttribute attTransform = xmlDoc.CreateAttribute("Transform");
                attTransform.InnerText = FloatArrayToString(pen.Transform.Elements);
                elmPen.Attributes.Append(attTransform);
                BecomeBrushXml(xmlDoc, elmPen, pen.Brush, LinearGradientMode.Horizontal);
                elmAddNode.AppendChild(elmPen);
            }
    public static Pen ExplainPenXml(XmlNode nodePen, RectangleF rectPath)
            {
                if (nodePen == null) return null;
                if (!ContainAttInElement(nodePen, "Color")) return null;
                Color color = StringToColor(nodePen.Attributes["Color"].InnerText.Trim());
                Pen pen = new Pen(color);
                if (ContainAttInElement(nodePen, "Alignment"))
                    pen.Alignment = (PenAlignment)Enum.Parse(typeof(PenAlignment),
                        nodePen.Attributes["Alignment"].InnerText.Trim());
                if (ContainAttInElement(nodePen, "Width"))
                    pen.Width = (float)Convert.ToDecimal(nodePen.Attributes["Width"].InnerText.Trim());
                if (ContainAttInElement(nodePen, "CompoundArrays"))
                {
                    float[] fACompound = StringToFloatArray(nodePen.Attributes["CompoundArrays"].InnerText.Trim());
                    if (fACompound != null) pen.CompoundArray = fACompound;
                }
                if (ContainAttInElement(nodePen, "DashCap"))
                    pen.DashCap = (DashCap)Enum.Parse(typeof(DashCap), nodePen.Attributes["DashCap"].InnerText.Trim());
                if (ContainAttInElement(nodePen, "DashOffset"))
                    pen.DashOffset = float.Parse(nodePen.Attributes["DashOffset"].InnerText.Trim());
                if (ContainAttInElement(nodePen, "DashStyle"))
                    pen.DashStyle = (DashStyle)Enum.Parse(typeof(DashStyle), nodePen.Attributes["DashStyle"].InnerText.Trim());
                if (ContainAttInElement(nodePen, "StartCap"))
                    pen.StartCap = (LineCap)Enum.Parse(typeof(LineCap), nodePen.Attributes["StartCap"].InnerText.Trim());
                if (ContainAttInElement(nodePen, "EndCap"))
                    pen.EndCap = (LineCap)Enum.Parse(typeof(LineCap), nodePen.Attributes["EndCap"].InnerText.Trim());
                if (ContainAttInElement(nodePen, "LineJoin"))
                    pen.LineJoin = (LineJoin)Enum.Parse(typeof(LineJoin), nodePen.Attributes["LineJoin"].InnerText.Trim());
                if (ContainAttInElement(nodePen, "MiterLimit"))
                    pen.MiterLimit = float.Parse(nodePen.Attributes["MiterLimit"].InnerText.Trim());
                if (ContainAttInElement(nodePen, "Transform"))
                {
                    float[] matrixData = StringToFloatArray(nodePen.Attributes["Transform"].InnerText.Trim());
                    if (matrixData.Length == 6)
                    {
                        pen.Transform = new Matrix(matrixData[0], matrixData[1], matrixData[2], matrixData[3], matrixData[4], matrixData[5]);
                    }
                }
                pen.Brush = ExplainBrushXml(nodePen["Brush"], rectPath);
                return pen;
            }
      

  8.   

    嘿嘿,ason909保存的就比较全了...