龙年新作:水印文字添加工具源码摘要

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4. using System.Windows;  
  5. using System.Windows.Media;  
  6. using System.Windows.Media.Imaging;  
  7.   
  8. namespace ImageProc  
  9. {  
  10.     public class WaterText  
  11.     {  
  12.         /// <summary>  
  13.         /// 生成添加水印后的图象。  
  14.         /// </summary>  
  15.         /// <param name="src">用于在其上添加水印的源图象,一个ImageSource的子类的对象。</param>  
  16.         /// <param name="rect">一个Rect结构,表示要生成的图象的大小。</param>  
  17.         /// <param name="test">水印文本。</param>  
  18.         /// <param name="fontFamily">使用的字体名称,如宋体等。</param>  
  19.         /// <param name="fontSize">字体大小。</param>  
  20.         /// <param name="DpiX">要生成的图象的水平分辨率(建议96以上)。</param>  
  21.         /// <param name="DpiY">要生成的图象的垂直分辨率(建议96以上)。</param>  
  22.         /// <param name="PaddingH">水平边距。</param>  
  23.         /// <param name="PaddingV">垂直边距。</param>  
  24.         /// <param name="loca">水印在图象中的位置,一个WaterLocation枚举。</param>  
  25.         /// <param name="wcolor">水印文字的颜色。</param>  
  26.         /// <param name="IsBold">水印文字是否加粗。</param>  
  27.         /// <param name="IsItalic">水印文是否倾斜。</param>  
  28.         /// <returns>一个ImageSource实例,表示处理后的图象。</returns>  
  29.         public BitmapSource BuildIamge(BitmapSource src, Rect rect, string test, string fontFamily, double fontSize,  
  30.                 double DpiX, double DpiY, double PaddingH, double PaddingV, WaterLocation loca, Color wcolor,  
  31.                 bool IsBold, bool IsItalic)  
  32.         {  
  33.             DrawingVisual drv = new DrawingVisual();  
  34.             // 通过DrawingVisual的RenderOpen方法返回一个DrawingContext  
  35.             using (DrawingContext dc = drv.RenderOpen())  
  36.             {  
  37.                 // 绘制图象  
  38.                 dc.DrawImage(src, rect);  
  39.                 // 设置文字  
  40.                 FontFamily fontf = new FontFamily(fontFamily);  
  41.                 FontStyle MyFontStyle = IsItalic == true ? FontStyles.Italic : FontStyles.Normal;  
  42.                 FontWeight MyFontWeight = IsBold == true ? FontWeights.Bold : FontWeights.Normal;  
  43.                 Typeface tface = new Typeface(fontf, MyFontStyle, MyFontWeight, FontStretches.Normal);  
  44.                 SolidColorBrush cb = new SolidColorBrush(wcolor);  
  45.                 FormattedText ft = new FormattedText(  
  46.                     test,  
  47.                     System.Globalization.CultureInfo.CurrentCulture,  
  48.                     FlowDirection.LeftToRight,  
  49.                     tface,  
  50.                     fontSize,  
  51.                     cb);  
  52.   
  53.                 // 确定文本的位置  
  54.                 double newX = 0, newY = 0;  
  55.                 TextAlignment MyAlignment = TextAlignment.Left;  
  56.                 switch (loca)  
  57.                 {  
  58.                     case WaterLocation.TopLeft:  
  59.                         newX = PaddingH;  
  60.                         newY = PaddingV;  
  61.                         MyAlignment = TextAlignment.Left;  
  62.                         break;  
  63.                     case WaterLocation.TopCenter:  
  64.                         newX = PaddingH;  
  65.                         newY = PaddingV;  
  66.                         MyAlignment = TextAlignment.Center;  
  67.                         break;  
  68.                     case WaterLocation.TopRight:  
  69.                         newX = PaddingH;  
  70.                         newY = PaddingV;  
  71.                         MyAlignment = TextAlignment.Right;  
  72.                         break;  
  73.                     case WaterLocation.MidLeft:  
  74.                         newX = PaddingH;  
  75.                         newY = ((rect.Height - PaddingV * 2) - ft.Height) / 2;  
  76.                         MyAlignment = TextAlignment.Left;  
  77.                         break;  
  78.                     case WaterLocation.MidCenter:  
  79.                         newX = PaddingH;  
  80.                         newY = ((rect.Height - PaddingV * 2) - ft.Height) / 2;  
  81.                         MyAlignment = TextAlignment.Center;  
  82.                         break;  
  83.                     case WaterLocation.MidRight:  
  84.                         newX = PaddingH;  
  85.                         newY = ((rect.Height - PaddingV * 2) - ft.Height) / 2;  
  86.                         MyAlignment = TextAlignment.Right;  
  87.                         break;  
  88.                     case WaterLocation.BottomLeft:  
  89.                         newX = PaddingH;  
  90.                         newY = rect.Height - PaddingV - ft.Height;  
  91.                         MyAlignment = TextAlignment.Left;  
  92.                         break;  
  93.                     case WaterLocation.BottomCenter:  
  94.                         newX = PaddingH;  
  95.                         newY = rect.Height - PaddingV - ft.Height;  
  96.                         MyAlignment = TextAlignment.Center;  
  97.                         break;  
  98.                     case WaterLocation.BottomRight:  
  99.                         newX = PaddingH;  
  100.                         newY = rect.Height - PaddingV - ft.Height;  
  101.                         MyAlignment = TextAlignment.Right;  
  102.                         break;  
  103.                     default:  
  104.                         break;  
  105.                 }  
  106.   
  107.                 ft.MaxTextWidth = rect.Width - 2 * PaddingH;  
  108.                 ft.TextAlignment = MyAlignment;  
  109.                 // 绘制文字  
  110.                 dc.DrawText(ft, new Point(newX, newY));  
  111.             }  
  112.   
  113.             RenderTargetBitmap rdb = new RenderTargetBitmap(  
  114.                 src.PixelWidth,  
  115.                 src.PixelHeight,  
  116.                 DpiX, DpiY, PixelFormats.Default);  
  117.   
  118.             // 通过该方法呈现可视化对象。  
  119.             rdb.Render(drv);  
  120.             return rdb;  
  121.         }  
  122.   
  123.     }  
  124.   
  125.     /// <summary>  
  126.     /// 水印的位置  
  127.     /// </summary>  
  128.     public enum WaterLocation  
  129.     {  
  130.         /// <summary>  
  131.         /// 左上角  
  132.         /// </summary>  
  133.         TopLeft,  
  134.         /// <summary>  
  135.         /// 顶部居中  
  136.         /// </summary>  
  137.         TopCenter,  
  138.         /// <summary>  
  139.         /// 右上角  
  140.         /// </summary>  
  141.         TopRight,  
  142.         /// <summary>  
  143.         /// 中部居左  
  144.         /// </summary>  
  145.         MidLeft,  
  146.         /// <summary>  
  147.         /// 中部居中  
  148.         /// </summary>  
  149.         MidCenter,  
  150.         /// <summary>  
  151.         /// 中部居右  
  152.         /// </summary>  
  153.         MidRight,  
  154.         /// <summary>  
  155.         /// 左下角  
  156.         /// </summary>  
  157.         BottomLeft,  
  158.         /// <summary>  
  159.         /// 底部居中  
  160.         /// </summary>  
  161.         BottomCenter,  
  162.         /// <summary>  
  163.         /// 右下角  
  164.         /// </summary>  
  165.         BottomRight  
  166.     }  
  167. }  

源码要点1:初始屏幕中的按钮,在打开图片前显示,存在已打开图片后隐藏,类似QQ影音。

这里自定义了一个转换器,如果Image的Source属性为null,则按钮的Visibility属性为Visible,否则为Collapsed。

  1.  /// <summary>  
  2. /// 类型转换器  
  3. /// </summary>  
  4. [ValueConversion(typeof(ImageSource), typeof(Visibility))]  
  5. public class VisibleConvert : IValueConverter  
  6. {  
  7.   
  8.     public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)  
  9.     {  
  10.         if (value == null)  
  11.         {  
  12.             return Visibility.Visible;  
  13.         }  
  14.         else  
  15.         {  
  16.             return Visibility.Collapsed;  
  17.         }  
  18.     }  
  19.   
  20.     public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)  
  21.     {  
  22.         return null;  
  23.     }  
  24.   
  25. }  

接着,把这个转换器用到Binding中,对按钮进行绑定。

  1. Binding myBinding = new Binding();  
  2. myBinding.Path = new PropertyPath(Image.SourceProperty);  
  3. myBinding.Converter = new VisibleConvert();  
  4. myBinding.Source = this.img;  
  5. myBinding.Mode = BindingMode.OneWay;  
  6. BindingOperations.SetBinding(btnOpenBig, Button.VisibilityProperty, myBinding);  

源码要点2:“打开”和“保存”对话框。

WPF中没有这些对话框组件,但是,我们不要忘了,在WinForm里面,有OpenFileDialog类和SaveFileDialog类。

但是,System.Windows.Forms命名空间下,个别类型的名字与System.Windows命名空间下的类型名字相同,也就是说,WinForm中用到的许多类和WPF中的类名字相同,但它们是不同逻辑的,所以,在引入命名空间的时候,应当使用别名,这样在代码中使用的时候就不会发生冲突了。

  1. using DW = System.Drawing;  
  2. using WF = System.Windows.Forms;  
  3. using IO = System.IO;  

所以,在保存文件的时候,我们可以这样写代码。

    1. WF.SaveFileDialog saveDlg = new WF.SaveFileDialog();  
    2. int MyIndex = this.cmbFormat.SelectedIndex;  
    3. saveDlg.Title = "保存文件";  
    4. BitmapEncoder MyEncoder = null;  
    5. switch (MyIndex)  
    6. {  
    7.     case 0:  
    8.         saveDlg.FileName = @"outPut.jpg";  
    9.         MyEncoder = new JpegBitmapEncoder();  
    10.         break;  
    11.     case 1:  
    12.         saveDlg.FileName = @"outPut.bmp";  
    13.         MyEncoder = new BmpBitmapEncoder();  
    14.         break;  
    15.     case 2:  
    16.         saveDlg.FileName = @"outPut.png";  
    17.         MyEncoder = new PngBitmapEncoder();  
    18.         break;  
    19.     default:  
    20.         saveDlg.FileName = @"outPut.jpg";  
    21.         MyEncoder = new JpegBitmapEncoder();  
    22.         break;  
    23. }  
    24. if (saveDlg.ShowDialog() == WF.DialogResult.OK)  
    25. {  
    26.     string fileName = saveDlg.FileName;  
    27.     try  
    28.     {  
    29.         using (IO.FileStream fs = IO.File.Open(fileName, IO.FileMode.OpenOrCreate, IO.FileAccess.Write))  
    30.         {  
    31.             MyEncoder.Frames.Add(BitmapFrame.Create((BitmapSource)this.img.Source));  
    32.             MyEncoder.Save(fs);  
    33.             fs.Close();  
    34.         }  
    35.         MessageBox.Show("保存成功。", "提示", MessageBoxButton.OK, MessageBoxImage.Information);  
    36.     }  
    37.     catch (Exception ex)  
    38.     {  
    39.         MessageBox.Show(ex.Message, "提示", MessageBoxButton.OK, MessageBoxImage.Warning);  
    40.     }  
    41. }  
原文地址:https://www.cnblogs.com/xieweikai/p/6832826.html