Grafiken zeichnen mit Graphics
Grafiken zeichnen mit Graphics
Mit der Aspose.PSD-Bibliothek können Sie einfache Formen wie Linien, Rechtecke und Kreise sowie komplexe Formen wie Polygone, Kurven, Bögen und Bezierformen zeichnen. Die Aspose.PSD-Bibliothek erstellt solche Formen mithilfe der Klasse Graphics, die im Namensraum Aspose.PSD enthalten ist. Grafikobjekte sind verantwortlich für verschiedene Zeichenoperationen auf einem Bild und ändern somit die Oberfläche des Bildes. Die Graphics-Klasse verwendet verschiedene Hilfsobjekte, um die Formen zu verbessern:
· Stifte zum Zeichnen von Linien, Umrandungen von Formen oder Rendern anderer geometrischer Darstellungen.
· Pinsel zum Definieren, wie Flächen gefüllt werden.
· Schriftarten zur Definition der Form von Textzeichen.
Zeichnen mit der Graphics-Klasse
Im Folgenden finden Sie ein Codebeispiel, das die Verwendung der Graphics-Klasse demonstriert. Der Beispielquellcode wurde in mehrere Teile aufgeteilt, um ihn einfach und leicht verständlich zu halten. Schritt für Schritt zeigen die Beispiele, wie Sie:
- Ein Bild erstellen.
- Ein Graphics-Objekt erstellen und initialisieren.
- Die Oberfläche löschen.
- Eine Ellipse zeichnen.
- Ein gefülltes Polygon zeichnen und das Bild speichern.
Programmierbeispiele
Erstellen eines Bildes
Beginnen Sie mit dem Erstellen eines Bildes mithilfe einer der Methoden, die in der Anleitung zur Erstellung von Dateien beschrieben sind.
// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET | |
string loadpath = dataDir + "sample.psd"; | |
string outpath = dataDir + "CoreDrawingFeatures.bmp"; | |
// Create an instance of Image | |
using (PsdImage image = new PsdImage(loadpath)) | |
{ | |
// load pixels | |
var pixels = image.LoadArgb32Pixels(new Rectangle(0, 0, 100, 10)); | |
for (int i = 0; i < pixels.Length; i++) | |
{ | |
// specify pixel color value (gradient in this case). | |
pixels[i] = i; | |
} | |
// save modified pixels. | |
image.SaveArgb32Pixels(new Rectangle(0, 0, 100, 10), pixels); | |
// export image to bmp file format. | |
image.Save(outpath, new BmpOptions()); | |
} |
Erstellen und Initialisieren eines Graphics-Objekts
Erstellen und Initialisieren Sie dann ein Graphics-Objekt, indem Sie das Image-Objekt an seinen Konstruktor übergeben.
// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET | |
string outpath = dataDir + "Arc.bmp"; | |
// Create an instance of BmpOptions and set its various properties | |
BmpOptions saveOptions = new BmpOptions(); | |
saveOptions.BitsPerPixel = 32; | |
// Create an instance of Image | |
using (Image image = new PsdImage(100, 100)) | |
{ | |
// Create and initialize an instance of Graphics class and clear Graphics surface | |
Graphics graphic = new Graphics(image); | |
graphic.Clear(Color.Yellow); | |
// Draw an arc shape by specifying the Pen object having red black color and coordinates, height, width, start & end angles | |
int width = 100; | |
int height = 200; | |
int startAngle = 45; | |
int sweepAngle = 270; | |
// Draw arc to screen and save all changes. | |
graphic.DrawArc(new Pen(Color.Black), 0, 0, width, height, startAngle, sweepAngle); | |
// export image to bmp file format. | |
image.Save(outpath, saveOptions); | |
} |
Die Oberfläche löschen
Löschen Sie die Grafikoberfläche, indem Sie die Clear-Methode der Graphics-Klasse aufrufen und die Farbe als Parameter übergeben. Diese Methode füllt die Grafikoberfläche mit der übergebenen Farbe.
// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET | |
// Create an instance of Image | |
using (PsdImage image = new PsdImage(500, 500)) | |
{ | |
var graphics = new Graphics(image); | |
// Clear the image surface with white color and Create and initialize a Pen object with blue color | |
graphics.Clear(Color.White); | |
var pen = new Pen(Color.Blue); | |
// Draw Ellipse by defining the bounding rectangle of width 150 and height 100 also Draw a polygon using the LinearGradientBrush | |
graphics.DrawEllipse(pen, new Rectangle(10, 10, 150, 100)); | |
using (var linearGradientBrush = new LinearGradientBrush(image.Bounds, Color.Red, Color.White, 45f)) | |
{ | |
graphics.FillPolygon(linearGradientBrush, new[] { new Point(200, 200), new Point(400, 200), new Point(250, 350) }); | |
} | |
// export modified image. | |
image.Save(dataDir + "DrawingUsingGraphics_output.bmp", new BmpOptions()); | |
} |
Eine Ellipse zeichnen
Sie werden feststellen, dass die Graphics-Klasse viele Methoden zum Zeichnen und Füllen von Formen bereitstellt. Die vollständige Liste der Methoden finden Sie in der Aspose.PSD für .NET-API-Referenz. Die Graphics-Klasse hat mehrere überladene Versionen der DrawEllipse-Methode. All diese Methoden akzeptieren ein Pen-Objekt als ersten Argument. Die späteren Parameter werden übergeben, um das Begrenzungsrechteck um die Ellipse zu definieren. Verwenden Sie für dieses Beispiel die Version, die ein Rectangle-Objekt als zweiten Parameter akzeptiert, um eine Ellipse mit dem Pen-Objekt in Ihrer gewünschten Farbe zu zeichnen.
// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET | |
string loadpath = dataDir + "sample.psd"; | |
string outpath = dataDir + "CoreDrawingFeatures.bmp"; | |
// Create an instance of Image | |
using (PsdImage image = new PsdImage(loadpath)) | |
{ | |
// load pixels | |
var pixels = image.LoadArgb32Pixels(new Rectangle(0, 0, 100, 10)); | |
for (int i = 0; i < pixels.Length; i++) | |
{ | |
// specify pixel color value (gradient in this case). | |
pixels[i] = i; | |
} | |
// save modified pixels. | |
image.SaveArgb32Pixels(new Rectangle(0, 0, 100, 10), pixels); | |
// export image to bmp file format. | |
image.Save(outpath, new BmpOptions()); | |
} |
Ein gefülltes Polygon zeichnen
Zeichnen Sie als nächstes ein Polygon mit dem LinearGradientBrush und einem Array von Punkten. Die Graphics-Klasse bietet mehrere überladene Versionen der FillPolygon()-Methode. Alle akzeptieren ein Brush-Objekt als erstes Argument zur Definition der Fülleigenschaften. Der zweite Parameter ist ein Array von Punkten. Beachten Sie, dass jedes zweite aufeinanderfolgende Elemente im Array eine Seite des Polygons festlegen.
// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET | |
// Create an instance of Image | |
using (PsdImage image = new PsdImage(500, 500)) | |
{ | |
var graphics = new Graphics(image); | |
// Clear the image surface with white color and Create and initialize a Pen object with blue color | |
graphics.Clear(Color.White); | |
var pen = new Pen(Color.Blue); | |
// Draw Ellipse by defining the bounding rectangle of width 150 and height 100 also Draw a polygon using the LinearGradientBrush | |
graphics.DrawEllipse(pen, new Rectangle(10, 10, 150, 100)); | |
using (var linearGradientBrush = new LinearGradientBrush(image.Bounds, Color.Red, Color.White, 45f)) | |
{ | |
graphics.FillPolygon(linearGradientBrush, new[] { new Point(200, 200), new Point(400, 200), new Point(250, 350) }); | |
} | |
// export modified image. | |
image.Save(dataDir + "DrawingUsingGraphics_output.bmp", new BmpOptions()); | |
} |
Grafiken zeichnen mit Graphics: Vollständiger Quellcode
// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET | |
// Create an instance of Image | |
using (PsdImage image = new PsdImage(500, 500)) | |
{ | |
var graphics = new Graphics(image); | |
// Clear the image surface with white color and Create and initialize a Pen object with blue color | |
graphics.Clear(Color.White); | |
var pen = new Pen(Color.Blue); | |
// Draw Ellipse by defining the bounding rectangle of width 150 and height 100 also Draw a polygon using the LinearGradientBrush | |
graphics.DrawEllipse(pen, new Rectangle(10, 10, 150, 100)); | |
using (var linearGradientBrush = new LinearGradientBrush(image.Bounds, Color.Red, Color.White, 45f)) | |
{ | |
graphics.FillPolygon(linearGradientBrush, new[] { new Point(200, 200), new Point(400, 200), new Point(250, 350) }); | |
} | |
// export modified image. | |
image.Save(dataDir + "DrawingUsingGraphics_output.bmp", new BmpOptions()); | |
} |
Alle Klassen, die IDisposable implementieren und auf nicht verwaltete Ressourcen zugreifen, werden in einer Using-Anweisung instanziiert, um sicherzustellen, dass sie korrekt verworfen werden.