画像を描画するためにGraphicsを使用する

画像を描画するためにGraphicsを使用する

Aspose.PSDライブラリを使用すると、線、四角形、円などの単純な形状だけでなく、多角形、曲線、円弧、ベジエ形状などの複雑な形状も描画できます。Aspose.PSDライブラリは、Aspose.PSDネームスペースに存在するGraphicsクラスを使用してこれらの形状を作成します。Graphicsオブジェクトは、異なる描画操作を行い、画像の表面を変更する責任があります。Graphicsクラスは、次のようなヘルパーオブジェクトの種類を使用して形状を向上させます:

·         ペンは、線を描画したり、形状の輪郭を描いたり、他の幾何学的表現をレンダリングするために使用されます。

·         ブラシは、領域をどのように塗りつぶすかを定義します。

·         フォントは、テキストの文字の形状を定義します。

Graphicsクラスを使用して描画する

以下はGraphicsクラスの使用を示すコード例です。例のソースコードは、シンプルで理解しやすいように複数の部分に分割されています。ステップバイステップで、例を使用して次のことができます:

  1. 画像を作成する。
  2. Graphicsオブジェクトを作成して初期化する。
  3. 表面を消去する。
  4. 楕円を描画する。
  5. 塗りつぶされた多角形を描画して画像を保存する。

プログラミングサンプル

画像の作成

作成方法に関する説明のいずれかを使用して画像を作成してください。

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

Graphicsオブジェクトの作成と初期化

次に、画像オブジェクトをGraphicsオブジェクトのコンストラクタに渡してGraphicsオブジェクトを作成および初期化します。

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

表面を消去する

GraphicsクラスのClearメソッドを呼び出してグラフィックス表面を消去し、パラメーターとして色を渡します。このメソッドは、引数として渡された色でGraphics表面を塗りつぶします。

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

楕円を描画する

Graphicsクラスには、形状を描画および塗りつぶすための多くのメソッドが公開されていることに気付くでしょう。Aspose.PSD for .NET APIリファレンスでメソッドの完全なリストを見つけることができます。Graphicsクラスによって公開されているDrawEllipseメソッドのいくつかのオーバーロードされたバージョンがあります。これらのメソッドはすべて、第1引数としてPenオブジェクトを受け取ります。後のパラメーターは、楕円の周りの境界矩形を定義するために渡されます。この例のために、第2パラメーターとしてRectangleオブジェクトを受け入れるバージョンを使用して、指定した色のPenオブジェクトを使用して楕円を描画してください。

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

塗りつぶされた多角形を描画する

次に、LinearGradientBrushおよびポイントの配列を使用して多角形を描画します。Graphicsクラスには、FillPolygon()メソッドの多くのオーバーロードされたバージョンが公開されています。これらすべては、塗りつぶしの特性を定義するBrushオブジェクトを最初の引数として受け入れます。第2パラメーターは、ポイントの配列です。配列の連続する2つのポイントが多角形の1辺を指定することに注意してください。

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

Graphicsを使用して画像を描画する: 完全なソース

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

全てのアンマネージドリソースにアクセスするIDisposableを実装するすべてのクラスは、正しく破棄されるようにUsingステートメントでインスタンス化されます。