画像に透かしを追加する
このドキュメントでは、Aspose.PSDを使用して画像に透かしを追加する方法について説明します。画像処理アプリケーションにおいて、画像に透かしを追加することは一般的な要件です。この例では、Graphicsクラスを使用して画像表面に文字列を描画します。
透かしの追加
操作を実証するために、ディスクからBMP画像を読み込み、GraphicsクラスのDrawStringメソッドを使用して透かしとして文字列を画像表面に描画します。また、PngOptionsクラスを使用して画像をPNG形式で保存します。以下は、画像に透かしを追加する方法を示すコード例です。コード例は工程ごとに分割されており、追いやすくなっています。
- 画像を読み込む
- Graphicsオブジェクトを作成し初期化する
- フォントとSolidBrushオブジェクトを作成および初期化する
- GraphicsクラスのDrawStringメソッドを使用して文字列を透かしとして描画する
- 画像をPNG形式で保存する
以下のコードスニペットは、画像に透かしを追加する方法を示しています。
String dataDir = Utils.getDataDir(AddWatermark.class) + "PSD/"; | |
// Load a PSD file as an image and cast it into PsdImage | |
try (PsdImage psdImage = (PsdImage) Image.load(dataDir + "layers.psd")) { | |
// Create graphics object to perform draw operations. | |
Graphics graphics = new Graphics(psdImage); | |
// Create font to draw watermark with. | |
Font font = new Font("Arial", 20.0f); | |
// Create a solid brush with color alpha set near to 0 to use watermarking effect. | |
SolidBrush brush = new SolidBrush(Color.fromArgb(50, 128, 128, 128)); | |
// Specify string alignment to put watermark at the image center. | |
StringFormat sf = new StringFormat(); | |
sf.setAlignment(StringAlignment.Center); | |
sf.setLineAlignment(StringAlignment.Center); | |
// Draw watermark using font, partly-transparent brush and rotation matrix at the image center. | |
graphics.drawString("Some watermark text", font, brush, new RectangleF(0, 0, psdImage.getWidth(), psdImage.getHeight()), sf); | |
// Export the image into PNG file format. | |
psdImage.save(dataDir + "AddWatermark_output.png", new PngOptions()); | |
} |
対角線状の透かしの追加
画像に対角線状の透かしを追加することは、上記で説明した水平方向の透かしを追加するのと似ており、いくつかの違いがあります。操作を実証するために、ディスクからJPG画像を読み込み、Matrixクラスのオブジェクトを使用して変換を追加し、GraphicsクラスのDrawStringメソッドを使用して、画像表面に文字列を透かしとして描画します。以下は、画像に対角線状の透かしを追加する方法を示すコード例です。コード例は工程ごとに分割されており、追いやすくなっています。
- 画像を読み込む
- Graphicsオブジェクトを作成し初期化する
- フォントとSolidBrushオブジェクトを作成および初期化する
- 画像のサイズをSizeFオブジェクトで取得する
- Matrixクラスのインスタンスを作成し、複合変換を実行する
- 変換をGraphicsオブジェクトに割り当てる
- StringFormatオブジェクトを作成し初期化する
- GraphicsクラスのDrawStringメソッドを使用して文字列を透かしとして描画する
- 結果の画像を保存する
以下のコードスニペットは、対角線状の透かしを追加する方法を示しています。
String dataDir = Utils.getDataDir(AddDiagnolWatermark.class) + "PSD/"; | |
// Load a PSD file as an image and cast it into PsdImage | |
try (PsdImage psdImage = (PsdImage) Image.load(dataDir + "layers.psd")) { | |
// Create graphics object to perform draw operations. | |
Graphics graphics = new Graphics(psdImage); | |
// Create font to draw watermark with. | |
Font font = new Font("Arial", 20.0f); | |
// Create a solid brush with color alpha set near to 0 to use watermarking effect. | |
SolidBrush brush = new SolidBrush(Color.fromArgb(50, 128, 128, 128)); | |
// specify transform matrix to rotate watermark. | |
graphics.setTransform(new Matrix()); | |
graphics.getTransform().rotateAt(45, new PointF(psdImage.getWidth() / 2, psdImage.getHeight() / 2)); | |
// Specify string alignment to put watermark at the image center. | |
StringFormat sf = new StringFormat(); | |
sf.setAlignment(StringAlignment.Center); | |
// Draw watermark using font, partly-transparent brush at the image center. | |
graphics.drawString("Some watermark text", font, brush, new RectangleF(0, psdImage.getHeight() / 2, psdImage.getWidth(), psdImage.getHeight() / 2), sf); | |
// Export the image into PNG file format. | |
psdImage.save(dataDir + "AddDiagnolWatermark_output.png", new PngOptions()); | |
} |