Disegno di Immagini usando Graphics

Disegno di Immagini usando Graphics

Con la libreria Aspose.PSD puoi disegnare forme semplici come linee, rettangoli e cerchi, così come forme complesse come poligoni, curve, archi e forme di Bezier. La libreria Aspose.PSD crea tali forme utilizzando la classe Graphics che risiede nello spazio dei nomi Aspose.PSD. Gli oggetti Graphics sono responsabili di eseguire diverse operazioni di disegno su un’immagine, modificando quindi la superficie dell’immagine. La classe Graphics utilizza una varietà di oggetti di supporto per migliorare le forme:

· Pennelli, per disegnare linee, contornare forme o renderizzare altre rappresentazioni geometriche.

· Pennelli, per definire come vengono riempite le aree.

· Caratteri, per definire la forma dei caratteri del testo.

Disegno con la Classe Graphics

Di seguito è riportato un esempio di codice che dimostra l’uso della classe Graphics. Il codice di esempio è stato suddiviso in diverse parti per mantenerlo semplice e facile da seguire. Passo dopo passo, gli esempi mostrano come:

  1. Creare un’immagine.
  2. Creare e inizializzare un oggetto Graphics.
  3. Pulire la superficie.
  4. Disegnare un’ellisse.
  5. Disegnare un poligono riempito e salvare l’immagine.

Esempi di Programmazione

Creazione di un’Immagine

Inizia creando un’immagine utilizzando uno qualsiasi dei metodi descritti in Creare File.

// 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());
}

Creare e Inizializzare un Oggetto Graphics

Quindi crea e inizializza un oggetto Graphics passando l’oggetto Image al suo costruttore.

// 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);
}

Pulire la Superficie

Pulisci la superficie Graphics chiamando il metodo Clear della classe Graphics e passa il colore come parametro. Questo metodo riempie la superficie Graphics con il colore passato come argomento.

// 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());
}

Disegnare un’Ellisse

Potresti notare che la classe Graphics ha esposto molti metodi per disegnare e riempire forme. Troverai l’elenco completo dei metodi nel riferimento API di Aspose.PSD per .NET. Sono state esposte diverse versioni sovraccaricate del metodo DrawEllipse dalla classe Graphics. Tutti questi metodi accettano un oggetto Pen come primo argomento. I parametri successivi vengono passati per definire il rettangolo di delimitazione attorno all’ellisse. Per questo esempio, usa la versione che accetta un oggetto Rectangle come secondo parametro per disegnare un’ellisse utilizzando l’oggetto Pen nel colore desiderato.

// 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());
}

Disegnare un Poligono Riempito

Successivamente, disegna un poligono utilizzando il LinearGradientBrush e un array di punti. La classe Graphics ha esposto diverse versioni sovraccariche del metodo FillPolygon(). Tutti questi accettano un oggetto Brush come primo argomento, definendo le caratteristiche del riempimento. Il secondo parametro è un array di punti. Si noti che ogni due punti consecutivi nell’array specificano un lato del poligono.

// 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());
}

Disegno di Immagini usando Graphics : Codice Sorgente Completo

// 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());
}

Tutte le classi che implementano IDisposable e accedono risorse non gestite vengono istanziate in una dichiarazione Using per garantire che vengano correttamente eliminate.