public WpfSvgPaint(WpfDrawingContext context, SvgStyleableElement elm, string propName) : base(elm.GetComputedStyle("").GetPropertyValue(propName)) { _propertyName = propName; _element = elm; _context = context; }
public override Brush GetBrush(Rect elementBounds, WpfDrawingContext context) { SvgLinearGradientElement linearGrad = _gradientElement as SvgLinearGradientElement; if (linearGrad != null) { return GetLinearGradientBrush(elementBounds, linearGrad); } SvgRadialGradientElement radialGrad = _gradientElement as SvgRadialGradientElement; if (radialGrad != null) { return GetRadialGradientBrush(elementBounds, radialGrad); } return new SolidColorBrush(Colors.Black); }
public override BitmapSource Visit(SvgImageElement element, WpfDrawingContext context) { string sURI = element.Href.AnimVal; int nColon = sURI.IndexOf(":"); int nSemiColon = sURI.IndexOf(";"); int nComma = sURI.IndexOf(","); string sMimeType = sURI.Substring(nColon + 1, nSemiColon - nColon - 1); string sContent = sURI.Substring(nComma + 1); byte[] imageBytes = Convert.FromBase64CharArray(sContent.ToCharArray(), 0, sContent.Length); //BitmapImage imageSource = new BitmapImage(); //imageSource.BeginInit(); //imageSource.StreamSource = new MemoryStream(imageBytes); //imageSource.EndInit(); return new EmbeddedBitmapSource(new MemoryStream(imageBytes)); }
public override Brush GetBrush(Rect elementBounds, WpfDrawingContext context) { Rect bounds = new Rect(0, 0, 1, 1); Drawing image = GetImage(context); Rect destRect = GetDestRect(bounds); DrawingBrush tb = new DrawingBrush(image); //tb.Viewbox = new Rect(0, 0, destRect.Width, destRect.Height); //tb.Viewport = new Rect(0, 0, destRect.Width, destRect.Height); tb.Viewbox = destRect; tb.Viewport = destRect; tb.ViewboxUnits = BrushMappingMode.Absolute; tb.ViewportUnits = BrushMappingMode.Absolute; tb.TileMode = TileMode.Tile; MatrixTransform transform = GetTransformMatrix(image.Bounds); if (transform != null && !transform.Matrix.IsIdentity) { tb.Transform = transform; } return tb; }
public override WpfFontFamilyInfo Visit(string fontName, WpfFontFamilyInfo familyInfo, WpfDrawingContext context) { if (String.IsNullOrEmpty(fontName)) { return null; } if (fontName.StartsWith("Arial", StringComparison.OrdinalIgnoreCase) && fontName.Length > 5) { if (String.Equals(fontName, "ArialMT", StringComparison.OrdinalIgnoreCase)) { return new WpfFontFamilyInfo(_arialFamily, familyInfo.Weight, familyInfo.Style, familyInfo.Stretch); } else if (String.Equals(fontName, "Arial-BoldMT", StringComparison.OrdinalIgnoreCase)) { return new WpfFontFamilyInfo(_arialFamily, FontWeights.Bold, familyInfo.Style, familyInfo.Stretch); } else if (String.Equals(fontName, "Arial-ItalicMT", StringComparison.OrdinalIgnoreCase)) { return new WpfFontFamilyInfo(_arialFamily, familyInfo.Weight, FontStyles.Italic, familyInfo.Stretch); } else if (String.Equals(fontName, "Arial-BoldItalicMT", StringComparison.OrdinalIgnoreCase)) { return new WpfFontFamilyInfo(_arialFamily, FontWeights.Bold, FontStyles.Italic, familyInfo.Stretch); } else if (String.Equals(fontName, "Arial Unicode MS", StringComparison.OrdinalIgnoreCase)) { return null; } return new WpfFontFamilyInfo(_arialFamily, familyInfo.Weight, familyInfo.Style, familyInfo.Stretch); } else if (fontName.StartsWith("Helvetica", StringComparison.OrdinalIgnoreCase)) { if (String.Equals(fontName, "Helvetica", StringComparison.OrdinalIgnoreCase)) { return new WpfFontFamilyInfo(_arialFamily, familyInfo.Weight, familyInfo.Style, familyInfo.Stretch); } else if (String.Equals(fontName, "Helvetica-Bold", StringComparison.OrdinalIgnoreCase)) { return new WpfFontFamilyInfo(_arialFamily, FontWeights.Bold, familyInfo.Style, familyInfo.Stretch); } else if (String.Equals(fontName, "Helvetica-Oblique", StringComparison.OrdinalIgnoreCase)) { return new WpfFontFamilyInfo(_arialFamily, familyInfo.Weight, FontStyles.Italic, familyInfo.Stretch); } else if (String.Equals(fontName, "Helvetica-BoldOblique", StringComparison.OrdinalIgnoreCase)) { return new WpfFontFamilyInfo(_arialFamily, FontWeights.Bold, FontStyles.Italic, familyInfo.Stretch); } return new WpfFontFamilyInfo(_arialFamily, familyInfo.Weight, familyInfo.Style, familyInfo.Stretch); } else if (fontName.StartsWith("TimesNewRomanPS", StringComparison.OrdinalIgnoreCase)) { if (String.Equals(fontName, "TimesNewRomanPSMT", StringComparison.OrdinalIgnoreCase)) { return new WpfFontFamilyInfo(_arialFamily, familyInfo.Weight, familyInfo.Style, familyInfo.Stretch); } else if (String.Equals(fontName, "TimesNewRomanPS-BoldMT", StringComparison.OrdinalIgnoreCase)) { return new WpfFontFamilyInfo(_arialFamily, FontWeights.Bold, familyInfo.Style, familyInfo.Stretch); } else if (String.Equals(fontName, "TimesNewRomanPS-ItalicMT", StringComparison.OrdinalIgnoreCase)) { return new WpfFontFamilyInfo(_arialFamily, familyInfo.Weight, FontStyles.Italic, familyInfo.Stretch); } else if (String.Equals(fontName, "TimesNewRomanPS-BoldItalicMT", StringComparison.OrdinalIgnoreCase)) { return new WpfFontFamilyInfo(_arialFamily, FontWeights.Bold, FontStyles.Italic, familyInfo.Stretch); } return new WpfFontFamilyInfo(_arialFamily, familyInfo.Weight, familyInfo.Style, familyInfo.Stretch); } else if (fontName.StartsWith("CourierNewPS", StringComparison.OrdinalIgnoreCase)) { if (String.Equals(fontName, "CourierNewPSMT", StringComparison.OrdinalIgnoreCase)) { return new WpfFontFamilyInfo(_arialFamily, familyInfo.Weight, familyInfo.Style, familyInfo.Stretch); } else if (String.Equals(fontName, "CourierNewPS-BoldMT", StringComparison.OrdinalIgnoreCase)) { return new WpfFontFamilyInfo(_arialFamily, FontWeights.Bold, familyInfo.Style, familyInfo.Stretch); } else if (String.Equals(fontName, "CourierNewPS-ItalicMT", StringComparison.OrdinalIgnoreCase)) { return new WpfFontFamilyInfo(_arialFamily, familyInfo.Weight, FontStyles.Italic, familyInfo.Stretch); } else if (String.Equals(fontName, "CourierNewPS-BoldItalicMT", StringComparison.OrdinalIgnoreCase)) { return new WpfFontFamilyInfo(_arialFamily, FontWeights.Bold, FontStyles.Italic, familyInfo.Stretch); } return new WpfFontFamilyInfo(_arialFamily, familyInfo.Weight, familyInfo.Style, familyInfo.Stretch); } else if (fontName.Equals("MS-Gothic", StringComparison.OrdinalIgnoreCase)) { return new WpfFontFamilyInfo(new FontFamily("MS Gothic"), familyInfo.Weight, familyInfo.Style, familyInfo.Stretch); //return new WpfFontFamilyInfo(_arialFamily, familyInfo.Weight, // familyInfo.Style, familyInfo.Stretch); } else if (fontName.Equals("MS-PGothic", StringComparison.OrdinalIgnoreCase)) { return new WpfFontFamilyInfo(new FontFamily("MS PGothic"), familyInfo.Weight, familyInfo.Style, familyInfo.Stretch); } else if (fontName.Equals("MS Pゴシック", StringComparison.OrdinalIgnoreCase)) { return new WpfFontFamilyInfo(new FontFamily("MS PGothic"), familyInfo.Weight, familyInfo.Style, familyInfo.Stretch); } return null; //return new WpfFontFamilyInfo(_arialFamily, familyInfo.Weight, // familyInfo.Style, familyInfo.Stretch); }
public void RenderMarkerEx0(WpfDrawingRenderer renderer, WpfDrawingContext gr, SvgMarkerPosition markerPos, SvgStyleableElement refElement) { //ISharpMarkerHost markerHostElm = (ISharpMarkerHost)refElement; //SvgMarkerElement markerElm = (SvgMarkerElement)element; //SvgPointF[] vertexPositions = markerHostElm.MarkerPositions; //int start; //int len; //// Choose which part of the position array to use //switch (markerPos) //{ // case SvgMarkerPosition.Start: // start = 0; // len = 1; // break; // case SvgMarkerPosition.Mid: // start = 1; // len = vertexPositions.Length - 2; // break; // default: // // == MarkerPosition.End // start = vertexPositions.Length - 1; // len = 1; // break; //} //for (int i = start; i < start + len; i++) //{ // SvgPointF point = vertexPositions[i]; // GdiGraphicsContainer gc = gr.BeginContainer(); // gr.TranslateTransform(point.X, point.Y); // if (markerElm.OrientType.AnimVal.Equals(SvgMarkerOrient.Angle)) // { // gr.RotateTransform((float)markerElm.OrientAngle.AnimVal.Value); // } // else // { // float angle; // switch (markerPos) // { // case SvgMarkerPosition.Start: // angle = markerHostElm.GetStartAngle(i + 1); // break; // case SvgMarkerPosition.Mid: // //angle = (markerHostElm.GetEndAngle(i) + markerHostElm.GetStartAngle(i + 1)) / 2; // angle = SvgNumber.CalcAngleBisection(markerHostElm.GetEndAngle(i), markerHostElm.GetStartAngle(i + 1)); // break; // default: // angle = markerHostElm.GetEndAngle(i); // break; // } // gr.RotateTransform(angle); // } // if (markerElm.MarkerUnits.AnimVal.Equals(SvgMarkerUnit.StrokeWidth)) // { // SvgLength strokeWidthLength = new SvgLength(refElement, // "stroke-width", SvgLengthSource.Css, SvgLengthDirection.Viewport, "1"); // float strokeWidth = (float)strokeWidthLength.Value; // gr.ScaleTransform(strokeWidth, strokeWidth); // } // SvgPreserveAspectRatio spar = // (SvgPreserveAspectRatio)markerElm.PreserveAspectRatio.AnimVal; // float[] translateAndScale = spar.FitToViewBox((SvgRect)markerElm.ViewBox.AnimVal, // new SvgRect(0, 0, markerElm.MarkerWidth.AnimVal.Value, // markerElm.MarkerHeight.AnimVal.Value)); // gr.TranslateTransform(-(float)markerElm.RefX.AnimVal.Value * translateAndScale[2], // -(float)markerElm.RefY.AnimVal.Value * translateAndScale[3]); // gr.ScaleTransform(translateAndScale[2], translateAndScale[3]); // Clip(gr); // renderer.RenderChildren(markerElm); // gr.EndContainer(gc); //} }
public void RenderMarker(WpfDrawingRenderer renderer, WpfDrawingContext gr, SvgMarkerPosition markerPos, SvgStyleableElement refElement) { ISharpMarkerHost markerHostElm = (ISharpMarkerHost)refElement; SvgMarkerElement markerElm = (SvgMarkerElement)_svgElement; SvgPointF[] vertexPositions = markerHostElm.MarkerPositions; int start; int len; // Choose which part of the position array to use switch (markerPos) { case SvgMarkerPosition.Start: start = 0; len = 1; break; case SvgMarkerPosition.Mid: start = 1; len = vertexPositions.Length - 2; break; default: // == MarkerPosition.End start = vertexPositions.Length - 1; len = 1; break; } for (int i = start; i < start + len; i++) { SvgPointF point = vertexPositions[i]; //GdiGraphicsContainer gc = gr.BeginContainer(); this.BeforeRender(renderer); //Matrix matrix = Matrix.Identity; Matrix matrix = GetTransformMatrix(_svgElement); if (markerElm.OrientType.AnimVal.Equals(SvgMarkerOrient.Angle)) { matrix.Rotate(markerElm.OrientAngle.AnimVal.Value); } else { double angle = 0; switch (markerPos) { case SvgMarkerPosition.Start: angle = markerHostElm.GetStartAngle(i + 1); break; case SvgMarkerPosition.Mid: //angle = (markerHostElm.GetEndAngle(i) + markerHostElm.GetStartAngle(i + 1)) / 2; angle = SvgNumber.CalcAngleBisection(markerHostElm.GetEndAngle(i), markerHostElm.GetStartAngle(i + 1)); break; default: angle = markerHostElm.GetEndAngle(i); break; } matrix.Rotate(angle); } if (markerElm.MarkerUnits.AnimVal.Equals(SvgMarkerUnit.StrokeWidth)) { SvgLength strokeWidthLength = new SvgLength(refElement, "stroke-width", SvgLengthSource.Css, SvgLengthDirection.Viewport, "1"); double strokeWidth = strokeWidthLength.Value; matrix.Scale(strokeWidth, strokeWidth); } SvgPreserveAspectRatio spar = (SvgPreserveAspectRatio)markerElm.PreserveAspectRatio.AnimVal; double[] translateAndScale = spar.FitToViewBox((SvgRect)markerElm.ViewBox.AnimVal, new SvgRect(0, 0, markerElm.MarkerWidth.AnimVal.Value, markerElm.MarkerHeight.AnimVal.Value)); matrix.Translate(-markerElm.RefX.AnimVal.Value * translateAndScale[2], -markerElm.RefY.AnimVal.Value * translateAndScale[3]); matrix.Scale(translateAndScale[2], translateAndScale[3]); matrix.Translate(point.X, point.Y); _matrix = matrix; this.Render(renderer); //Clip(gr); renderer.RenderChildren(markerElm); //gr.EndContainer(gc); this.AfterRender(renderer); } }
public override void AfterRender(WpfDrawingRenderer renderer) { if (_horzRenderer != null) { _horzRenderer.Uninitialize(); _horzRenderer = null; } if (_vertRenderer != null) { _vertRenderer.Uninitialize(); _vertRenderer = null; } if (_pathRenderer != null) { _pathRenderer.Uninitialize(); _pathRenderer = null; } if (_drawContext != null) { _drawContext.Close(); _drawContext = null; } WpfDrawingContext context = renderer.Context; // TODO-PAUL: Testing this for validity... // Remove the GuidelineSet from the groups added by the FormattedText to reduced the // size of output XAML... if (_drawGroup != null) { ResetGuidelineSet(_drawGroup); } if (context.IncludeRuntime) { if (_drawGroup != null) { // Add the element/object type... SvgObject.SetType(_drawGroup, SvgObjectType.Text); // Add title for tooltips, if any... SvgTitleElement titleElement = _svgElement.SelectSingleNode("title") as SvgTitleElement; if (titleElement != null) { string titleValue = titleElement.InnerText; if (!String.IsNullOrEmpty(titleValue)) { SvgObject.SetTitle(_drawGroup, titleValue); } } } } if (!_isGroupAdded) { if (_drawGroup != null) { if (_isTextPath || _drawGroup.Transform != null || _drawGroup.ClipGeometry != null) { DrawingGroup curGroup = _context.Peek(); Debug.Assert(curGroup != null); if (curGroup != null) { curGroup.Children.Add(_drawGroup); } } else if (_drawGroup.Children.Count != 0) { DrawingGroup firstGroup = _drawGroup.Children[0] as DrawingGroup; if (firstGroup != null && firstGroup.Children.Count != 0) { //Drawing firstDrawing = firstGroup.Children[0]; DrawingGroup curGroup = _context.Peek(); Debug.Assert(curGroup != null); if (curGroup != null) { curGroup.Children.Add(_drawGroup); } } } } } else { if (_drawGroup != null) { DrawingGroup currentGroup = context.Peek(); if (currentGroup == null || currentGroup != _drawGroup) { throw new InvalidOperationException("An existing group is expected."); } context.Pop(); } } _context = null; _drawGroup = null; base.AfterRender(renderer); }
public virtual void Initialize(DrawingContext textContext, WpfDrawingContext drawContext) { if (textContext == null) { throw new ArgumentNullException("textContext", "The text context is required, and cannot be null (or Nothing)."); } if (drawContext == null) { throw new ArgumentNullException("drawContext", "The drawing context is required, and cannot be null (or Nothing)."); } _textContext = textContext; _drawContext = drawContext; }
private GeometryCollection CreateClippingRegion(SvgClipPathElement clipPath, WpfDrawingContext context) { GeometryCollection geomColl = new GeometryCollection(); foreach (XmlNode node in clipPath.ChildNodes) { if (node.NodeType != XmlNodeType.Element) { continue; } // Handle a case where the clip element has "use" element as a child... if (String.Equals(node.LocalName, "use")) { SvgUseElement useElement = (SvgUseElement)node; XmlElement refEl = useElement.ReferencedElement; if (refEl != null) { XmlElement refElParent = (XmlElement)refEl.ParentNode; useElement.OwnerDocument.Static = true; useElement.CopyToReferencedElement(refEl); refElParent.RemoveChild(refEl); useElement.AppendChild(refEl); foreach (XmlNode useChild in useElement.ChildNodes) { if (useChild.NodeType != XmlNodeType.Element) { continue; } SvgStyleableElement element = useChild as SvgStyleableElement; if (element != null && element.RenderingHint == SvgRenderingHint.Shape) { Geometry childPath = CreateGeometry(element, context.OptimizePath); if (childPath != null) { geomColl.Add(childPath); } } } useElement.RemoveChild(refEl); useElement.RestoreReferencedElement(refEl); refElParent.AppendChild(refEl); useElement.OwnerDocument.Static = false; } } else { SvgStyleableElement element = node as SvgStyleableElement; if (element != null) { if (element.RenderingHint == SvgRenderingHint.Shape) { Geometry childPath = CreateGeometry(element, context.OptimizePath); if (childPath != null) { geomColl.Add(childPath); } } else if (element.RenderingHint == SvgRenderingHint.Text) { GeometryCollection textGeomColl = GetTextClippingRegion(element, context); if (textGeomColl != null) { for (int i = 0; i < textGeomColl.Count; i++) { geomColl.Add(textGeomColl[i]); } } } } } } return geomColl; }
protected void SetTransform(WpfDrawingContext context) { _transformMatrix = null; ISvgTransformable transElm = _svgElement as ISvgTransformable; if (transElm != null) { SvgTransformList svgTList = (SvgTransformList)transElm.Transform.AnimVal; SvgMatrix svgMatrix = ((SvgTransformList)transElm.Transform.AnimVal).TotalMatrix; if (svgMatrix.IsIdentity) { return; } _transformMatrix = new MatrixTransform(svgMatrix.A, svgMatrix.B, svgMatrix.C, svgMatrix.D, svgMatrix.E, svgMatrix.F); } }
protected void SetQuality(WpfDrawingContext context) { //Graphics graphics = gr.Graphics; //string colorRendering = _svgElement.GetComputedStringValue("color-rendering", String.Empty); //switch (colorRendering) //{ // case "optimizeSpeed": // graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighSpeed; // break; // case "optimizeQuality": // graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; // break; // default: // // "auto" // // todo: could use AssumeLinear for slightly better // graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.Default; // break; //} //if (element is SvgTextContentElement) //{ // // Unfortunately the text rendering hints are not applied because the // // text path is recorded and painted to the Graphics object as a path // // not as text. // string textRendering = _svgElement.GetComputedStringValue("text-rendering", String.Empty); // switch (textRendering) // { // case "optimizeSpeed": // graphics.SmoothingMode = SmoothingMode.HighSpeed; // //graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixel; // break; // case "optimizeLegibility": // graphics.SmoothingMode = SmoothingMode.HighQuality; // //graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit; // break; // case "geometricPrecision": // graphics.SmoothingMode = SmoothingMode.AntiAlias; // //graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias; // break; // default: // // "auto" // graphics.SmoothingMode = SmoothingMode.AntiAlias; // //graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SystemDefault; // break; // } //} //else //{ // string shapeRendering = _svgElement.GetComputedStringValue("shape-rendering", String.Empty); // switch (shapeRendering) // { // case "optimizeSpeed": // graphics.SmoothingMode = SmoothingMode.HighSpeed; // break; // case "crispEdges": // graphics.SmoothingMode = SmoothingMode.None; // break; // case "geometricPrecision": // graphics.SmoothingMode = SmoothingMode.HighQuality; // break; // default: // // "auto" // graphics.SmoothingMode = SmoothingMode.AntiAlias; // break; // } //} }
private void AggregateChildren(SvgAElement aElement, WpfDrawingContext context, float opacity) { _isAggregated = false; if (aElement == null || aElement.ChildNodes == null) { return; } string aggregatedFill = aElement.GetAttribute("fill"); bool isFillFound = !String.IsNullOrEmpty(aggregatedFill); SvgStyleableElement paintElement = null; if (isFillFound) { paintElement = aElement; } XmlNode targetNode = aElement; // Check if the children of the link are wrapped in a Group Element... if (aElement.ChildNodes.Count == 1) { SvgGElement groupElement = aElement.ChildNodes[0] as SvgGElement; if (groupElement != null) { targetNode = groupElement; } } WpfDrawingSettings settings = context.Settings; GeometryCollection geomColl = new GeometryCollection(); foreach (XmlNode node in targetNode.ChildNodes) { if (node.NodeType != XmlNodeType.Element) { continue; } // Handle a case where the clip element has "use" element as a child... if (String.Equals(node.LocalName, "use")) { SvgUseElement useElement = (SvgUseElement)node; XmlElement refEl = useElement.ReferencedElement; if (refEl != null) { XmlElement refElParent = (XmlElement)refEl.ParentNode; useElement.OwnerDocument.Static = true; useElement.CopyToReferencedElement(refEl); refElParent.RemoveChild(refEl); useElement.AppendChild(refEl); foreach (XmlNode useChild in useElement.ChildNodes) { if (useChild.NodeType != XmlNodeType.Element) { continue; } SvgStyleableElement element = useChild as SvgStyleableElement; if (element != null && element.RenderingHint == SvgRenderingHint.Shape) { Geometry childPath = WpfRendering.CreateGeometry(element, settings.OptimizePath); if (childPath != null) { if (isFillFound) { string elementFill = element.GetAttribute("fill"); if (!String.IsNullOrEmpty(elementFill) && !String.Equals(elementFill, aggregatedFill, StringComparison.OrdinalIgnoreCase)) { return; } } else { aggregatedFill = element.GetAttribute("fill"); isFillFound = !String.IsNullOrEmpty(aggregatedFill); if (isFillFound) { paintElement = element; } } geomColl.Add(childPath); } } } useElement.RemoveChild(refEl); useElement.RestoreReferencedElement(refEl); refElParent.AppendChild(refEl); useElement.OwnerDocument.Static = false; } } //else if (String.Equals(node.LocalName, "g")) //{ //} else { SvgStyleableElement element = node as SvgStyleableElement; if (element != null && element.RenderingHint == SvgRenderingHint.Shape) { Geometry childPath = WpfRendering.CreateGeometry(element, settings.OptimizePath); if (childPath != null) { if (isFillFound) { string elementFill = element.GetAttribute("fill"); if (!String.IsNullOrEmpty(elementFill) && !String.Equals(elementFill, aggregatedFill, StringComparison.OrdinalIgnoreCase)) { return; } } else { aggregatedFill = element.GetAttribute("fill"); isFillFound = !String.IsNullOrEmpty(aggregatedFill); if (isFillFound) { paintElement = element; } } geomColl.Add(childPath); } } } } if (geomColl.Count == 0 || paintElement == null) { return; } _aggregatedFill = paintElement; _aggregatedGeom = geomColl; _isAggregated = true; }
public override void Visit(DrawingGroup group, SvgAElement element, WpfDrawingContext context, float opacity) { _isAggregated = false; if (group == null || element == null || context == null) { return; } AddExtraLinkInformation(group, element); //string linkId = element.GetAttribute("id"); string linkId = GetElementName(element); if (String.IsNullOrEmpty(linkId)) { return; } SvgLink.SetKey(group, linkId); if (_dicLinks.ContainsKey(linkId)) { _isAggregated = _dicLinks[linkId]; return; } string linkAction = element.GetAttribute("onclick"); if (String.IsNullOrEmpty(linkAction)) { linkAction = element.GetAttribute("onmouseover"); if (!String.IsNullOrEmpty(linkAction) && linkAction.StartsWith("parent.svgMouseOverName", StringComparison.OrdinalIgnoreCase)) { SvgLink.SetAction(group, SvgLinkAction.LinkTooltip); } else { SvgLink.SetAction(group, SvgLinkAction.LinkNone); } } else { if (linkAction.StartsWith("parent.svgClick", StringComparison.OrdinalIgnoreCase)) { SvgLink.SetAction(group, SvgLinkAction.LinkPage); } else if (linkAction.StartsWith("parent.svgOpenHtml", StringComparison.OrdinalIgnoreCase)) { SvgLink.SetAction(group, SvgLinkAction.LinkHtml); } else { SvgLink.SetAction(group, SvgLinkAction.LinkNone); } } if (!String.IsNullOrEmpty(linkAction)) { if (linkAction.IndexOf("'Top'") > 0) { SvgLink.SetLocation(group, "Top"); } else if (linkAction.IndexOf("'Bottom'") > 0) { SvgLink.SetLocation(group, "Bottom"); } else { SvgLink.SetLocation(group, "Top"); } } this.AggregateChildren(element, context, opacity); if (_isAggregated) { Geometry drawGeometry = null; if (_aggregatedGeom.Count == 1) { drawGeometry = _aggregatedGeom[0]; } else { GeometryGroup geomGroup = new GeometryGroup(); geomGroup.FillRule = FillRule.Nonzero; for (int i = 0; i < _aggregatedGeom.Count; i++) { geomGroup.Children.Add(_aggregatedGeom[i]); } drawGeometry = geomGroup; } WpfSvgPaint fillPaint = new WpfSvgPaint(context, _aggregatedFill, "fill"); Brush brush = fillPaint.GetBrush(false); brush.SetValue(FrameworkElement.NameProperty, linkId + "_Brush"); GeometryDrawing drawing = new GeometryDrawing(brush, null, drawGeometry); group.Children.Add(drawing); } _dicLinks.Add(linkId, _isAggregated); }
public override void Initialize(DrawingGroup linkGroup, WpfDrawingContext context) { if (linkGroup != null) { SvgLink.SetKey(linkGroup, SvgObject.LinksLayer); } }
public void RenderMask(ISvgElement node, WpfDrawingContext context) { if (context == null) { _renderingContext = new WpfDrawingContext(true, _renderingSettings); _renderingContext.Initialize(null, _fontFamilyVisitor, _imageVisitor); } else { _renderingContext = context; } _renderingContext.BeginDrawing(); _svgRenderer.RenderMask(node); _renderingContext.EndDrawing(); }
private GeometryCollection GetTextClippingRegion(SvgStyleableElement element, WpfDrawingContext context) { GeometryCollection geomColl = new GeometryCollection(); WpfDrawingRenderer renderer = new WpfDrawingRenderer(); renderer.Window = _svgElement.OwnerDocument.Window as SvgWindow; WpfDrawingSettings settings = context.Settings.Clone(); settings.TextAsGeometry = true; WpfDrawingContext clipContext = new WpfDrawingContext(true, settings); clipContext.RenderingClipRegion = true; clipContext.Initialize(null, context.FontFamilyVisitor, null); renderer.Render(element, clipContext); DrawingGroup rootGroup = renderer.Drawing as DrawingGroup; if (rootGroup != null && rootGroup.Children.Count == 1) { DrawingGroup textGroup = rootGroup.Children[0] as DrawingGroup; if (textGroup != null) { ExtractGeometry(textGroup, geomColl); } } return geomColl; }
public virtual void Uninitialize() { _textContext = null; _drawContext = null; }
protected static void RenderMarkers(WpfDrawingRenderer renderer, SvgStyleableElement styleElm, WpfDrawingContext gr) { // OPTIMIZE if (styleElm is ISharpMarkerHost) { string markerStartUrl = ExtractMarkerUrl(styleElm.GetPropertyValue("marker-start", "marker")); string markerMiddleUrl = ExtractMarkerUrl(styleElm.GetPropertyValue("marker-mid", "marker")); string markerEndUrl = ExtractMarkerUrl(styleElm.GetPropertyValue("marker-end", "marker")); if (markerStartUrl.Length > 0) { WpfMarkerRendering markerRenderer = CreateByUri(styleElm.OwnerDocument, styleElm.BaseURI, markerStartUrl) as WpfMarkerRendering; if (markerRenderer != null) { markerRenderer.RenderMarker(renderer, gr, SvgMarkerPosition.Start, styleElm); } } if (markerMiddleUrl.Length > 0) { // TODO markerMiddleUrl != markerStartUrl WpfMarkerRendering markerRenderer = CreateByUri(styleElm.OwnerDocument, styleElm.BaseURI, markerMiddleUrl) as WpfMarkerRendering; if (markerRenderer != null) { markerRenderer.RenderMarker(renderer, gr, SvgMarkerPosition.Mid, styleElm); } } if (markerEndUrl.Length > 0) { // TODO: markerEndUrl != markerMiddleUrl WpfMarkerRendering markerRenderer = CreateByUri(styleElm.OwnerDocument, styleElm.BaseURI, markerEndUrl) as WpfMarkerRendering; if (markerRenderer != null) { markerRenderer.RenderMarker(renderer, gr, SvgMarkerPosition.End, styleElm); } } } }
public override void BeforeRender(WpfDrawingRenderer renderer) { _isTextPath = false; _isGroupAdded = false; _textWidth = 0; _isMeasuring = false; WpfDrawingContext context = renderer.Context; SvgRenderingHint hint = _svgElement.RenderingHint; if (hint == SvgRenderingHint.Clipping) { return; } // We do not directly render the contents of the clip-path, unless specifically requested... if (String.Equals(_svgElement.ParentNode.LocalName, "clipPath") && !context.RenderingClipRegion) { return; } string sVisibility = _textElement.GetPropertyValue("visibility"); string sDisplay = _textElement.GetPropertyValue("display"); if (String.Equals(sVisibility, "hidden") || String.Equals(sDisplay, "none")) { return; } _context = renderer.Context; SetQuality(context); SetTransform(context); SetClip(_context); SetMask(_context); _drawGroup = new DrawingGroup(); string elementId = this.GetElementName(); if (!String.IsNullOrEmpty(elementId) && !context.IsRegisteredId(elementId)) { _drawGroup.SetValue(FrameworkElement.NameProperty, elementId); context.RegisterId(elementId); if (context.IncludeRuntime) { SvgObject.SetId(_drawGroup, elementId); } } Transform textTransform = this.Transform; if (textTransform != null && !textTransform.Value.IsIdentity) { _drawGroup.Transform = textTransform; } else { textTransform = null; // render any identity transform useless... } Geometry textClip = this.ClipGeometry; if (textClip != null && !textClip.IsEmpty()) { _drawGroup.ClipGeometry = textClip; } else { textClip = null; // render any empty geometry useless... } Brush textMask = this.Masking; if (textMask != null) { _drawGroup.OpacityMask = textMask; } if (textTransform != null || textClip != null || textMask != null) { DrawingGroup curGroup = _context.Peek(); Debug.Assert(curGroup != null); if (curGroup != null) { curGroup.Children.Add(_drawGroup); context.Push(_drawGroup); _isGroupAdded = true; } } _drawContext = _drawGroup.Open(); _horzRenderer.Initialize(_drawContext, _context); _vertRenderer.Initialize(_drawContext, _context); _pathRenderer.Initialize(_drawContext, _context); }
protected void FitToViewbox(WpfDrawingContext context, SvgElement svgElement, Rect elementBounds) { if (svgElement == null) { return; } ISvgFitToViewBox fitToView = svgElement as ISvgFitToViewBox; if (fitToView == null) { return; } SvgPreserveAspectRatio spar = (SvgPreserveAspectRatio)fitToView.PreserveAspectRatio.AnimVal; double[] transformArray = spar.FitToViewBox((SvgRect)fitToView.ViewBox.AnimVal, new SvgRect(elementBounds.X, elementBounds.Y, elementBounds.Width, elementBounds.Height)); double translateX = transformArray[0]; double translateY = transformArray[1]; double scaleX = transformArray[2]; double scaleY = transformArray[3]; Transform translateMatrix = null; Transform scaleMatrix = null; //if (translateX != 0 || translateY != 0) { translateMatrix = new TranslateTransform(translateX, translateY); } if ((float)scaleX != 1.0f && (float)scaleY != 1.0f) { scaleMatrix = new ScaleTransform(scaleX, scaleY); } if (translateMatrix != null && scaleMatrix != null) { // Create a TransformGroup to contain the transforms // and add the transforms to it. TransformGroup transformGroup = new TransformGroup(); transformGroup.Children.Add(scaleMatrix); transformGroup.Children.Add(translateMatrix); this.Transform = transformGroup; } else if (translateMatrix != null) { this.Transform = translateMatrix; } else if (scaleMatrix != null) { this.Transform = scaleMatrix; } }
public abstract BitmapSource Visit(SvgImageElement element, WpfDrawingContext context);
protected void SetClip(WpfDrawingContext context) { _clipPathUnits = SvgUnitType.UserSpaceOnUse; if (_svgElement == null) { return; } #region Clip with clip // see http://www.w3.org/TR/SVG/masking.html#OverflowAndClipProperties if (_svgElement is ISvgSvgElement || _svgElement is ISvgMarkerElement || _svgElement is ISvgSymbolElement || _svgElement is ISvgPatternElement) { // check overflow property CssValue overflow = _svgElement.GetComputedCssValue("overflow", String.Empty) as CssValue; // TODO: clip can have "rect(10 10 auto 10)" CssPrimitiveValue clip = _svgElement.GetComputedCssValue("clip", String.Empty) as CssPrimitiveValue; string sOverflow = null; if (overflow != null || overflow.CssText == "") { sOverflow = overflow.CssText; } else { if (this is ISvgSvgElement) sOverflow = "hidden"; } if (sOverflow != null) { // "If the 'overflow' property has a value other than hidden or scroll, the property has no effect (i.e., a clipping rectangle is not created)." if (sOverflow == "hidden" || sOverflow == "scroll") { Rect clipRect = Rect.Empty; if (clip != null && clip.PrimitiveType == CssPrimitiveType.Rect) { if (_svgElement is ISvgSvgElement) { ISvgSvgElement svgElement = (ISvgSvgElement)_svgElement; SvgRect viewPort = svgElement.Viewport as SvgRect; clipRect = WpfConvert.ToRect(viewPort); ICssRect clipShape = (CssRect)clip.GetRectValue(); if (clipShape.Top.PrimitiveType != CssPrimitiveType.Ident) clipRect.Y += clipShape.Top.GetFloatValue(CssPrimitiveType.Number); if (clipShape.Left.PrimitiveType != CssPrimitiveType.Ident) clipRect.X += clipShape.Left.GetFloatValue(CssPrimitiveType.Number); if (clipShape.Right.PrimitiveType != CssPrimitiveType.Ident) clipRect.Width = (clipRect.Right - clipRect.X) - clipShape.Right.GetFloatValue(CssPrimitiveType.Number); if (clipShape.Bottom.PrimitiveType != CssPrimitiveType.Ident) clipRect.Height = (clipRect.Bottom - clipRect.Y) - clipShape.Bottom.GetFloatValue(CssPrimitiveType.Number); } } else if (clip == null || (clip.PrimitiveType == CssPrimitiveType.Ident && clip.GetStringValue() == "auto")) { if (_svgElement is ISvgSvgElement) { ISvgSvgElement svgElement = (ISvgSvgElement)_svgElement; SvgRect viewPort = svgElement.Viewport as SvgRect; clipRect = WpfConvert.ToRect(viewPort); } else if (_svgElement is ISvgMarkerElement || _svgElement is ISvgSymbolElement || _svgElement is ISvgPatternElement) { // TODO: what to do here? } } if (clipRect != Rect.Empty) { _clipGeometry = new RectangleGeometry(clipRect); //gr.SetClip(clipRect); } } } } #endregion #region Clip with clip-path SvgRenderingHint hint = _svgElement.RenderingHint; if (hint == SvgRenderingHint.Image) { } // see: http://www.w3.org/TR/SVG/masking.html#EstablishingANewClippingPath if (hint == SvgRenderingHint.Shape || hint == SvgRenderingHint.Text || hint == SvgRenderingHint.Clipping || hint == SvgRenderingHint.Masking || hint == SvgRenderingHint.Containment || hint == SvgRenderingHint.Image) { CssPrimitiveValue clipPath = _svgElement.GetComputedCssValue("clip-path", String.Empty) as CssPrimitiveValue; if (clipPath != null && clipPath.PrimitiveType == CssPrimitiveType.Uri) { string absoluteUri = _svgElement.ResolveUri(clipPath.GetStringValue()); SvgClipPathElement eClipPath = _svgElement.OwnerDocument.GetNodeByUri(absoluteUri) as SvgClipPathElement; if (eClipPath != null) { GeometryCollection geomColl = CreateClippingRegion(eClipPath, context); if (geomColl == null || geomColl.Count == 0) { return; } Geometry gpClip = geomColl[0]; int geomCount = geomColl.Count; if (geomCount > 1) { //GeometryGroup clipGroup = new GeometryGroup(); //clipGroup.Children.Add(gpClip); for (int k = 1; k < geomCount; k++) { gpClip = CombinedGeometry.Combine(gpClip, geomColl[k], GeometryCombineMode.Union, null); //clipGroup.Children.Add(geomColl[k]); } //clipGroup.Children.Reverse(); //gpClip = clipGroup; } if (gpClip == null || gpClip.IsEmpty()) { return; } _clipPathUnits = (SvgUnitType)eClipPath.ClipPathUnits.AnimVal; //if (_clipPathUnits == SvgUnitType.ObjectBoundingBox) //{ // SvgTransformableElement transElement = _svgElement as SvgTransformableElement; // if (transElement != null) // { // ISvgRect bbox = transElement.GetBBox(); // // scale clipping path // gpClip.Transform = new ScaleTransform(bbox.Width, bbox.Height); // //gr.SetClip(gpClip); // // offset clip // //TODO--PAUL gr.TranslateClip((float)bbox.X, (float)bbox.Y); // _clipGeometry = gpClip; // } // else // { // throw new NotImplementedException("clip-path with SvgUnitType.ObjectBoundingBox " // + "not supported for this type of element: " + _svgElement.GetType()); // } //} //else { //gr.SetClip(gpClip); _clipGeometry = gpClip; } } } } #endregion }
public void RenderMarker0(WpfDrawingRenderer renderer, WpfDrawingContext gr, SvgMarkerPosition markerPos, SvgStyleableElement refElement) { //PathGeometry g; //g.GetPointAtFractionLength( ISharpMarkerHost markerHostElm = (ISharpMarkerHost)refElement; SvgMarkerElement markerElm = (SvgMarkerElement)_svgElement; SvgPointF[] vertexPositions = markerHostElm.MarkerPositions; int start; int len; // Choose which part of the position array to use switch (markerPos) { case SvgMarkerPosition.Start: start = 0; len = 1; break; case SvgMarkerPosition.Mid: start = 1; len = vertexPositions.Length - 2; break; default: // == MarkerPosition.End start = vertexPositions.Length - 1; len = 1; break; } for (int i = start; i < start + len; i++) { SvgPointF point = vertexPositions[i]; Matrix m = GetTransformMatrix(_svgElement); //GraphicsContainer gc = gr.BeginContainer(); this.BeforeRender(renderer); //gr.TranslateTransform(point.X, point.Y); //PAUL: //m.Translate(point.X, point.Y); if (markerElm.OrientType.AnimVal.Equals(SvgMarkerOrient.Angle)) { m.Rotate(markerElm.OrientAngle.AnimVal.Value); //gr.RotateTransform((double)markerElm.OrientAngle.AnimVal.Value); } else { double angle; switch (markerPos) { case SvgMarkerPosition.Start: angle = markerHostElm.GetStartAngle(i + 1); break; case SvgMarkerPosition.Mid: //angle = (markerHostElm.GetEndAngle(i) + markerHostElm.GetStartAngle(i + 1)) / 2; angle = SvgNumber.CalcAngleBisection(markerHostElm.GetEndAngle(i), markerHostElm.GetStartAngle(i + 1)); break; default: angle = markerHostElm.GetEndAngle(i); break; } //gr.RotateTransform(angle); m.Rotate(angle); } if (markerElm.MarkerUnits.AnimVal.Equals(SvgMarkerUnit.StrokeWidth)) { string propValue = refElement.GetPropertyValue("stroke-width"); if (propValue.Length == 0) propValue = "1"; SvgLength strokeWidthLength = new SvgLength("stroke-width", propValue, refElement, SvgLengthDirection.Viewport); double strokeWidth = strokeWidthLength.Value; //gr.ScaleTransform(strokeWidth, strokeWidth); m.Scale(strokeWidth, strokeWidth); } SvgPreserveAspectRatio spar = (SvgPreserveAspectRatio)markerElm.PreserveAspectRatio.AnimVal; double[] translateAndScale = spar.FitToViewBox( (SvgRect)markerElm.ViewBox.AnimVal, new SvgRect(0, 0, markerElm.MarkerWidth.AnimVal.Value, markerElm.MarkerHeight.AnimVal.Value)); //PAUL: //m.Translate(-(double)markerElm.RefX.AnimVal.Value * translateAndScale[2], -(double)markerElm.RefY.AnimVal.Value * translateAndScale[3]); //PAUL: m.Scale(translateAndScale[2], translateAndScale[3]); m.Translate(point.X, point.Y); //Matrix oldTransform = TransformMatrix; //TransformMatrix = m; //try //{ //newTransform.Append(m); //TransformGroup tg = new TransformGroup(); //renderer.Canvas.re //gr.TranslateTransform( // -(double)markerElm.RefX.AnimVal.Value * translateAndScale[2], // -(double)markerElm.RefY.AnimVal.Value * translateAndScale[3] // ); //gr.ScaleTransform(translateAndScale[2], translateAndScale[3]); renderer.RenderChildren(markerElm); // markerElm.RenderChildren(renderer); //} //finally //{ // TransformMatrix = oldTransform; //} // //gr.EndContainer(gc); _matrix = m; this.Render(renderer); //gr.EndContainer(gc); this.AfterRender(renderer); } }
protected void SetMask(WpfDrawingContext context) { _maskUnits = SvgUnitType.UserSpaceOnUse; _maskContentUnits = SvgUnitType.UserSpaceOnUse; CssPrimitiveValue maskPath = _svgElement.GetComputedCssValue( "mask", String.Empty) as CssPrimitiveValue; if (maskPath != null && maskPath.PrimitiveType == CssPrimitiveType.Uri) { string absoluteUri = _svgElement.ResolveUri(maskPath.GetStringValue()); SvgMaskElement maskElement = _svgElement.OwnerDocument.GetNodeByUri(absoluteUri) as SvgMaskElement; if (maskElement != null) { WpfDrawingRenderer renderer = new WpfDrawingRenderer(); renderer.Window = _svgElement.OwnerDocument.Window as SvgWindow; WpfDrawingSettings settings = context.Settings.Clone(); settings.TextAsGeometry = true; WpfDrawingContext maskContext = new WpfDrawingContext(true, settings); //maskContext.Initialize(null, context.FontFamilyVisitor, null); maskContext.Initialize(context.LinkVisitor, context.FontFamilyVisitor, context.ImageVisitor); renderer.RenderMask(maskElement, maskContext); Drawing image = renderer.Drawing; Rect bounds = new Rect(0, 0, 1, 1); //Rect destRect = GetMaskDestRect(maskElement, bounds); //destRect = bounds; //DrawingImage drawImage = new DrawingImage(image); //DrawingVisual drawingVisual = new DrawingVisual(); //DrawingContext drawingContext = drawingVisual.RenderOpen(); //drawingContext.DrawDrawing(image); //drawingContext.Close(); //RenderTargetBitmap drawImage = new RenderTargetBitmap((int)200, // (int)200, 96, 96, PixelFormats.Pbgra32); //drawImage.Render(drawingVisual); //ImageBrush imageBrush = new ImageBrush(drawImage); //imageBrush.Viewbox = image.Bounds; //imageBrush.Viewport = image.Bounds; //imageBrush.ViewboxUnits = BrushMappingMode.Absolute; //imageBrush.ViewportUnits = BrushMappingMode.Absolute; //imageBrush.TileMode = TileMode.None; //imageBrush.Stretch = Stretch.None; //this.Masking = imageBrush; DrawingBrush maskBrush = new DrawingBrush(image); //tb.Viewbox = new Rect(0, 0, destRect.Width, destRect.Height); //tb.Viewport = new Rect(0, 0, destRect.Width, destRect.Height); maskBrush.Viewbox = image.Bounds; maskBrush.Viewport = image.Bounds; maskBrush.ViewboxUnits = BrushMappingMode.Absolute; maskBrush.ViewportUnits = BrushMappingMode.Absolute; maskBrush.TileMode = TileMode.None; maskBrush.Stretch = Stretch.Uniform; ////maskBrush.AlignmentX = AlignmentX.Center; ////maskBrush.AlignmentY = AlignmentY.Center; this.Masking = maskBrush; _maskUnits = (SvgUnitType)maskElement.MaskUnits.AnimVal; _maskContentUnits = (SvgUnitType)maskElement.MaskContentUnits.AnimVal; } } }
private Drawing GetImage(WpfDrawingContext context) { WpfDrawingRenderer renderer = new WpfDrawingRenderer(); renderer.Window = _patternElement.OwnerDocument.Window as SvgWindow; WpfDrawingSettings settings = context.Settings.Clone(); settings.TextAsGeometry = true; WpfDrawingContext patternContext = new WpfDrawingContext(true, settings); patternContext.Initialize(null, context.FontFamilyVisitor, null); SvgSvgElement elm = MoveIntoSvgElement(); renderer.Render((SvgElement)elm, patternContext); Drawing img = renderer.Drawing; MoveOutOfSvgElement(elm); return img; }
public void Render(ISvgElement node, WpfDrawingContext context) { //throw new NotImplementedException(); //SvgRectF updatedRect; //if (invalidRect != SvgRectF.Empty) // updatedRect = new SvgRectF(invalidRect.X, invalidRect.Y, // invalidRect.Width, invalidRect.Height); //else // updatedRect = SvgRectF.Empty; //RendererBeforeRender(); //if (graphics != null && graphics.Graphics != null) //{ // _svgRenderer.Render(node); //} //RendererAfterRender(); //if (onRender != null) // OnRender(updatedRect); if (context == null) { _renderingContext = new WpfDrawingContext(true, _renderingSettings); _renderingContext.Initialize(null, _fontFamilyVisitor, _imageVisitor); } else { _renderingContext = context; } _renderingContext.BeginDrawing(); _svgRenderer.Render(node); _renderingContext.EndDrawing(); }
public abstract Brush GetBrush(Rect elementBounds, WpfDrawingContext context);
public void Render(ISvgDocument node) { //SvgRectF updatedRect; //if (invalidRect != SvgRectF.Empty) // updatedRect = new SvgRectF(invalidRect.X, invalidRect.Y, // invalidRect.Width, invalidRect.Height); //else // updatedRect = SvgRectF.Empty; //RendererBeforeRender(); //_renderingContext = new WpfDrawingContext(new DrawingGroup()); _renderingContext = new WpfDrawingContext(false, _renderingSettings); _renderingContext.Initialize(_linkVisitor, _fontFamilyVisitor, _imageVisitor); _renderingContext.BeginDrawing(); _svgRenderer.Render(node); _renderingContext.EndDrawing(); //RendererAfterRender(); //if (onRender != null) // OnRender(updatedRect); }
public abstract WpfFontFamilyInfo Visit(string fontName, WpfFontFamilyInfo familyInfo, WpfDrawingContext context);
private ImageSource GetImage(SvgImageElement element, WpfDrawingContext context) { bool isSavingImages = _saveImages; string imagesDir = _saveDirectory; if (context != null && context.Settings.IncludeRuntime == false) { isSavingImages = true; if (string.IsNullOrWhiteSpace(_saveDirectory) || Directory.Exists(_saveDirectory) == false) { var assembly = Assembly.GetExecutingAssembly(); if (assembly != null) { imagesDir = Path.GetDirectoryName(assembly.Location); } } } var comparer = StringComparison.OrdinalIgnoreCase; string sURI = element.Href.AnimVal.Replace(" ", ""); int nColon = sURI.IndexOf(":", comparer); int nSemiColon = sURI.IndexOf(";", comparer); int nComma = sURI.IndexOf(",", comparer); string sMimeType = sURI.Substring(nColon + 1, nSemiColon - nColon - 1); string sContent = SvgObject.RemoveWhitespace(sURI.Substring(nComma + 1)); byte[] imageBytes = Convert.FromBase64CharArray(sContent.ToCharArray(), 0, sContent.Length); if (string.Equals(sMimeType, "image/svg+xml", comparer)) { using (var stream = new MemoryStream(imageBytes)) using (var reader = new FileSvgReader(context.Settings)) return(new DrawingImage(reader.Read(stream))); } BitmapImage imageSource = new BitmapImage(); imageSource.BeginInit(); imageSource.CreateOptions = BitmapCreateOptions.PreservePixelFormat; imageSource.StreamSource = new MemoryStream(imageBytes); imageSource.EndInit(); imageSource.Freeze(); if (isSavingImages && !string.IsNullOrWhiteSpace(imagesDir) && Directory.Exists(imagesDir)) { var imagePath = this.GetImagePath(imagesDir); BitmapEncoder encoder = new PngBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(imageSource)); using (var fileStream = new FileStream(imagePath, FileMode.Create)) { encoder.Save(fileStream); } BitmapImage savedSource = new BitmapImage(); savedSource.BeginInit(); savedSource.CacheOption = BitmapCacheOption.OnLoad; savedSource.CreateOptions = BitmapCreateOptions.PreservePixelFormat; savedSource.UriSource = new Uri(imagePath); savedSource.EndInit(); savedSource.Freeze(); return(savedSource); } return(imageSource); }