Bir Resme Filigran Ekleme

Bir Resme Filigran Ekleme

Bu belge, Aspose.PSD kullanarak bir resme filigran eklemenin nasıl yapılacağını açıklar. Bir resme filigran eklemek, resim işleme uygulamaları için yaygın bir gereksinimdir. Bu örnek, bir dizeyi resim yüzeyine çizmek için Graphics sınıfını kullanan bir örnektir.

Filigran Ekleme

İşlemi göstermek için diskten bir BMP resim yükleyecek ve Graphics sınıfının DrawString yöntemini kullanarak resim yüzeyine filigran olarak bir dize çizilecektir. PngOptions sınıfını kullanarak resmi PNG biçiminde kaydedeceğiz. Aşağıda, bir resme filigran eklemenin nasıl yapılacağını gösteren bir kod örneği bulunmaktadır. Örnek kaynak kodu, kolayca takip edilebilmesi için parçalara ayrılmıştır. Adım adım, örnekler, aşağıdakileri nasıl yapılacağını göstermektedir:

  1. Bir resim yükle.
  2. Bir Graphics nesnesi oluştur ve başlat.
  3. Font ve SolidBrush nesnelerini oluştur ve başlat.
  4. Graphics sınıfının DrawString yöntemini kullanarak dizeyi filigran olarak çiz.
  5. Resmi PNG olarak kaydet.

Aşağıdaki kod parçacığı, resme filigran ekleme işlemini göstermektedir.

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

Çapraz Bir Filigran Ekleme

Bir resme çapraz bir filigran eklemek, yukarıda tartışılan yatay bir filigran eklemeye benzerdir, ancak birkaç farklılık vardır. İşlemi göstermek için diskin bir JPG resmini yükleyecek, Matrix sınıfından bir nesne kullanarak dönüşümler ekleyecek ve Graphics sınıfının DrawString yöntemini kullanarak resim yüzeyine bir dizeyi filigran olarak çizeceğiz. Aşağıda, bir resme çapraz bir filigran eklemenin nasıl yapılacağını gösteren bir kod örneği bulunmaktadır. Örnek kaynak kodu, kolayca takip edilebilmesi için parçalara ayrılmıştır. Adım adım, örnekler, aşağıdakileri nasıl yapılacağını göstermektedir:

  1. Bir resim yükle.
  2. Bir Graphics nesnesi oluştur ve başlat.
  3. Font ve SolidBrush nesnelerini oluştur ve başlat.
  4. Resmin SizeF nesnesindeki boyutunu al.
  5. Matrix sınıfından bir örnek oluştur ve bileşik dönüşüm gerçekleştir.
  6. Dönüşümü Graphics nesnesine atayın.
  7. StringFormat nesnesi oluştur ve başlat.
  8. Graphics sınıfının DrawString yöntemini kullanarak dizeyi filigran olarak çiz.
  9. Sonuç resmi kaydet.

Aşağıdaki kod parçacığı, resme çapraz bir filigran eklemenin nasıl yapılacağını göstermektedir.

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