WPF Image add watermark and save marked image as jpg

<Window x:Class="WpfApp165.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp165"
        mc:Ignorable="d" WindowState="Maximized"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="50"/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <StackPanel Orientation="Horizontal">
            <Button Content="Open" Click="OpenImageClick"/>
            <TextBlock Text="WaterMark:" VerticalAlignment="Center"/>
            <TextBox x:Name="tbx" Width="200"/>
            <Button Content="Add WaterMark" Click="AddWaterMarkClick" Width="200"/>
            <Button Content="Save" Click="SaveImageClick" Width="200"/>
        </StackPanel>
        <Image Grid.Row="1" x:Name="imgCtrl"/>
    </Grid>
</Window>





//cs
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using static System.Net.Mime.MediaTypeNames;
using Image = System.Windows.Controls.Image;

namespace WpfApp165
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        BitmapImage rawBmi = new BitmapImage();
        BitmapSource  markedBmi { get; set; }
        public MainWindow()
        {
            InitializeComponent(); 
        }
         

        //public FormattedText(string textToFormat, CultureInfo culture,
        //    FlowDirection flowDirection, Typeface typeface, double emSize,
        //    Brush foreground, NumberSubstitution numberSubstitution,
        //    TextFormattingMode textFormattingMode, double pixelsPerDip);
        public BitmapSource AddWatermark(BitmapSource image, string watermarkText)
        {
            var text = new FormattedText(
                watermarkText,CultureInfo.InvariantCulture,  FlowDirection.LeftToRight,
                new Typeface("Segoe UI"), 200, Brushes.Blue,1.25); 
            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;
        }

        private void OpenImageClick(object sender, RoutedEventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Filter = "Image (.jpg)|*.jpg|All Files (*.*)|*.*";
            if(dialog.ShowDialog()==true)
            {
                string imgUrl=dialog.FileName;
                rawBmi.BeginInit();
                rawBmi.UriSource=new Uri(imgUrl,UriKind.RelativeOrAbsolute);
                rawBmi.EndInit();
                imgCtrl.Source = rawBmi;
            }
        }

        private void AddWaterMarkClick(object sender, RoutedEventArgs e)
        {
            string markedText = tbx.Text;
            if(!string.IsNullOrWhiteSpace(markedText))
            {
                markedBmi = AddWatermark(rawBmi,markedText);
                imgCtrl.Source = markedBmi;
            }
        }

        private void SaveImageClick(object sender, RoutedEventArgs e)
        {
            if(markedBmi.Height>0)
            {
                SaveFileDialog dialog = new SaveFileDialog();
                dialog.Filter = "Image (.jpg)|*.jpg";
                if (dialog.ShowDialog() == true)
                {
                    string fileName = dialog.FileName;
                    using (var fileStream = new FileStream(fileName, FileMode.Create))
                    {
                        var encoder = new JpegBitmapEncoder();
                        encoder.Frames.Add(BitmapFrame.Create(markedBmi));
                        encoder.QualityLevel = 100;
                        encoder.Save(fileStream);
                    }
                }
            } 
        }
    }
}

 




 

 

 

 

 

 

posted @ 2024-06-15 11:57  FredGrit  阅读(5)  评论(0编辑  收藏  举报