グラフィックスを使用して画像を描画する
Aspose.PSDライブラリを使用すると、直線、四角形、円などの単純な形状だけでなく、多角形、曲線、円弧、ベジエ形状などの複雑な形状も描画できます。 Aspose.PSDライブラリは、Aspose.PSDネームスペースに存在するGraphicsクラスを使用してこれらの形状を作成します。 Graphicsオブジェクトは、画像にさまざまな描画操作を行い、画像の表面を変更する責任があります。 Graphicsクラスは、次のようないくつかのヘルパーオブジェクトを使用して形状を拡張します:
- ペン:直線を描画したり、形状の輪郭を描画したり、他の幾何学的表現をレンダリングしたりするためのもの。
- ブラシ:領域がどのように塗りつぶされるかを定義するもの。
- フォント:テキストの文字の形状を定義するもの。
Graphicsクラスを使用した描画
以下は、Graphicsクラスの使用法を示すコード例です。コード例のソースコードはいくつかの部分に分割されており、簡単で追いやすいようにしています。ステップバイステップで、次のように行う方法を示します:
- 画像を作成する。
- Graphicsオブジェクトを作成および初期化する。
- 表面をクリアする。
- 楕円を描画する。
- 塗りつぶされた多角形を描画して画像を保存する。
プログラミングサンプル
画像の作成
画像を作成するには、Creating Filesで説明されている方法のいずれかを使用して画像を作成します。
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); | |
} |
Graphicsオブジェクトの作成および初期化
次に、Imageオブジェクトをそのコンストラクタに渡すことで、Graphicsオブジェクトを作成および初期化します。
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); | |
} |
表面のクリア
Graphicsの表面をクリアするには、GraphicsクラスのClearメソッドを呼び出し、パラメータとして色を渡します。このメソッドは、引数として渡された色でGraphicsの表面を塗りつぶします。
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()); | |
} |
楕円を描画
Graphicsクラスには、多くの形状を描画したり塗りつぶしたりするためのメソッドが公開されていることに気づくでしょう。Aspose.PSD for Java API Referenceでメソッドの完全なリストが見つかります。Graphicsクラスによって公開されているDrawEllipseメソッドのいくつかのオーバーロードされたバージョンがあります。これらのメソッドは、楕円の周りの境界矩形を定義するために後続のパラメータとして渡されるPenオブジェクトを最初の引数として受け入れます。この例では、望む色でPenオブジェクトを使用して楕円を描画するために、第2パラメータとしてRectangleオブジェクトを受け入れるバージョンを使用します。
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); | |
} |
塗りつぶされた多角形を描画
次に、LinearGradientBrushおよびポイントの配列を使用して多角形を描画します。Graphicsクラスは、FillPolygonメソッドのいくつかのオーバーロードされたバージョンを公開しています。これらすべては塗りつぶしの特性を定義するBrushオブジェクトを最初の引数として受け入れ、第2パラメータとしてポイントの配列を受け入れます。配列内の連続する2つのポイントは、多角形の辺を指定しますのでご注意ください。
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()); | |
} |
グラフィックスを使用して画像を描画する:完全なソース
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()); | |
} |
全てのIDisposableを実装し、アンマネージリソースにアクセスするクラスはきちんと破棄されるように、Usingステートメントでインスタンス化されます。