Add a watermark to an image in C#
Subroutine DrawWatermark copies a watermark image onto an image.
// Copy the watermark image over the result image.The code first loops through the watermark's pixels setting the alpha component of each to 128 to make them 50 percent opaque. It then sets the image's transparent color to the color of the pixel in the upper left corner. This pixel should have the image's background color, as should any other pixels that you want to be transparent in the result. The program then copies the watermark image onto the result image.
private void DrawWatermark(Bitmap watermark_bm, Bitmap result_bm, int x, int y)
{
const byte ALPHA = 128;
// Set the watermark's pixels' Alpha components.
Color clr;
for (int py = 0; py < watermark_bm.Height; py++)
{
for (int px = 0; px < watermark_bm.Width; px++)
{
clr = watermark_bm.GetPixel(px, py);
watermark_bm.SetPixel(px, py, Color.FromArgb(ALPHA, clr.R, clr.G, clr.);
}
}
// Set the watermark's transparent color.
watermark_bm.MakeTransparent(watermark_bm.GetPixel(0, 0));
// Copy onto the result image.
using (Graphics gr = Graphics.FromImage(result_bm))
{
gr.DrawImage(watermark_bm, x, y);
}
}



Comments