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

2024-03-20 01:32

本文主要是介绍龙年新作:水印文字添加工具源码摘要,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

下载地址:http://www.xdowns.com/soft/31/93/2012/Soft_84985.html
 
源码要点核心:生成添加水印文字后的图片。
 
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace ImageProc
{
public class WaterText
{
/// <summary>
/// 生成添加水印后的图象。
/// </summary>
/// <param name="src">用于在其上添加水印的源图象,一个ImageSource的子类的对象。</param>
/// <param name="rect">一个Rect结构,表示要生成的图象的大小。</param>
/// <param name="test">水印文本。</param>
/// <param name="fontFamily">使用的字体名称,如宋体等。</param>
/// <param name="fontSize">字体大小。</param>
/// <param name="DpiX">要生成的图象的水平分辨率(建议96以上)。</param>
/// <param name="DpiY">要生成的图象的垂直分辨率(建议96以上)。</param>
/// <param name="PaddingH">水平边距。</param>
/// <param name="PaddingV">垂直边距。</param>
/// <param name="loca">水印在图象中的位置,一个WaterLocation枚举。</param>
/// <param name="wcolor">水印文字的颜色。</param>
/// <param name="IsBold">水印文字是否加粗。</param>
/// <param name="IsItalic">水印文是否倾斜。</param>
/// <returns>一个ImageSource实例,表示处理后的图象。</returns>
public BitmapSource BuildIamge(BitmapSource src, Rect rect, string test, string fontFamily, double fontSize,
double DpiX, double DpiY, double PaddingH, double PaddingV, WaterLocation loca, Color wcolor,
bool IsBold, bool IsItalic)
{
DrawingVisual drv = new DrawingVisual();
// 通过DrawingVisual的RenderOpen方法返回一个DrawingContext
using (DrawingContext dc = drv.RenderOpen())
{
// 绘制图象
dc.DrawImage(src, rect);
// 设置文字
FontFamily fontf = new FontFamily(fontFamily);
FontStyle MyFontStyle = IsItalic == true ? FontStyles.Italic : FontStyles.Normal;
FontWeight MyFontWeight = IsBold == true ? FontWeights.Bold : FontWeights.Normal;
Typeface tface = new Typeface(fontf, MyFontStyle, MyFontWeight, FontStretches.Normal);
SolidColorBrush cb = new SolidColorBrush(wcolor);
FormattedText ft = new FormattedText(
test,
System.Globalization.CultureInfo.CurrentCulture,
FlowDirection.LeftToRight,
tface,
fontSize,
cb);
// 确定文本的位置
double newX = 0, newY = 0;
TextAlignment MyAlignment = TextAlignment.Left;
switch (loca)
{
case WaterLocation.TopLeft:
newX = PaddingH;
newY = PaddingV;
MyAlignment = TextAlignment.Left;
break;
case WaterLocation.TopCenter:
newX = PaddingH;
newY = PaddingV;
MyAlignment = TextAlignment.Center;
break;
case WaterLocation.TopRight:
newX = PaddingH;
newY = PaddingV;
MyAlignment = TextAlignment.Right;
break;
case WaterLocation.MidLeft:
newX = PaddingH;
newY = ((rect.Height - PaddingV * 2) - ft.Height) / 2;
MyAlignment = TextAlignment.Left;
break;
case WaterLocation.MidCenter:
newX = PaddingH;
newY = ((rect.Height - PaddingV * 2) - ft.Height) / 2;
MyAlignment = TextAlignment.Center;
break;
case WaterLocation.MidRight:
newX = PaddingH;
newY = ((rect.Height - PaddingV * 2) - ft.Height) / 2;
MyAlignment = TextAlignment.Right;
break;
case WaterLocation.BottomLeft:
newX = PaddingH;
newY = rect.Height - PaddingV - ft.Height;
MyAlignment = TextAlignment.Left;
break;
case WaterLocation.BottomCenter:
newX = PaddingH;
newY = rect.Height - PaddingV - ft.Height;
MyAlignment = TextAlignment.Center;
break;
case WaterLocation.BottomRight:
newX = PaddingH;
newY = rect.Height - PaddingV - ft.Height;
MyAlignment = TextAlignment.Right;
break;
default:
break;
}
ft.MaxTextWidth = rect.Width - 2 * PaddingH;
ft.TextAlignment = MyAlignment;
// 绘制文字
dc.DrawText(ft, new Point(newX, newY));
}
RenderTargetBitmap rdb = new RenderTargetBitmap(
src.PixelWidth,
src.PixelHeight,
DpiX, DpiY, PixelFormats.Default);
// 通过该方法呈现可视化对象。
rdb.Render(drv);
return rdb;
}
}
/// <summary>
/// 水印的位置
/// </summary>
public enum WaterLocation
{
/// <summary>
/// 左上角
/// </summary>
TopLeft,
/// <summary>
/// 顶部居中
/// </summary>
TopCenter,
/// <summary>
/// 右上角
/// </summary>
TopRight,
/// <summary>
/// 中部居左
/// </summary>
MidLeft,
/// <summary>
/// 中部居中
/// </summary>
MidCenter,
/// <summary>
/// 中部居右
/// </summary>
MidRight,
/// <summary>
/// 左下角
/// </summary>
BottomLeft,
/// <summary>
/// 底部居中
/// </summary>
BottomCenter,
/// <summary>
/// 右下角
/// </summary>
BottomRight
}
}

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

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

     /// <summary>
/// 类型转换器
/// </summary>
[ValueConversion(typeof(ImageSource), typeof(Visibility))]
public class VisibleConvert : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value == null)
{
return Visibility.Visible;
}
else
{
return Visibility.Collapsed;
}
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return null;
}
}

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

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

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

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

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

using DW = System.Drawing;
using WF = System.Windows.Forms;
using IO = System.IO;

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

            WF.SaveFileDialog saveDlg = new WF.SaveFileDialog();
int MyIndex = this.cmbFormat.SelectedIndex;
saveDlg.Title = "保存文件";
BitmapEncoder MyEncoder = null;
switch (MyIndex)
{
case 0:
saveDlg.FileName = @"outPut.jpg";
MyEncoder = new JpegBitmapEncoder();
break;
case 1:
saveDlg.FileName = @"outPut.bmp";
MyEncoder = new BmpBitmapEncoder();
break;
case 2:
saveDlg.FileName = @"outPut.png";
MyEncoder = new PngBitmapEncoder();
break;
default:
saveDlg.FileName = @"outPut.jpg";
MyEncoder = new JpegBitmapEncoder();
break;
}
if (saveDlg.ShowDialog() == WF.DialogResult.OK)
{
string fileName = saveDlg.FileName;
try
{
using (IO.FileStream fs = IO.File.Open(fileName, IO.FileMode.OpenOrCreate, IO.FileAccess.Write))
{
MyEncoder.Frames.Add(BitmapFrame.Create((BitmapSource)this.img.Source));
MyEncoder.Save(fs);
fs.Close();
}
MessageBox.Show("保存成功。", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
}
}

源码要点3:如何取得程序标题和版本号。

程序标题栏上的文字是在运行后动态生成的,可以通过反射技术,在窗口类构造函数中设置。

我们先为窗口类定义两个公共属性,分别用来取得程序标题和版本号。

         /// <summary>
/// 程序标题
/// </summary>
public string TitleText
{
get
{
object[] objs = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyTitleAttribute), false);
if (objs.Length > 0)
{
AssemblyTitleAttribute attr = (AssemblyTitleAttribute)objs[0];
return attr.Title;
}
return string.Empty;
}
}
/// <summary>
/// 返回程序版本
/// </summary>
public string AppVersion
{
get
{
string ver = Assembly.GetExecutingAssembly().GetName().Version.ToString();
if (ver != "")
{
return ver;
}
return string.Empty;
}
}

这篇关于龙年新作:水印文字添加工具源码摘要的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/827877

相关文章

Java实现在Word文档中添加文本水印和图片水印的操作指南

《Java实现在Word文档中添加文本水印和图片水印的操作指南》在当今数字时代,文档的自动化处理与安全防护变得尤为重要,无论是为了保护版权、推广品牌,还是为了在文档中加入特定的标识,为Word文档添加... 目录引言Spire.Doc for Java:高效Word文档处理的利器代码实战:使用Java为Wo

Python实战之SEO优化自动化工具开发指南

《Python实战之SEO优化自动化工具开发指南》在数字化营销时代,搜索引擎优化(SEO)已成为网站获取流量的重要手段,本文将带您使用Python开发一套完整的SEO自动化工具,需要的可以了解下... 目录前言项目概述技术栈选择核心模块实现1. 关键词研究模块2. 网站技术seo检测模块3. 内容优化分析模

MySQL慢查询工具的使用小结

《MySQL慢查询工具的使用小结》使用MySQL的慢查询工具可以帮助开发者识别和优化性能不佳的SQL查询,本文就来介绍一下MySQL的慢查询工具,具有一定的参考价值,感兴趣的可以了解一下... 目录一、启用慢查询日志1.1 编辑mysql配置文件1.2 重启MySQL服务二、配置动态参数(可选)三、分析慢查

基于Python实现进阶版PDF合并/拆分工具

《基于Python实现进阶版PDF合并/拆分工具》在数字化时代,PDF文件已成为日常工作和学习中不可或缺的一部分,本文将详细介绍一款简单易用的PDF工具,帮助用户轻松完成PDF文件的合并与拆分操作... 目录工具概述环境准备界面说明合并PDF文件拆分PDF文件高级技巧常见问题完整源代码总结在数字化时代,PD

Python按照24个实用大方向精选的上千种工具库汇总整理

《Python按照24个实用大方向精选的上千种工具库汇总整理》本文整理了Python生态中近千个库,涵盖数据处理、图像处理、网络开发、Web框架、人工智能、科学计算、GUI工具、测试框架、环境管理等多... 目录1、数据处理文本处理特殊文本处理html/XML 解析文件处理配置文件处理文档相关日志管理日期和

使用Python开发一个Ditto剪贴板数据导出工具

《使用Python开发一个Ditto剪贴板数据导出工具》在日常工作中,我们经常需要处理大量的剪贴板数据,下面将介绍如何使用Python的wxPython库开发一个图形化工具,实现从Ditto数据库中读... 目录前言运行结果项目需求分析技术选型核心功能实现1. Ditto数据库结构分析2. 数据库自动定位3

基于Python实现简易视频剪辑工具

《基于Python实现简易视频剪辑工具》这篇文章主要为大家详细介绍了如何用Python打造一个功能完备的简易视频剪辑工具,包括视频文件导入与格式转换,基础剪辑操作,音频处理等功能,感兴趣的小伙伴可以了... 目录一、技术选型与环境搭建二、核心功能模块实现1. 视频基础操作2. 音频处理3. 特效与转场三、高

基于Python开发一个图像水印批量添加工具

《基于Python开发一个图像水印批量添加工具》在当今数字化内容爆炸式增长的时代,图像版权保护已成为创作者和企业的核心需求,本方案将详细介绍一个基于PythonPIL库的工业级图像水印解决方案,有需要... 目录一、系统架构设计1.1 整体处理流程1.2 类结构设计(扩展版本)二、核心算法深入解析2.1 自

Python办公自动化实战之打造智能邮件发送工具

《Python办公自动化实战之打造智能邮件发送工具》在数字化办公场景中,邮件自动化是提升工作效率的关键技能,本文将演示如何使用Python的smtplib和email库构建一个支持图文混排,多附件,多... 目录前言一、基础配置:搭建邮件发送框架1.1 邮箱服务准备1.2 核心库导入1.3 基础发送函数二、

基于Python实现一个图片拆分工具

《基于Python实现一个图片拆分工具》这篇文章主要为大家详细介绍了如何基于Python实现一个图片拆分工具,可以根据需要的行数和列数进行拆分,感兴趣的小伙伴可以跟随小编一起学习一下... 简单介绍先自己选择输入的图片,默认是输出到项目文件夹中,可以自己选择其他的文件夹,选择需要拆分的行数和列数,可以通过