public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) { if (!(value is string)) { throw new NotSupportedException("SizeConverter only supports converting from strings"); } return(Size.Parse((string)value)); }
/// <summary>Attempts to convert a specified object to an instance of <see cref="T:System.Windows.Size" />.</summary> /// <returns>The instance of <see cref="T:System.Windows.Size" /> that is created from the converted <paramref name="source" />.</returns> /// <param name="context">Provides contextual information about a component.</param> /// <param name="culture">Culture-specific information that should be respected during conversion.</param> /// <param name="value">The source object that is being converted.</param> public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) { if (value == null) { throw GetConvertFromException(value); } string text = value as string; if (text != null) { return(Size.Parse(text)); } return(base.ConvertFrom(context, culture, value)); }
public object ConvertFrom(XamlNamespaces namespaces, object value) { return(Size.Parse(value.ToString().Trim())); }
public static bool RenderFromXml(Stream xmlStream, IRenderContext renderContext, out Size imageSize) { XmlDocument doc = new XmlDocument(); doc.Load(xmlStream); XmlNamespaceManager namespaceManager = new XmlNamespaceManager(doc.NameTable); namespaceManager.AddNamespace("p", PreviewNamespace); XmlNode previewNode = doc.SelectSingleNode("/p:preview", namespaceManager); imageSize = new Size(double.Parse(previewNode.Attributes["width"].InnerText), double.Parse(previewNode.Attributes["height"].InnerText)); XmlNodeList renderNodes = previewNode.ChildNodes; foreach (XmlNode renderNode in renderNodes) { XmlElement renderElement = renderNode as XmlElement; if (renderElement == null) { continue; } if (renderElement.Name == "line") { Point start = Point.Parse(renderElement.Attributes["start"].InnerText); Point end = Point.Parse(renderElement.Attributes["end"].InnerText); double thickness = double.Parse(renderElement.Attributes["thickness"].InnerText); renderContext.DrawLine(start, end, thickness); } else if (renderElement.Name == "rect") { Point start = Point.Parse(renderElement.Attributes["start"].InnerText); Size size = Size.Parse(renderElement.Attributes["size"].InnerText); double thickness = double.Parse(renderElement.Attributes["thickness"].InnerText); bool fill = bool.Parse(renderElement.Attributes["fill"].InnerText); renderContext.DrawRectangle(start, size, thickness, fill); } else if (renderElement.Name == "ellipse") { Point centre = Point.Parse(renderElement.Attributes["centre"].InnerText); double radiusx = double.Parse(renderElement.Attributes["rx"].InnerText); double radiusy = double.Parse(renderElement.Attributes["ry"].InnerText); double thickness = double.Parse(renderElement.Attributes["thickness"].InnerText); bool fill = bool.Parse(renderElement.Attributes["fill"].InnerText); renderContext.DrawEllipse(centre, radiusx, radiusy, thickness, fill); } else if (renderElement.Name == "path") { Point start = Point.Parse(renderElement.Attributes["start"].InnerText); double thickness = double.Parse(renderElement.Attributes["thickness"].InnerText); bool fill = bool.Parse(renderElement.Attributes["fill"].InnerText); string data = renderElement.InnerText; List <IPathCommand> pathCommands = new List <IPathCommand>(); using (MemoryStream dataStream = new MemoryStream(Convert.FromBase64String(data))) { BinaryReader reader = new BinaryReader(dataStream); int numCommands = reader.ReadInt32(); for (int l = 0; l < numCommands; l++) { CommandType pType = (CommandType)reader.ReadInt32(); IPathCommand theCommand = null; switch (pType) { case CommandType.MoveTo: theCommand = new MoveTo(); break; case CommandType.LineTo: theCommand = new LineTo(); break; case CommandType.CurveTo: theCommand = new CurveTo(); break; case CommandType.EllipticalArcTo: theCommand = new EllipticalArcTo(); break; case CommandType.QuadraticBeizerCurveTo: theCommand = new QuadraticBeizerCurveTo(); break; case CommandType.SmoothCurveTo: theCommand = new SmoothCurveTo(); break; case CommandType.SmoothQuadraticBeizerCurveTo: theCommand = new SmoothQuadraticBeizerCurveTo(); break; default: theCommand = new ClosePath(); break; } theCommand.Read(reader); pathCommands.Add(theCommand); } } renderContext.DrawPath(start, pathCommands, thickness, fill); } else if (renderElement.Name == "text") { Point anchor = Point.Parse(renderElement.Attributes["anchor"].InnerText); TextAlignment alignment = (TextAlignment)Enum.Parse(typeof(TextAlignment), renderElement.Attributes["alignment"].InnerText); List <TextRun> runs = new List <TextRun>(); foreach (XmlNode runNode in renderElement.ChildNodes) { if (runNode.Name != "run") { continue; } double size = double.Parse(runNode.Attributes["size"].InnerText); TextRunFormattingType formattingType = (TextRunFormattingType)Enum.Parse(typeof(TextRunFormattingType), runNode.Attributes["formatting"].InnerText); string text = runNode.InnerText; runs.Add(new TextRun(text, new TextRunFormatting(formattingType, size))); } renderContext.DrawText(anchor, alignment, runs); } } return(true); }