Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
Aspose.SVG for .NET bearbeitet SVG als strukturiertes Vektordokument. Laden Sie eine SVG-Datei in ein
SVGDocument, suchen oder erstellen Sie SVG-Elemente über DOM-APIs, ändern Sie Geometrie, Attribute, Text, Stile oder Pfaddaten und speichern Sie die bearbeitete Datei, ohne sie zu rastern.
Ein typischer Arbeitsablauf zur SVG-Bearbeitung:
SVGDocument.CreateElementNS() ein neues Element.Save().Das folgende Beispiel lädt eine SVG-Datei, fügt ihrem <svg>-Stammelement einen blauen Kreis hinzu und speichert das geänderte Dokument:
1using System.IO;
2using Aspose.Svg;
3
4string inputPath = Path.Combine(DataDir, "shapes.svg");
5string outputPath = Path.Combine(OutputDir, "shapes-with-circle.svg");
6const string SvgNamespace = "http://www.w3.org/2000/svg";
7
8using (SVGDocument document = new SVGDocument(inputPath))
9{
10 // Access the root <svg> element, which is present in an SVG document.
11 SVGSVGElement svgElement = document.RootElement;
12
13 // Create a circle and define its geometry and appearance.
14 SVGCircleElement circle = (SVGCircleElement)document.CreateElementNS(SvgNamespace, "circle");
15 circle.Cx.BaseVal.Value = 50;
16 circle.Cy.BaseVal.Value = 50;
17 circle.R.BaseVal.Value = 40;
18 circle.SetAttribute("fill", "#3A86FF");
19
20 // Add the circle to the document tree and save the edited SVG file.
21 svgElement.AppendChild(circle);
22 document.Save(outputPath);
23}Wenn Sie das zu bearbeitende Element zunächst suchen müssen, finden Sie Beispiele mit CSS-Selektoren und XPath im Artikel SVG in C# inspizieren und navigieren. Optionen zum Laden von Dokumenten beschreibt SVG-Dokumente erstellen und laden.
Die Eigenschaft
RootElement gibt das <svg>-Stammelement zurück. Neue SVG-Elemente werden im SVG-Namespace erstellt und anschließend an einen Container angehängt oder an einer bestimmten Position in die Dokumentstruktur eingefügt.
Dieses Fragment erstellt ein <g>-Element und fügt es als erstes untergeordnetes Element des Stammelements ein:
1const string SvgNamespace = "http://www.w3.org/2000/svg";
2
3SVGSVGElement svgElement = document.RootElement;
4SVGGElement groupElement = (SVGGElement)document.CreateElementNS(SvgNamespace, "g");
5
6// Apply common styles to every graphic element later added to the group.
7groupElement.SetAttribute("fill", "#8A8D8F");
8groupElement.SetAttribute("stroke", "magenta");
9groupElement.SetAttribute("stroke-width", "4");
10
11svgElement.InsertBefore(groupElement, svgElement.FirstChild);Verwenden Sie
SetAttribute(), um SVG-Attribute wie fill, stroke, transform, class oder d für neu erstellte oder vorhandene Elemente festzulegen oder zu ändern.
Attributwerte können mit den Methoden
SetAttribute(name, value),
GetAttribute(name),
HasAttribute(name) und
RemoveAttribute(name) der Klasse
Element verwaltet werden.
Aspose.SVG for .NET stellt typisierte DOM-Klassen für Standardformen bereit, darunter
SVGCircleElement,
SVGEllipseElement,
SVGRectElement,
SVGLineElement,
SVGPolylineElement,
SVGPolygonElement und
SVGPathElement. Mit ihren typisierten Eigenschaften lässt sich Geometrie direkt bearbeiten: Zum Beispiel definieren Cx.BaseVal.Value, Cy.BaseVal.Value und R.BaseVal.Value einen Kreis, während der Inhalt als editierbare Vektorgrafik erhalten bleibt.
Ein Kreis wird über die typisierten Eigenschaften Cx, Cy und R gesteuert. Das folgende vollständige Beispiel fügt einem vorhandenen SVG-Dokument einen Kreis hinzu:
1using Aspose.Svg;
2using System.IO;
3using Aspose.Svg.Dom; 1// Add a circle element to an existing SVG document in C#
2
3// Set the SVG namespace URI
4string SvgNamespace = "http://www.w3.org/2000/svg";
5
6// Load an SVG document from a file
7using (SVGDocument document = new SVGDocument(Path.Combine(DataDir, "basic-shapes.svg")))
8{
9 // Get the root <svg> element of the document
10 SVGSVGElement svgElement = document.RootElement;
11
12 // Create a <circle> element
13 SVGCircleElement circleElement = (SVGCircleElement)document.CreateElementNS(SvgNamespace, "circle");
14 circleElement.Cx.BaseVal.Value = 100F;
15 circleElement.Cy.BaseVal.Value = 100F;
16 circleElement.R.BaseVal.Value = 50F;
17 circleElement.SetAttribute("fill", "Salmon");
18
19 // Add the <circle> element as the first child to <svg> element
20 svgElement.InsertBefore(circleElement, svgElement.FirstChild);
21
22 // Work with the document here...
23 // Add a polyline and change stroke attributes for all circle and ellipse elements (see later)
24
25 // Save the document
26 document.Save(Path.Combine(OutputDir, "basic-shapes_add-circle.svg"));
27}Die Ellipse (Cx, Cy, Rx, Ry), das Rechteck (X, Y, Width, Height, Rx, Ry) und die Linie (X1, Y1, X2, Y2) besitzen eigene Attribute, die auf ähnliche Weise festgelegt werden können.
Das SVG-Element <polyline> speichert seine Scheitelpunktkoordinaten im Attribut points. In Aspose.SVG bietet die Liste SVGPolylineElement.Points typisierten Zugriff auf diese Koordinaten. Mit
CreateSVGPoint() können Sie Punkte hinzufügen, ohne die Attributzeichenfolge manuell zusammenzusetzen:
1const string SvgNamespace = "http://www.w3.org/2000/svg";
2
3SVGSVGElement svgElement = document.RootElement;
4SVGPolylineElement polylineElement = (SVGPolylineElement)document.CreateElementNS(SvgNamespace, "polyline");
5
6SVGPoint point1 = svgElement.CreateSVGPoint();
7point1.X = 270;
8point1.Y = 240;
9SVGPoint point2 = svgElement.CreateSVGPoint();
10point2.X = 290;
11point2.Y = 220;
12SVGPoint point3 = svgElement.CreateSVGPoint();
13point3.X = 310;
14point3.Y = 240;
15
16polylineElement.Points.AppendItem(point1);
17polylineElement.Points.AppendItem(point2);
18polylineElement.Points.AppendItem(point3);
19polylineElement.SetAttribute("stroke", "grey");
20polylineElement.SetAttribute("stroke-width", "5");
21polylineElement.SetAttribute("fill", "none");
22
23// Add the new polyline to the SVG root element
24svgElement.AppendChild(polylineElement);
25
26// Update strokes of circle and ellipse elements already in the SVG document
27foreach (Element element in svgElement.Children)
28{
29 if (element is SVGCircleElement || element is SVGEllipseElement)
30 {
31 element.SetAttribute("stroke-width", "6");
32 element.SetAttribute("stroke", "#C8102E");
33 }
34}Das folgende Bild zeigt die ursprüngliche Datei basic-shapes.svg und das Ergebnis nach dem Hinzufügen von Formen und Ändern der Konturattribute.

Die Geometrie eines <path>-Elements wird in seinem Attribut d gespeichert. Erstellen Sie ein typisiertes
SVGPathElement, ändern Sie dessen Pfaddaten und fügen Sie es wie jedes andere Element zum SVG-Dokument hinzu:
1const string SvgNamespace = "http://www.w3.org/2000/svg";
2
3SVGPathElement pathElement =
4 (SVGPathElement)document.CreateElementNS(SvgNamespace, "path");
5
6// Set the path geometry and its visible style.
7pathElement.SetAttribute("d", "M 10 200 Q 25 210 180 200 T 300 250 T 420 250 T 490 150");
8pathElement.SetAttribute("stroke", "magenta");
9pathElement.SetAttribute("fill", "none");
10pathElement.SetAttribute("stroke-width", "4");
11
12document.RootElement.InsertBefore(pathElement, document.RootElement.FirstChild);Wenn der Code Pfadbefehle segmentweise erstellt oder ändert, stellt SVGPathElement auch Operationen für Pfadsegmente bereit. Das folgende Beispiel erstellt einen Pfad und ändert ausgewählte Befehlskoordinaten in einer vorhandenen SVG-Datei:
1using Aspose.Svg;
2using System.IO;
3using Aspose.Svg.Paths; 1// Edit SVG path data programmatically in C#
2
3// Set the SVG namespace URI
4string SvgNamespace = "http://www.w3.org/2000/svg";
5
6using (SVGDocument document = new SVGDocument())
7{
8 SVGSVGElement svgElement = document.RootElement;
9
10 // Create a <path> element
11 SVGPathElement pathElement = (SVGPathElement)document.CreateElementNS(SvgNamespace, "path");
12
13 // Set d attribute parameters – SVG path data
14 pathElement.SetAttribute("d", "M 10 200 Q 25 110 180 200 T 300 250 T 420 250 T 490 150");
15
16 // Edit SVG path
17 foreach (SVGPathSeg pathSeg in pathElement.PathSegList)
18 {
19 // Editing T commands parameters
20 if (pathSeg is SVGPathSegCurvetoQuadraticSmoothAbs)
21 {
22 SVGPathSegCurvetoQuadraticSmoothAbs pathSegCurvetoQuadraticSmoothAbs = pathSeg as SVGPathSegCurvetoQuadraticSmoothAbs;
23
24 pathSegCurvetoQuadraticSmoothAbs.X -= 60;
25 pathSegCurvetoQuadraticSmoothAbs.Y -= 65;
26 }
27
28 // Editing M command parameters
29 if (pathSeg is SVGPathSegMovetoAbs)
30 {
31 SVGPathSegMovetoAbs pathSegMovetoAbs = pathSeg as SVGPathSegMovetoAbs;
32
33 pathSegMovetoAbs.X = 200;
34 pathSegMovetoAbs.Y = 100;
35 }
36 }
37 // Set fill and stroke attributes
38 pathElement.SetAttribute("stroke", "red");
39 pathElement.SetAttribute("fill", "none");
40 pathElement.SetAttribute("stroke-width", "4");
41
42 // Add the <path> element as the first child to the <svg> element
43 svgElement.InsertBefore(pathElement, svgElement.FirstChild);
44
45 // Save the document
46 document.Save(Path.Combine(OutputDir, "edit-svg-path-data.svg"));
47}Die Abbildung vergleicht den ursprünglichen schwarzen Pfad mit dem bearbeiteten roten Pfad. Sie können auch die bearbeitete Beispieldatei öffnen: edit-svg-path-data.svg.

Informationen zur Syntax von Pfadbefehlen und zum Zeichenverhalten finden Sie unter SVG-Pfaddaten.
Ein SVG-Dokument kann über ein <image>-Element auf ein Rasterbild verweisen und editierbaren Vektortext oder Formen darüber platzieren. Dabei werden die Pixel der Bitmap nicht verändert; Hintergrundbild und SVG-Überlagerung bleiben separate Teile des Vektordokuments.
Dieses Verfahren ist nützlich, wenn ein Foto, ein Scan oder eine andere Rastergrafik Beschriftungen, Markierungen, Badges oder dekorative Grafiken erhalten soll, die weiterhin als SVG bearbeitet werden können. In SVG entspricht die Zeichenreihenfolge der Reihenfolge der Elemente: Wird <image> zuerst und Text oder Formen danach eingefügt, erscheinen die Vektorelemente über dem Bitmap-Hintergrund.
1<svg xmlns="http://www.w3.org/2000/svg">
2 <image href="/svg/images/api/seaside.jpg" height="480" width="640" x="20" y="20"/>
3 <text style="font-size: 1.4em;" x="420" y="280" fill="gold">The beach is beautiful...</text>
4 <circle cx="520" cy="120" r="60" stroke="gold" stroke-width="70" fill="none" stroke-dasharray="2,14"/>
5</svg>So erstellen Sie diese Komposition mit Aspose.SVG for .NET:
SVGDocument und greifen Sie über
RootElement auf das <svg>-Stammelement zu.CreateElementNS() ein
SVGImageElement, legen Sie Bildverweis, Position und Größe fest und hängen Sie es als Hintergrund an das Stammelement an.SVGTextElement, legen Sie Text, Position, Schriftstil und Füllfarbe fest und hängen Sie es nach dem Bild an.SVGCircleElement, definieren Sie Position, Radius und Konturattribute und hängen Sie es nach dem Hintergrundbild an. In diesem Beispiel erzeugt eine breite gestrichelte Kontur einen sonnenähnlichen Effekt.Save() auf, um das SVG-Dokument zu schreiben. In der gespeicherten Datei bleiben Bitmap-Verweis, Text und Kreis als editierbare SVG-Elemente erhalten.Das folgende Beispiel führt diese Schritte aus und speichert die SVG-Ausgabe:
1using Aspose.Svg;
2using System.IO;
3using Aspose.Svg.Paths; 1// Draw SVG shapes and text on an existing bitmap in C#
2
3// Set the SVG namespace URI
4string SvgNamespace = "http://www.w3.org/2000/svg";
5
6using (SVGDocument document = new SVGDocument())
7{
8 SVGSVGElement svgElement = document.RootElement;
9
10 // Create an <image> element and add it into svgElement
11 SVGImageElement imageElement = (SVGImageElement)document.CreateElementNS(SvgNamespace, "image");
12 imageElement.Href.BaseVal = "http://docs.aspose.com/svg/images/api/seaside.jpg";
13 imageElement.Height.BaseVal.ConvertToSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_PX);
14 imageElement.Width.BaseVal.ConvertToSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_PX);
15 imageElement.Height.BaseVal.Value = 480;
16 imageElement.Width.BaseVal.Value = 640;
17 imageElement.X.BaseVal.Value = 20;
18 imageElement.Y.BaseVal.Value = 20;
19 svgElement.AppendChild(imageElement);
20
21 // Create a <text> element, set its attributes, and add it to the <svg> element
22 SVGTextElement textElement = (SVGTextElement)document.CreateElementNS(SvgNamespace, "text");
23 textElement.Style.FontSize = "1.4em";
24 textElement.SetAttribute("x", "420px");
25 textElement.SetAttribute("fill", "gold");
26 textElement.SetAttribute("y", "280px");
27 textElement.TextContent = "The beach is beautiful...";
28 svgElement.AppendChild(textElement);
29
30 // Create a <circle> element, set its attributes, and add it to the <svg> element
31 SVGCircleElement circleElement = (SVGCircleElement)document.CreateElementNS(SvgNamespace, "circle");
32 circleElement.Cx.BaseVal.Value = 520;
33 circleElement.Cy.BaseVal.Value = 120;
34 circleElement.R.BaseVal.Value = 60;
35 circleElement.SetAttribute("stroke", "gold");
36 circleElement.SetAttribute("stroke-width", "70");
37 circleElement.SetAttribute("fill", "none");
38 circleElement.SetAttribute("stroke-dasharray", "2,14");
39 svgElement.AppendChild(circleElement);
40
41 // Save the document
42 document.Save(Path.Combine(OutputDir, "svg-drawing-on-bitmap.svg"));
43}Weitere Informationen zu Konturbreite, Strichmustern und anderen SVG-Darstellungsattributen finden Sie unter Füllungen und Konturen in SVG.

1. Wird eine SVG-Datei beim Bearbeiten mit Aspose.SVG in eine Bitmap konvertiert?
Nein. Bei der Bearbeitung über SVGDocument werden SVG-Elemente, Attribute und Pfaddaten geändert, während das Dokument Vektorinhalt bleibt. Eine Rasterausgabe entsteht nur, wenn SVG ausdrücklich in ein Bildformat wie PNG, JPEG oder WebP konvertiert oder gerendert wird.
2. Wie bearbeite ich ein vorhandenes SVG-Element, anstatt ein neues hinzuzufügen?
Suchen Sie das gewünschte Element mit einem CSS-Selektor oder einer XPath-Abfrage und ändern Sie anschließend seine Attribute oder typisierten SVG-Eigenschaften. Nach Auswahl eines <circle> kann der Code beispielsweise Radius, Position, Füllung oder Kontur ändern. Siehe
SVG in C# inspizieren und navigieren und
SVG-Farbe ändern.
3. Kann ich ein SVG-Dokument bearbeiten und das geänderte Ergebnis im selben Ablauf konvertieren?
Ja. Laden Sie die Quelle in SVGDocument, nehmen Sie die Änderungen vor, erstellen Sie Speicheroptionen für das Zielformat und rufen Sie Converter.ConvertSVG(document, options, outputPath) auf. Verwenden Sie beispielsweise PdfSaveOptions für PDF oder ImageSaveOptions für PNG, JPEG, TIFF, GIF, BMP oder WebP. Vollständige Beispiele finden Sie unter
SVG-Dateien in C# konvertieren.
4. Kann ich eine vorhandene SVG-Form durch ein anderes Element ersetzen?
Ja. Suchen Sie das vorhandene Element mit einem Selektor oder einer XPath-Abfrage, erstellen Sie den Ersatz mit CreateElementNS(), fügen Sie das neue Element in denselben übergeordneten Knoten ein und entfernen Sie den alten Knoten. So können etwa Kreismarkierungen durch Rechtecke ersetzt werden, ohne den Rest des SVG-Dokuments zu verändern. Ein Beispiel finden Sie unter
SVG in C# inspizieren und navigieren.
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.