使用RenderTargetBitmap和DrawingVisual为图片添加简单的水印

<Window x:Class="WpfApplication2.WatermarkWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="WatermarkWindow" Height="400" Width="500">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <StackPanel HorizontalAlignment="Center" Orientation="Horizontal">
            <Image Name="image1" Source="a.jpg" />
            <Image Name="image2" Source="a.jpg" />
        </StackPanel>
        <Button Grid.Row="1" Content="添加水印" Name="btnWatermark" Click="btnWatermark_Click" />
    </Grid>
</Window>

namespace WpfApplication2
{
    /// <summary>
    /// 使用RenderTargetBitmap和DrawingVisual为图片添加简单的水印
    /// </summary>
    public partial class WatermarkWindow : Window
    {
        const string path = @"D:\b.jpg";
        const string savePath = @"D:\c.jpg";
        public WatermarkWindow()
        {
            InitializeComponent();
            this.Loaded += (sender, e) =>
            {
                image1.Source = new BitmapImage(new Uri(path));
            };
        }
        private void btnWatermark_Click(object sender, RoutedEventArgs e)
        {
            //读取图像
            BitmapSource bitmap = new BitmapImage(new Uri(path, UriKind.Absolute));
            //使用DrawingVisual,然后通过RenderOpen方法获取DrawingContext,把图像和文字输出上去
            //创建 RenderTargetBitmap
            var renderTargetBitmap = new RenderTargetBitmap(bitmap.PixelWidth, bitmap.PixelHeight, bitmap.DpiX, bitmap.DpiY, PixelFormats.Default);
            //创建FormattedText对象来做水印
            //生成 FormattedText
            var text = new FormattedText("CMONO.NET .INC", CultureInfo.CurrentCulture, System.Windows.FlowDirection.LeftToRight, new Typeface(SystemFonts.MessageFontFamily, FontStyles.Normal, FontWeights.Bold, FontStretches.Normal), 24, Brushes.Red);
            //使用DrawingVisual和DrawingContext进行绘图,把图像和FormattedText输出
            var drawingVisual = new DrawingVisual();
            using (var dc = drawingVisual.RenderOpen())
            {
                dc.DrawImage(bitmap, new Rect(0, 0, bitmap.Width, bitmap.Height));
                dc.DrawRectangle(Brushes.Transparent, new Pen(Brushes.White, 7), new Rect(10, 10, 20 + text.Width, 20 + text.Height));
                dc.DrawText(text, new Point(bitmap.Width / 2, bitmap.Height - 100));
            }
            //调用RenderTargetBitmap的Render方法就可以了,传入刚才的DrawingVisual对象
            renderTargetBitmap.Render(drawingVisual);
            //使用BitmapDecoder把RenderTargetBitmap(也属于BitmapSource)保存至文件
            var bitmapEncoder = new JpegBitmapEncoder();
            bitmapEncoder.Frames.Add(BitmapFrame.Create(renderTargetBitmap));
            using (FileStream fs=new FileStream(savePath, FileMode.OpenOrCreate))
            {
                bitmapEncoder.Save(fs);
            }
            this.image2.Source = new BitmapImage(new Uri(savePath));
        }
    }
}

点击查看原图

知识共享许可协议
《使用RenderTargetBitmap和DrawingVisual为图片添加简单的水印》常伟华 创作。
采用 知识共享 署名-相同方式共享 3.0 中国大陆 许可协议进行许可。
  • 多说评论
  • 签名
  • 新浪微博
  • 默认评论
  • Tab Header 5

0 条评论 / 点击此处发表评论

Tab Content 5

开发技术


开发平台和工具

sitemap     157.81ms