0

如何向以下例程中保存的网络摄像头图像添加水印:

 public static void SaveImageCapture(BitmapSource bitmap)
    {
        JpegBitmapEncoder encoder = new JpegBitmapEncoder();
        encoder.Frames.Add(BitmapFrame.Create(bitmap));

        encoder.QualityLevel = 100;

        // Configure save file dialog box
        Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
        dlg.FileName = "Image"; // Default file name
        dlg.DefaultExt = ".Jpg"; // Default file extension
        dlg.Filter = "Image (.jpg)|*.jpg"; // Filter files by extension

        // Show save file dialog box
        Nullable<bool> result = dlg.ShowDialog();

        // Process save file dialog box results
        if (result == true)
        {
            // Save Image
            string filename = dlg.FileName;
            FileStream fstream = new FileStream(filename, FileMode.Create);
            encoder.Save(fstream);
            fstream.Close();
        }
4

1 回答 1

1

以下方法在 BitmapSource 上绘制文本并将整个内容作为新的 BitmapSource 返回,您可以将其传递给您的保存方法。

你必须玩弄FormattedText对象的字体、大小和位置。有关所有详细信息,请参阅 MSDN 文档。请特别注意DrawingContext文档以了解您的选择。

public BitmapSource AddWatermark(BitmapSource image, string watermarkText)
{
    var text = new FormattedText(
        watermarkText,
        CultureInfo.InvariantCulture,
        FlowDirection.LeftToRight,
        new Typeface("Segoe UI"),
        14,
        Brushes.White);

    var visual = new DrawingVisual();

    using (var drawingContext = visual.RenderOpen())
    {
        drawingContext.DrawImage(image, new Rect(0, 0, image.Width, image.Height));
        drawingContext.DrawText(text, new Point(0, 0));
    }

    var bitmap = new RenderTargetBitmap(image.PixelWidth, image.PixelHeight,
                                        image.DpiX, image.DpiY, PixelFormats.Default);
    bitmap.Render(visual);
    return bitmap;
}
于 2013-10-24T13:47:06.993 回答