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