场景

项目运行效果

注:

博客主页:
https://blog.csdn.net/badao_liumang_qizhi
关注公众号
霸道的程序猿
获取编程相关电子书、教程推送与免费下载。

实现

新建一个窗体页面,设计页面布局如下

其中图片列表是ListBox,透明度设置是TrackBar,水印位置是ComboBox,下面窗口是PictureBox。

首先加载图片按钮的点击事件中

private void btnLoadImg_Click(object sender, EventArgs e){if (openFileDialog1.ShowDialog() == DialogResult.OK){lbImgList.Items.Clear();ImgArray = openFileDialog1.FileNames;string ImgP = ImgArray[0].ToString();ImgP = ImgP.Remove(ImgP.LastIndexOf("\\"));ImgDirectoryPath = ImgP;for (int i = 0; i < ImgArray.Length; i++){string ImgPath = ImgArray[i].ToString();string ImgName = ImgPath.Substring(ImgPath.LastIndexOf("\\") + 1, ImgPath.Length - ImgPath.LastIndexOf("\\") - 1);lbImgList.Items.Add(ImgName);}tsslStatus.Text = "图片总数:" + lbImgList.Items.Count;}}

预览按钮的点击事件中,需要显示预览页面,首先新建一个预览页面Form2,页面上放一个PictureBox

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace IMGwatermark
{public partial class Form2 : Form{public Form2(){InitializeComponent();}public Image ig = null;private void Form2_Load(object sender, EventArgs e){pictureBox1.Image = ig;}}
}

在预览按钮的点击事件中

private void btnPreview_Click(object sender, EventArgs e){if (lbImgList.Items.Count > 0){if (rbTxt.Checked){if (txtWaterMarkFont.Text != "" && txtSavaPath.Text.Trim() != ""){AddFontWatermark(txtWaterMarkFont.Text.Trim(), lbImgList.Items[0].ToString(), 0);Form2 frm2 = new Form2();frm2.ig = BigBt;frm2.ShowDialog();}}else{if (txtWaterMarkImg.Text != "" && txtSavaPath.Text != ""){ChangeAlpha();AddFontWatermark(txtWaterMarkFont.Text.Trim(), lbImgList.Items[0].ToString(), 2);Form2 frm2 = new Form2();frm2.ig = BigBt;frm2.ShowDialog();}}}}

在字体设置的点击事件中

private void button1_Click(object sender, EventArgs e){if (lbImgList.Items.Count > 0){fontDialog1.ShowColor = true;fontDialog1.ShowHelp = false;fontDialog1.ShowApply = false;if (fontDialog1.ShowDialog() == DialogResult.OK){FontF = fontDialog1.Font.FontFamily;fontColor = fontDialog1.Color;fontSize = fontDialog1.Font.Size;fontStyle = fontDialog1.Font.Style;AddFontWatermark(txtWaterMarkFont.Text.Trim(), lbImgList.Items[0].ToString(), 0);pbImgPreview.Image = bt;}}}

开始执行的点击事件中

private void btnPerform_Click(object sender, EventArgs e){if (rbTxt.Checked&&txtSavaPath.Text!=""&&txtWaterMarkFont.Text!=""){for (int i = 0; i < lbImgList.Items.Count; i++){AddFontWatermark(txtWaterMarkFont.Text.Trim(), lbImgList.Items[i].ToString(), 1);}MessageBox.Show("添加水印成功","提示",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);}if (rbPIC.Checked && txtSavaPath.Text != "" && pbImgPreview.Image != null){for (int i = 0; i < lbImgList.Items.Count; i++){AddFontWatermark(txtWaterMarkFont.Text.Trim(), lbImgList.Items[i].ToString(),3);}MessageBox.Show("添加水印成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);}}

完整示例代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Text;
using System.Drawing.Imaging;
namespace IMGwatermark
{public partial class Form1 : Form{public Form1(){InitializeComponent();}#region 获取系统字体private void GetSystemFont(ToolStripComboBox cb){InstalledFontCollection myFont = new InstalledFontCollection();foreach (FontFamily ff in myFont.Families){cb.Items.Add(ff.Name);}cb.SelectedItem = "宋体";}#endregionprivate void Form1_Load(object sender, EventArgs e){cbbPosition.SelectedIndex = 0;}string [] ImgArray=null;string ImgDirectoryPath=null;private void btnLoadImg_Click(object sender, EventArgs e){if (openFileDialog1.ShowDialog() == DialogResult.OK){lbImgList.Items.Clear();ImgArray = openFileDialog1.FileNames;string ImgP = ImgArray[0].ToString();ImgP = ImgP.Remove(ImgP.LastIndexOf("\\"));ImgDirectoryPath = ImgP;for (int i = 0; i < ImgArray.Length; i++){string ImgPath = ImgArray[i].ToString();string ImgName = ImgPath.Substring(ImgPath.LastIndexOf("\\") + 1, ImgPath.Length - ImgPath.LastIndexOf("\\") - 1);lbImgList.Items.Add(ImgName);}tsslStatus.Text = "图片总数:" + lbImgList.Items.Count;}}private void lbImgList_SelectedIndexChanged(object sender, EventArgs e){if (lbImgList.SelectedItems.Count > 0){tsslText.Text = "图片位置:" + ImgDirectoryPath + "\\" + lbImgList.SelectedItems[0].ToString();}}private void btnPreview_Click(object sender, EventArgs e){if (lbImgList.Items.Count > 0){if (rbTxt.Checked){if (txtWaterMarkFont.Text != "" && txtSavaPath.Text.Trim() != ""){AddFontWatermark(txtWaterMarkFont.Text.Trim(), lbImgList.Items[0].ToString(), 0);Form2 frm2 = new Form2();frm2.ig = BigBt;frm2.ShowDialog();}}else{if (txtWaterMarkImg.Text != "" && txtSavaPath.Text != ""){ChangeAlpha();AddFontWatermark(txtWaterMarkFont.Text.Trim(), lbImgList.Items[0].ToString(), 2);Form2 frm2 = new Form2();frm2.ig = BigBt;frm2.ShowDialog();}}}}private void trackBar1_Enter(object sender, EventArgs e){lbImgList.Focus();}private void btnSelect_Click(object sender, EventArgs e){if (openFileDialog2.ShowDialog() == DialogResult.OK){txtWaterMarkImg.Text = openFileDialog2.FileName;if (rbPIC.Checked == true){ChangeAlpha();pbImgPreview.Image = Image.FromFile(txtWaterMarkImg.Text.Trim());}}}private void rbTxt_CheckedChanged(object sender, EventArgs e){trackBar1.Enabled = false;if(rbPIC.Checked)pbImgPreview.Image = null;}private void rbPIC_CheckedChanged(object sender, EventArgs e){trackBar1.Enabled = true;if (rbTxt.Checked){pbImgPreview.Image = null;}}private void button1_Click(object sender, EventArgs e){if (lbImgList.Items.Count > 0){fontDialog1.ShowColor = true;fontDialog1.ShowHelp = false;fontDialog1.ShowApply = false;if (fontDialog1.ShowDialog() == DialogResult.OK){FontF = fontDialog1.Font.FontFamily;fontColor = fontDialog1.Color;fontSize = fontDialog1.Font.Size;fontStyle = fontDialog1.Font.Style;AddFontWatermark(txtWaterMarkFont.Text.Trim(), lbImgList.Items[0].ToString(), 0);pbImgPreview.Image = bt;}}}Bitmap bt=null;float fontSize = 8;Color fontColor=Color.Black;FontFamily FontF = null;FontStyle fontStyle = FontStyle.Regular;int Fwidth;int Fheight;Bitmap BigBt;Font f;Brush b;private void AddFontWatermark(string txt,string Iname,int i)//预览{b = new SolidBrush(fontColor);bt = new Bitmap(368,75);BigBt = new Bitmap(Image.FromFile(ImgDirectoryPath + "\\" +Iname));Graphics g = Graphics.FromImage(bt);Graphics g1 = Graphics.FromImage(BigBt);g.Clear(Color.Gainsboro);pbImgPreview.Image = bt;if (FontF == null){f = new Font(txt,fontSize);SizeF XMaxSize = g.MeasureString(txt, f);Fwidth = (int)XMaxSize.Width;Fheight = (int)XMaxSize.Height;g.DrawString(txt,f, b, (int)(368 - Fwidth) / 2, (int)(75 - Fheight) / 2);if (cbbPosition.SelectedIndex==0)//正中{g1.DrawString(txt, f, b, (int)(BigBt.Width - Fwidth) / 2, (int)(BigBt.Height - Fheight) / 2);}if (cbbPosition.SelectedIndex == 1)//左上{g1.DrawString(txt, f, b, 30,30);}if (cbbPosition.SelectedIndex ==2)//左下{g1.DrawString(txt, f, b, 30, (int)(BigBt.Height - Fheight)-30);}if (cbbPosition.SelectedIndex == 3) //右上{g1.DrawString(txt, f, b, (int)(BigBt.Width - Fwidth), 30);}if (cbbPosition.SelectedIndex == 4)//右下{g1.DrawString(txt, f, b, (int)(BigBt.Width - Fwidth), (int)(BigBt.Height - Fheight)-30);}}else{f = new Font(FontF, fontSize, fontStyle);SizeF XMaxSize = g.MeasureString(txt, f);Fwidth = (int)XMaxSize.Width;Fheight = (int)XMaxSize.Height;g.DrawString(txt, new Font(FontF, fontSize, fontStyle), b, (int)(368 - Fwidth) / 2, (int)(75 - Fheight) / 2);if (cbbPosition.SelectedIndex == 0)//正中{g1.DrawString(txt, new Font(FontF, fontSize, fontStyle), b, (int)(BigBt.Width - Fwidth) / 2, (int)(BigBt.Height - Fheight) / 2);}if (cbbPosition.SelectedIndex == 1)//左上{g1.DrawString(txt, new Font(FontF, fontSize, fontStyle), b, 30, 30);}if (cbbPosition.SelectedIndex == 2)//左下{g1.DrawString(txt, new Font(FontF, fontSize, fontStyle), b, 30, (int)(BigBt.Height - Fheight)-30);}if (cbbPosition.SelectedIndex == 3) //右上{g1.DrawString(txt, new Font(FontF, fontSize, fontStyle), b, (int)(BigBt.Width - Fwidth), Fheight);}if (cbbPosition.SelectedIndex == 4)//右下{g1.DrawString(txt, new Font(FontF, fontSize, fontStyle), b, (int)(BigBt.Width - Fwidth), (int)(BigBt.Height - Fheight)-30);}}if (i == 1){string ipath;if (NewFolderPath.Length == 3)ipath = NewFolderPath.Remove(NewFolderPath.LastIndexOf(":") + 1);elseipath = NewFolderPath;string imgstype = Iname.Substring(Iname.LastIndexOf(".") + 1, Iname.Length - 1 - Iname.LastIndexOf("."));if (imgstype.ToLower() == "jpeg" || imgstype.ToLower() == "jpg"){BigBt.Save(ipath + "\\_" + Iname, ImageFormat.Jpeg);}if (imgstype.ToLower() == "png"){BigBt.Save(ipath + "\\_" + Iname, ImageFormat.Png);}if (imgstype.ToLower() == "bmp"){BigBt.Save(ipath + "\\_" + Iname, ImageFormat.Bmp);}if (imgstype.ToLower() == "gif"){BigBt.Save(ipath + "\\_" + Iname, ImageFormat.Gif);}g1.Dispose();BigBt.Dispose();}if (i == 2){if (cbbPosition.SelectedIndex == 0)//正中{g1.DrawImage(effect, (int)(BigBt.Width - effect.Width) / 2, (int)(BigBt.Height - effect.Height) / 2);}if (cbbPosition.SelectedIndex == 1)//左上{g1.DrawImage(effect, 30, 30);}if (cbbPosition.SelectedIndex == 2)//左下{g1.DrawImage(effect, 30, (int)(BigBt.Height - effect.Height) - 30);}if (cbbPosition.SelectedIndex == 3) //右上{g1.DrawImage(effect, (int)(BigBt.Width - effect.Width)-30, 30);}if (cbbPosition.SelectedIndex == 4)//右下{g1.DrawImage(effect, (int)(BigBt.Width - effect.Width)-30, (int)(BigBt.Height - effect.Height) - 30);}}if (i == 3){if (cbbPosition.SelectedIndex == 0)//正中{g1.DrawImage(effect, (int)(BigBt.Width - effect.Width) / 2, (int)(BigBt.Height - effect.Height) / 2);}if (cbbPosition.SelectedIndex == 1)//左上{g1.DrawImage(effect, 30, 30);}if (cbbPosition.SelectedIndex == 2)//左下{g1.DrawImage(effect, 30, (int)(BigBt.Height - effect.Height) - 30);}if (cbbPosition.SelectedIndex == 3) //右上{g1.DrawImage(effect, (int)(BigBt.Width - effect.Width), 30);}if (cbbPosition.SelectedIndex == 4)//右下{g1.DrawImage(effect, (int)(BigBt.Width - effect.Width), (int)(BigBt.Height - effect.Height) - 30);}string ipath;if (NewFolderPath.Length == 3)ipath = NewFolderPath.Remove(NewFolderPath.LastIndexOf(":") + 1);elseipath = NewFolderPath;string imgstype = Iname.Substring(Iname.LastIndexOf(".") + 1, Iname.Length - 1 - Iname.LastIndexOf("."));if (imgstype.ToLower() == "jpeg" || imgstype.ToLower() == "jpg"){BigBt.Save(ipath + "\\_" + Iname, ImageFormat.Jpeg);}if (imgstype.ToLower() == "png"){BigBt.Save(ipath + "\\_" + Iname, ImageFormat.Png);}if (imgstype.ToLower() == "bmp"){BigBt.Save(ipath + "\\_" + Iname, ImageFormat.Bmp);}if (imgstype.ToLower() == "gif"){BigBt.Save(ipath + "\\_" + Iname, ImageFormat.Gif);}}}private void txtWaterMarkFont_TextChanged(object sender, EventArgs e){if (lbImgList.Items.Count <= 0){MessageBox.Show("请加载图片","警告",MessageBoxButtons.OK,MessageBoxIcon.Error);return;}else{AddFontWatermark(txtWaterMarkFont.Text.Trim(), lbImgList.Items[0].ToString(), 0);pbImgPreview.Image = bt;}}private void btnExit_Click(object sender, EventArgs e){Application.Exit();}/// <summary>/// 调节透明度/// </summary>/// <param name="sender"></param>/// <param name="e"></param>Bitmap effect;Bitmap source;Image new_img;private void ChangeAlpha(){pbImgPreview.Refresh();source = new Bitmap(Image.FromFile(txtWaterMarkImg.Text.Trim()));if (source.Width <= 368)effect = new Bitmap(368, 75);else{Image.GetThumbnailImageAbort callb = null;//对水印图片生成缩略图,缩小到原图得1/4new_img = source.GetThumbnailImage(source.Width / 4, source.Width / 4, callb, new System.IntPtr());effect = new Bitmap(this.new_img.Width, this.new_img.Height);}Graphics _effect = Graphics.FromImage(effect);float[][] matrixItems ={new float[]{1,0,0,0,0},new float [] {0,1,0,0,0},new float []{0,0,1,0,0},new float []{0,0,0,0,0},new float[]{0,0,0,trackBar1.Value/255f,1}};ColorMatrix imgMatrix = new ColorMatrix(matrixItems);ImageAttributes imgEffect = new ImageAttributes();imgEffect.SetColorMatrix(imgMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);if (source.Width <= 368){_effect.DrawImage(source, new Rectangle(0, 0, 368, 75), 0, 0, 368, 75, GraphicsUnit.Pixel, imgEffect);}else{_effect.DrawImage(new_img, new Rectangle(0, 0, new_img.Width, new_img.Height), 0, 0, new_img.Width, new_img.Height, GraphicsUnit.Pixel, imgEffect);}pbImgPreview.Image = effect;}private void trackBar1_ValueChanged(object sender, EventArgs e){if(rbPIC.Checked&&txtWaterMarkImg.Text.Trim()!="")ChangeAlpha();}private void btnPerform_Click(object sender, EventArgs e){if (rbTxt.Checked&&txtSavaPath.Text!=""&&txtWaterMarkFont.Text!=""){for (int i = 0; i < lbImgList.Items.Count; i++){AddFontWatermark(txtWaterMarkFont.Text.Trim(), lbImgList.Items[i].ToString(), 1);}MessageBox.Show("添加水印成功","提示",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);}if (rbPIC.Checked && txtSavaPath.Text != "" && pbImgPreview.Image != null){for (int i = 0; i < lbImgList.Items.Count; i++){AddFontWatermark(txtWaterMarkFont.Text.Trim(), lbImgList.Items[i].ToString(),3);}MessageBox.Show("添加水印成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);}}string NewFolderPath;private void button2_Click(object sender, EventArgs e){if (folderBrowserDialog1.ShowDialog() == DialogResult.OK){txtSavaPath.Text = folderBrowserDialog1.SelectedPath;NewFolderPath = txtSavaPath.Text.Trim();}}}
}

代码下载

https://download.csdn.net/download/BADAO_LIUMANG_QIZHI/12241196

Winform中实现对照片添加文字和图片水印(附代码下载)相关推荐

  1. php给图片加图片水印,php给图片添加文字或图片水印实现代码

    原标题:php给图片添加文字或图片水印实现代码 一.文字水印 文字水印就是在图片上加上文字,主要使用gd库的imagefttext方法,并且需要字体文件.效果图如下: $dst_path = 'dst ...

  2. 如何让图片在html中自动填充颜色,如何使用传图水印功能在图片上传时自动添加文字或图片水印?...

    下面我们来了解一下小蚂蚁编辑器的功能"传图水印"!开启传图水印功能后,所有上传的图片会自动添加文字或图片水印,并且支持自定义设置水印的展现形式. 一.开启传图水印 ① 鼠标移动到首 ...

  3. php图片写入带问号_php实现图片上传时添加文字和图片水印技巧

    本文实现的功能特别适用于一些商城和图片站中,分享了图片在上传时添加文字和图片水印的技巧,供大家参考,具体内容如下 1. water.class.php header('Content-Type:tex ...

  4. 手把手教你如何通过Java给图片添加文字和图片水印

    本文首发于个人网站 前言 最近工作上有个需求,动态生成一张图片,具体来说就是基于模版图片动态添加文字和图片(文字内容不同,图片数目不同),其中文字大小不全一样,且对位置有所要求. 本文将剖析多个技术方 ...

  5. html图片加文字批量处理,图片批量加水印工具,图片批量添加文字|图片同时添加文字或图片水印...

    一般在网上下载的图片都会自动带有相应网站的文字或是图片水印,虽然可能在使用图片素材的时候,图片上的水印会在一定程度上影响美观,但是水印是对于版权或者是原创的一个保护,图片水印不仅是可以保护别人的原创图 ...

  6. Python代码学习之给图片添加文字或图片水印

    前言 图片加水印有什么好处?在现今的数字化时代,网络上的图片泛滥,盗图现象也越来越严重.因此,在发布文章时,为了保护自己的原创作品版权,很多人选择使用水印来保护他们的图片.这样就能更好地做到: 1.版 ...

  7. thinkphp5.1 || 给图片添加文字,图片水印

    composter下载扩展: https://packagist.org/packages/aliyuncs/oss-sdk-php composer require topthink/think-i ...

  8. acrobat给pdf加多行水印_批量pdf如何添加水印 多个pdf批量加相同水印的方法|支持同时添加文字、图片水印...

    本次还是给大家讲讲pdf文件的编辑转换处理,毕竟pdf文件也是在办公学习上是很常用到的,工作时很经常会遇到多个pdf文件要处理的情况,比如说制作好了一系列的pdf文档,需要上传到网络或者传输给他人查阅 ...

  9. php imagick 水印,Imagick库图片添加文字及图片水印

    在上一篇文章中,我们介绍了Imagick库的安装: 这篇文章我们介绍一下Imagick库的基本使用: 如何给图片添加渐变水印,及图片水印. 首先我们要生成一张透明底的画布: // 创建新的画布对象和透 ...

最新文章

  1. python openstack究竟能干嘛_openstack是什么,能干什么
  2. 如何在ftp服务器下查找文件夹,查找ftp服务器下的文件夹名
  3. 【Python3网络爬虫开发实战】1.5.2-PyMongo的安装
  4. 多线程面试体系列(13):多线程同步内功心法——PV操作下
  5. 6 个实用的 Code Review 实践技巧
  6. .NET 基础一步步一幕幕[out、ref、params]
  7. 自己动手写cpu pdf_教你自己动手组装电脑(第一篇:CPU)
  8. 8cm等于多少像素_像素和厘米的换算
  9. 运筹说 第19期 | 线性规划经典例题讲解
  10. 如何区分零线和地线,及其相关理解
  11. 【AOP】面向切面谈恋爱(一)|学会了AOP,他最终收获了爱情
  12. guitar chord html5,‎App Store 上的“吉他和弦(基本): GUITAR CHORD”
  13. 中国传媒大学GPA算法
  14. 软件测试的艺术 读书笔记完整版
  15. 攻防世界MISC练习区(SimpleRAR、base64stego、功夫再高也怕菜刀)
  16. Win10+Debian11双系统的配置小记
  17. web前端开发笔试题
  18. Linux C语言自己动手写日志生成函数
  19. 响应号召!中国北斗+国产GIS 打好基础软件国产化攻坚战
  20. antV/L7@1.3.20-」实现北京地图+散点图

热门文章

  1. SpringBoot 序列化与反序列化日期类型参数
  2. SHA1加密(简单)
  3. Hashtable TreeMap HashMap LinkedHashMap的区别
  4. 「后端小伙伴来学前端了」Element修改默认样式 | 记录自己学习前端踩坑日记
  5. 谷歌浏览器怎么重发请求_chrome 浏览器的预提取资源机制导致的一个请求发送两次的问题以及ClientAbortException异常...
  6. mysql 降序_MySQL 8 新特性之降序索引底层实现
  7. 点击事件调用匿名函数如何传参_事件发布/订阅模式的简单实现
  8. python中字典的常用函数_python中得字典和常用函数总结
  9. centeros7 mysql,center os 7 Mysql 安装
  10. wpf 如何设置弹出窗口必须关闭才能打开其他软件_新电脑到手后,这3项定要这样设置,能让你的电脑多用三年!...