Disegno di immagini utilizzando Graphics

Disegno di immagini utilizzando 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, modificandone così la superficie. La classe Graphics utilizza una varietà di oggetti ausiliari per migliorare le forme:

  • Penne, per disegnare linee, delineare forme o renderizzare altre rappresentazioni geometriche.
  • Pennelli, per definire come vengono riempite le aree.
  • Caratteri, per definire la forma dei caratteri di testo.

Disegno con la classe Graphics

Di seguito è riportato un esempio di codice che dimostra l’utilizzo della classe Graphics. Il codice sorgente dell’esempio è stato diviso 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 dei metodi descritti in Creazione di file.

String dataDir = Utils.getDataDir(DrawingBezier.class) + "DrawingImages/";
// Create an instance of BmpOptions and set its various properties
String outpath = dataDir + "Bezier.bmp";
// Create an instance of BmpOptions and set its various properties
BmpOptions saveOptions = new BmpOptions();
saveOptions.setBitsPerPixel(32);
// Create an instance of Image
try (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.getYellow());
// Initializes the instance of PEN class with black color and width
Pen BlackPen = new Pen(Color.getBlack(), 3);
float startX = 10;
float startY = 25;
float controlX1 = 20;
float controlY1 = 5;
float controlX2 = 55;
float controlY2 = 10;
float endX = 90;
float endY = 25;
// Draw a Bezier shape by specifying the Pen object having black color and co-ordinate Points and save all changes.
graphic.drawBezier(BlackPen, startX, startY, controlX1, controlY1, controlX2, controlY2, endX, endY);
// export image to bmp file format.
image.save(outpath, saveOptions);
}

Creare e inizializzare un oggetto Graphics

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

String dataDir = Utils.getDataDir(DrawingArc.class) + "DrawingImages/";
// Create an instance of BmpOptions and set its various properties
String outpath = dataDir + "Arc.bmp";
// Create an instance of BmpOptions and set its various properties
BmpOptions saveOptions = new BmpOptions();
saveOptions.setBitsPerPixel(32);
// Create an instance of Image
try (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.getYellow());
// 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.getBlack()), 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 un colore come parametro. Questo metodo riempie la superficie Graphics con il colore passato come argomento.

String dataDir = Utils.getDataDir(DrawingUsingGraphics.class) + "DrawingImages/";
// Create an instance of Image
try (PsdImage image = new PsdImage(500, 500)) {
Graphics graphics = new Graphics(image);
// Clear the image surface with white color and Create and initialize a Pen object with blue color
graphics.clear(Color.getWhite());
Pen pen = new Pen(Color.getBlue());
// 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));
LinearGradientBrush linearGradientBrush = new LinearGradientBrush(image.getBounds(), Color.getRed(), Color.getWhite(), 45f);
// graphics.fillPolygon(linearGradientBrush, new Point(200, 200), new Point(400, 200), new Point(250, 350));
Point[] points = {new Point(200, 200), new Point(400, 200), new Point(250, 350)};
graphics.fillPolygon(linearGradientBrush, points);
// 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 Java. La classe Graphics ha esposto diverse versioni sovraccaricate del metodo DrawEllipse. Tutti questi metodi accettano un oggetto Pen come primo argomento. I parametri successivi sono passati per definire il rettangolo delimitante intorno all’ellisse. Per l’esempio, utilizza la versione che accetta un oggetto Rectangle come secondo parametro per disegnare un’ellisse utilizzando l’oggetto Pen nel colore desiderato.

String dataDir = Utils.getDataDir(DrawingEllipse.class) + "DrawingImages/";
// Create an instance of BmpOptions and set its various properties
String outpath = dataDir + "Ellipse.bmp";
// Create an instance of BmpOptions and set its various properties
BmpOptions saveOptions = new BmpOptions();
saveOptions.setBitsPerPixel(32);
// Create an instance of Image
try (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.getYellow());
// Draw a dotted ellipse shape by specifying the Pen object having red color and a surrounding Rectangle
graphic.drawEllipse(new Pen(Color.getRed()), new Rectangle(30, 10, 40, 80));
// Draw a continuous ellipse shape by specifying the Pen object having solid brush with blue color and a surrounding Rectangle
graphic.drawEllipse(new Pen(new SolidBrush(Color.getBlue())), new Rectangle(10, 30, 80, 40));
// export image to bmp file format.
image.save(outpath, saveOptions);
}

Disegnare un poligono riempito

Successivamente, disegna un poligono utilizzando il LinearGradientBrush e un array di punti. La classe Graphics ha esposto diverse versioni sovraccaricate del metodo FillPolygon. Tutti questi accettano un oggetto Pennello come primo argomento, che definisce 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.

String dataDir = Utils.getDataDir(DrawingUsingGraphics.class) + "DrawingImages/";
// Create an instance of Image
try (PsdImage image = new PsdImage(500, 500)) {
Graphics graphics = new Graphics(image);
// Clear the image surface with white color and Create and initialize a Pen object with blue color
graphics.clear(Color.getWhite());
Pen pen = new Pen(Color.getBlue());
// 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));
LinearGradientBrush linearGradientBrush = new LinearGradientBrush(image.getBounds(), Color.getRed(), Color.getWhite(), 45f);
// graphics.fillPolygon(linearGradientBrush, new Point(200, 200), new Point(400, 200), new Point(250, 350));
Point[] points = {new Point(200, 200), new Point(400, 200), new Point(250, 350)};
graphics.fillPolygon(linearGradientBrush, points);
// export modified image.
image.save(dataDir + "DrawingUsingGraphics_output.bmp", new BmpOptions());
}

Disegno di immagini utilizzando Graphics: Codice sorgente completo

String dataDir = Utils.getDataDir(DrawingUsingGraphics.class) + "DrawingImages/";
// Create an instance of Image
try (PsdImage image = new PsdImage(500, 500)) {
Graphics graphics = new Graphics(image);
// Clear the image surface with white color and Create and initialize a Pen object with blue color
graphics.clear(Color.getWhite());
Pen pen = new Pen(Color.getBlue());
// 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));
LinearGradientBrush linearGradientBrush = new LinearGradientBrush(image.getBounds(), Color.getRed(), Color.getWhite(), 45f);
// graphics.fillPolygon(linearGradientBrush, new Point(200, 200), new Point(400, 200), new Point(250, 350));
Point[] points = {new Point(200, 200), new Point(400, 200), new Point(250, 350)};
graphics.fillPolygon(linearGradientBrush, points);
// export modified image.
image.save(dataDir + "DrawingUsingGraphics_output.bmp", new BmpOptions());
}

Tutte le classi che implementano IDisposable e accedono a risorse non gestite sono istanziate in un’istruzione Using per garantire che vengano correttamente dispose.