[C# Helper]
Index Books FAQ Contact About Rod
[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

[C# 24-Hour Trainer]

[Beginning Software Engineering]

[C# 5.0 Programmer's Reference]

[MCSD Certification Toolkit (Exam 70-483): Programming in C#]

Title: Add a watermark to an image in C#

[Add a watermark to an image in C#]

The following DrawWatermark method copies a watermark image onto an image.

// Copy the watermark image over 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.B)); } } // 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); } }

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.

[Add a watermark to an image in C#] For instance, this example uses the picture on the right so the black and white areas are visible at 50% opacity and the rest of the image is transparent.

Having prepared the watermark image, the program then copies it onto the result image.

Download the example to experiment with it and to see additional details.

© 2009-2022 Rocky Mountain Computer Consulting, Inc. All rights reserved.