public bool LoadImage(byte[] svg) { logger.Debug("Загружаем svg в помошник."); try { XmlSvg = new XmlDocument(); XmlSvg.LoadXml(System.Text.Encoding.UTF8.GetString(svg)); foreach (XmlNode node in XmlSvg.GetElementsByTagName("svg")) { string units; SvgHeight = ParseSize(node.Attributes["height"].Value, out units); if(units == "mm") SvgHeight *= 100; SvgWidht = ParseSize(node.Attributes["width"].Value, out units); if(units == "mm") SvgWidht *= 100; break; } foreach (XmlNode node in XmlSvg.GetElementsByTagName("rect")) { if(node.Attributes["id"].Value == "framework") { BaseX = XmlConvert.ToDouble(node.Attributes["x"].Value); BaseY = XmlConvert.ToDouble(node.Attributes["y"].Value); BaseWidht = XmlConvert.ToDouble(node.Attributes["width"].Value); BaseHeight = XmlConvert.ToDouble(node.Attributes["height"].Value); CentreX = BaseX + BaseWidht / 2; CentreY = BaseY + BaseHeight / 2; FrameSet = true; break; } } if(FrameSet) { OriginalFile = svg; DrawingSvg = null; } } catch (Exception ex) { logger.Error(ex, "Ошибка в чтении svg!"); return false; } logger.Debug("Закончили загрузку."); return FrameSet; }
public void ModifyDrawingImage() { if (OriginalFile == null) return; AddX = (float)(BaseWidht / 2) * (CubesH - 1); AddY = (float)(BaseHeight / 2) * (CubesV - 1); if(CubesV == 1 && CubesH == 1) { DrawingSvg = new Handle(OriginalFile); return; } logger.Debug("Начала изменение svg"); MemoryStream stream = new MemoryStream(OriginalFile); SvgDocument imagefile = SvgDocument.Open<SvgDocument>(stream); SvgClipPath clip = imagefile.GetElementById<SvgClipPath>("presentation_clip_path"); if(clip != null) clip.Parent.Children.Remove(clip); foreach(SvgLine line in imagefile.Children.FindSvgElementsOf<SvgLine>()) { line.StartX = FixX(line.StartX); line.EndX = FixX(line.EndX); line.StartY = FixY(line.StartY); line.EndY = FixY(line.EndY); } stream = new MemoryStream(); imagefile.Write(stream); DrawingSvg = new Handle(stream.ToArray()); //logger.Debug(System.Text.Encoding.Default.GetString(DrawingFile)); logger.Debug("Закончили изменение svg."); }
public static Gdk.Pixbuf LoadFromStream (System.IO.Stream input) { Handle loader = new Handle (); byte [] buffer = new byte [8192]; int n; while ((n = input.Read (buffer, 0, 8192)) != 0) loader.Write (buffer, (uint) n); loader.Close (); return loader.Pixbuf; }
/// <summary> /// Changes the color of the first occuring style attribute in the SVG-image /// </summary> /// <param name="color"> /// A <see cref="Gdk.Color"/> /// </param> public void Colorize(Gdk.Color color, double opacity) { if(svgContent == null) return; if(this.colorizeItems == null) return; // make sure the color-values stay <= 255 string hexcolor = String.Format("#{0:x2}{1:x2}{2:x2}", color.Red / 255 > 255 ? 255 : color.Red / 255, color.Green / 255 > 255 ? 255 : color.Green / 255, color.Blue / 255 > 255 ? 255 : color.Blue / 255); opacity = Math.Round(opacity, 1); // circle, ellipse, rect, line, polyline, polygon, text(X) foreach(ColorizeItem clrItem in this.colorizeItems) { XmlNode gNode = this.svgDoc.DocumentElement.GetElementsByTagName("g")[0]; // if the geometric primitives are grouped in a g-tag if(gNode != null) { try { string style = gNode.ChildNodes[clrItem.index].Attributes["style"].Value; if(clrItem.stroke && clrItem.fill) { // replace the current fill color value style = Regex.Replace(style, "fill:#[0-9a-fA-F]{6}", "fill:" + hexcolor); // replace the fill opacity style = Regex.Replace(style, "fill-opacity:[1]|fill-opacity:0.[0-9]+", "fill-opacity:" + opacity); // replace the stroke color style = Regex.Replace(style, "stroke:#[0-9a-fA-F]{6}", "stroke:" + hexcolor); // replace the stroke opacity style = Regex.Replace(style, "stroke-opacity:[1]|fill-opacity:0.[0-9]+", "stroke-opacity:" + opacity); // apply the changes gNode.ChildNodes[clrItem.index].Attributes["style"].Value = style; }else if(clrItem.fill && !clrItem.stroke) { style = Regex.Replace(style, "fill:#[0-9a-fA-F]{6}", "fill:" + hexcolor); style = Regex.Replace(style, "fill-opacity:[1]|fill-opacity:0.[0-9]+", "fill-opacity:" + opacity); gNode.ChildNodes[clrItem.index].Attributes["style"].Value = style; }else if(!clrItem.fill && clrItem.stroke) { style = Regex.Replace(style, "stroke:#[0-9a-fA-F]{6}", "stroke:" + hexcolor); style = Regex.Replace(style, "stroke-opacity:[1]|fill-opacity:0.[0-9]+", "stroke-opacity:" + opacity); gNode.ChildNodes[clrItem.index].Attributes["style"].Value = style; } }catch(Exception e) { MessageBox.ShowError("There was an error when trying to change the shape's color:\n" + e.Message); break; } } } this.svgContent = this.svgDoc.OuterXml; // recreate this Rsvg.Handle with the changed svg data this.svgHandle = new Handle(System.Text.Encoding.UTF8.GetBytes(svgDoc.OuterXml)); }
internal Shape() { this.position = new Point(0, 0); this.svgHandle = new Handle(); this.size = new Size(); this.size.Width = 0; this.size.Height = 0; this.zoom = 0; this.rotation = 0; this.selected = false; this.flipMode = FlipMode.None; this.svgContent = null; this.svgDoc = new XmlDocument(); }
/// <summary> /// Creates a new instance of the MeeGen.Shape class /// </summary> /// <param name="handle"> /// The Rsvg.Handle that contains the image data for this instance /// A <see cref="Handle"/> /// </param> /// <param name="pos"> /// The starting position of the new shape /// A <see cref="Point"/> /// </param> public Shape(Handle handle, Point pos) { this.position = pos; this.svgHandle = handle; this.size = new Size(this.svgHandle.Dimensions.Width, this.svgHandle.Dimensions.Height); this.zoom = 1d; this.rotation = 0d; this.selected = false; this.flipMode = FlipMode.None; if(handle.BaseUri.Length > 0) { StreamReader reader = new StreamReader(handle.BaseUri.Substring(7)); svgContent = reader.ReadToEnd(); reader.Close(); } this.svgDoc = new XmlDocument(); svgDoc.LoadXml(svgContent); this.colorizeItems = this.ParseMetadata(); }