可以 选择批量图片添加水印
效果图如下所示
代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Imaging;
namespace 水印
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
string[] FileList;
/// <summary>
/// 所有选中的图片的文件路径获取到
/// </summary>
string dirFilePath;
private void button1_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
listBox1.Items.Clear();
//获取所有选中的文件的文件名
FileList = openFileDialog1.FileNames;
dirFilePath = FileList[0].ToString().Remove(FileList[0].ToString().LastIndexOf("\\"));
for (int i = 0; i < FileList.Length; i++)
{
string ImgPath = FileList[i].ToString();
FileInfo info = new FileInfo(ImgPath);
if (info.Extension.ToLower() == ".png" || info.Extension.ToLower() == ".jpg" || info.Extension.ToLower() == ".jpeg" || info.Extension.ToLower() == ".gif" || info.Extension.ToLower() == ".bmp")
{
listBox1.Items.Add(info.Name);
}
}
}
}
FontFamily fontFamily = null;
FontStyle fontStyle = FontStyle.Regular;
float emSize = 8;
Color fontColor = Color.Black;
private void button2_Click(object sender, EventArgs e)
{
fontDialog1.ShowHelp = false;
fontDialog1.ShowColor = true;
if (fontDialog1.ShowDialog() == DialogResult.OK)
{
fontFamily = fontDialog1.Font.FontFamily;
fontStyle = fontDialog1.Font.Style;
emSize = fontDialog1.Font.Size;
fontColor = fontDialog1.Color;
AddFontWaterMark("", 0);
}
}
string waterMark = "";
Font font;
Brush brush;
int FWidth;
int FHeight;
private void AddFontWaterMark(string imgName, int i)
{
//水印文字
brush = new SolidBrush(fontColor);
waterMark = textbox1.Text.Trim();
//创建的预览图片
Bitmap bt = new Bitmap(200, 50);
Graphics g = Graphics.FromImage(bt);
g.Clear(Color.Cyan);
font = new Font("宋体", emSize, fontStyle);
SizeF maxsize = g.MeasureString(waterMark, font);
FWidth = (int)maxsize.Width;
FHeight = (int)maxsize.Height;
g.DrawString(waterMark, font, brush, (int)(bt.Width - FWidth) / 2, (int)(bt.Height - FHeight) / 2);
pictureBox1.Image = bt;
pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;
if (i == 1)
{
string fullpath = dirFilePath + "\\" + imgName;
//创建的添加水印的图片
Bitmap targetlmg = new Bitmap(Image.FromFile(fullpath));
Graphics graphics = Graphics.FromImage(targetlmg);
graphics.DrawString(waterMark, font, brush, (targetlmg.Width - FWidth) / 2, (targetlmg.Height - FHeight) / 2);
FileInfo file = new FileInfo(fullpath);
string hou = file.Extension;
//保存每张图片,原图片的格式必须保证一致
if (hou.ToLower() == ".jpg" || hou.ToLower() == ".jpeg")
{
targetlmg.Save(txtNewPath.Text + "\\_" + file.Name, ImageFormat.Jpeg);
}
else if (hou.ToLower() == ".png")
{
targetlmg.Save(txtNewPath.Text + "\\_" + file.Name, ImageFormat.Png);
}
}
}
private void textbox1_TextChanged(object sender, EventArgs e)
{
if (listBox1.Items.Count <= 0)
{
if (MessageBox.Show("请先载入图片", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning) == DialogResult.OK)
{
textbox1.Text = "";
}
else
{
AddFontWaterMark("", 0);
}
}
}
private void button3_Click(object sender, EventArgs e)
{
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
txtNewPath.Text = folderBrowserDialog1.SelectedPath;
}
}
private void button4_Click(object sender, EventArgs e)
{
if (txtNewPath.Text != "" && textbox1.Text != "")
{
for (int i = 0; i < listBox1.Items.Count; i++)
{
AddFontWaterMark(listBox1.Items[i].ToString(), 1);
}
MessageBox.Show("添加水印成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
}
}