Menü 2 für Experten: Wasserzeichen auf Images
Mit XAML kann man einen Text als Wasserzeichen über Bilder legen. Dieses How-to zeigt allerdings, wie WriteableBitmap-Instanzen ein Wasserzeichen dynamisch erhalten können. Dazu kann man sich mit einer Extension-Method von Benjamin Roux (MVP für Client App Dev) behelfen (Listing 3). Zusätzlich müssen die Libraries vom Open-Source-Projekt WriteableBitmapEx herunterladen und referenziert werden.
Listing 3: WriteableBitmap-Extension mit Wasserzeichentext für Bilder
public static class WriteableBitmapEx { public static WriteableBitmap Watermark(this WriteableBitmap input, string watermark, Color color = default(Color), double fontSize = 50, double opacity = 0.25, bool hasDropShadow = true) { var watermarked = GetTextBitmap(watermark, fontSize, color == default(Color) ? Colors.White : color, opacity, hasDropShadow); var width = watermarked.PixelWidth; var height = watermarked.PixelHeight; var result = input.Clone(); var position = new Rect(input.PixelWidth - width - 20, input.PixelHeight - height, width, height); result.Blit(position, watermarked, new Rect(0, 0, width, height)); return result; } private static WriteableBitmap GetTextBitmap(string text, double fontSize, Color color, double opacity, bool hasDropShadow) { TextBlock txt = new TextBlock(); txt.Text = text; txt.FontSize = fontSize; txt.Foreground = new SolidColorBrush(color); txt.Opacity = opacity; if (hasDropShadow) txt.Effect = new DropShadowEffect(); WriteableBitmap bitmap = new WriteableBitmap((int)txt.ActualWidth, (int)txt.ActualHeight); bitmap.Render(txt, null); bitmap.Invalidate(); return bitmap; } }