画像にウォーターマークを追加する
この文書では、Aspose.PSDを使用して画像にウォーターマークを追加する方法について説明します。画像処理アプリケーションで画像にウォーターマークを追加することは一般的な要件です。この例では、Graphicsクラスを使用して画像の表面に文字列を描画します。
ウォーターマークの追加
操作を示すために、ディスクからBMP画像を読み込み、GraphicsクラスのDrawStringメソッドを使用して画像の表面にウォーターマークとして文字列を描画します。PngOptionsクラスを使用して画像をPNG形式で保存します。以下に、画像にウォーターマークを追加する方法を示すコード例があります。この例のソースコードは、追いやすいように複数の部分に分割されています。ステップバイステップで、例は以下のようにどのようにするかを示します。
- 画像を読み込む。
- Graphicsオブジェクトを作成および初期化する。
- FontおよびSolidBrushオブジェクトを作成および初期化する。
- GraphicsクラスのDrawStringメソッドを使用して、ウォーターマークとして文字列を描画する。
- 画像をPNG形式で保存する。
以下のコードスニペットは、画像にウォーターマークを追加する方法を示します。
// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET | |
// Load a PSD file as an image and cast it into PsdImage | |
using (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. | |
using (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.Alignment = StringAlignment.Center; | |
sf.LineAlignment = 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.Width, psdImage.Height), sf); | |
} | |
// Export the image into PNG file format. | |
psdImage.Save(dataDir + "AddWatermark_output.png", new PngOptions()); | |
} |
対角線のウォーターマークの追加
画像に対角線のウォーターマークを追加することは、上記で説明した水平のウォーターマークを追加することと類似していますが、いくつかの違いがあります。操作を示すために、ディスクからJPG画像を読み込み、Matrixクラスのオブジェクトを使用して変換を追加し、GraphicsクラスのDrawStringメソッドを使用して画像の表面にウォーターマークとして文字列を描画します。以下に、画像に対角線のウォーターマークを追加する方法を示すコード例があります。この例のソースコードは、追いやすいように複数の部分に分割されています。ステップバイステップで、例は以下のようにどのようにするかを示します。
- 画像を読み込む。
- Graphicsオブジェクトを作成および初期化する。
- FontおよびSolidBrushオブジェクトを作成および初期化する。
- SizeFオブジェクト内の画像サイズを取得する。
- Matrixクラスのインスタンスを作成し、複合変換を行う。
- 変換をGraphicsオブジェクトに割り当てる。
- StringFormatオブジェクトを作成および初期化する。
- GraphicsクラスのDrawStringメソッドを使用して、ウォーターマークとして文字列を描画する。
- 結果画像を保存する。
以下のコードスニペットは、対角線のウォーターマークを追加する方法を示します。
// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET | |
// Load a PSD file as an image and cast it into PsdImage | |
using (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. | |
using (SolidBrush brush = new SolidBrush(Color.FromArgb(50, 128, 128, 128))) | |
{ | |
// specify transform matrix to rotate watermark. | |
graphics.Transform = new Matrix(); | |
graphics.Transform.RotateAt(45, new PointF(psdImage.Width / 2, psdImage.Height / 2)); | |
// Specify string alignment to put watermark at the image center. | |
StringFormat sf = new StringFormat(); | |
sf.Alignment = StringAlignment.Center; | |
// Draw watermark using font, partly-transparent brush at the image center. | |
graphics.DrawString("Some watermark text", font, brush, new RectangleF(0, psdImage.Height / 2, psdImage.Width, psdImage.Height / 2), sf); | |
} | |
// Export the image into PNG file format. | |
psdImage.Save(dataDir + "AddDiagnolWatermark_output.png", new PngOptions()); | |
} |