private System.Drawing.Image Watermark(ref System.Drawing.Image sourceImage, ref System.Drawing.Image watermarkImage, int x, int y, int opacity = 30)
{
Bitmap srcImage = new Bitmap(sourceImage);
Bitmap wtrImage = new Bitmap(watermarkImage);
if (opacity < 0) { opacity = 0; }
if (opacity > 100) { opacity = 100; }
float opaque = (opacity / 100.0f);
using (Graphics g = Graphics.FromImage(srcImage))
{
System.Drawing.Imaging.ColorMatrix matrix = new System.Drawing.Imaging.ColorMatrix(
new float[][]
{
new float[] {1, 0, 0, 0, 0},
new float[] {0, 1, 0, 0, 0},
new float[] {0, 0, 1, 0, 0},
new float[] {0, 0, 0, opaque, 0},
new float[] {0, 0, 0, 0, 1}
});
System.Drawing.Imaging.ImageAttributes attributes = new System.Drawing.Imaging.ImageAttributes();
attributes.SetColorMatrix(matrix);
g.DrawImage(wtrImage, new Rectangle(x, y, wtrImage.Width, wtrImage.Height), 0, 0, wtrImage.Width, wtrImage.Height, GraphicsUnit.Pixel, attributes);
}
return srcImage;
}
Posted: March 19, 2023
Return to the snippets listing