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;
/// <summary>
/// 选择图片
/// </summary>
private void button1_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog()==DialogResult.OK)
{
listBox1.Items.Clear();
//获取所有选中文件的文件名
FileList = openFileDialog1.FileNames;
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;
/// <summary>
/// 字体样式
/// </summary>
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;
//添加水印的方法
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.Gainsboro);
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 targetImg = new Bitmap(Image.FromFile(fullPath));
Graphics graphics = Graphics.FromImage(targetImg);
graphics.DrawString(waterMark, font, brush, (targetImg.Width - FWidth) / 2, (targetImg.Height - FHeight) / 2);
FileInfo file = new FileInfo(fullPath);
string hou = file.Extension;
//保存每张图片,原图片的格式必须保证一致
if (hou.ToLower()==".jpg"|| hou.ToLower()==".jepg")
{
targetImg.Save(textBox2.Text + "\\_" + file.Name, ImageFormat.Jpeg);
}
else if (hou.ToLower() == ".png")
{
targetImg.Save(textBox2.Text + "\\_" + file.Name, ImageFormat.Png);
}
}
}
/// <summary>
/// 输入的水印文字文本框
/// </summary>
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);
}
}
/// <summary>
/// 浏览保存路径
/// </summary>
private void button3_Click(object sender, EventArgs e)
{
if (folderBrowserDialog1.ShowDialog()==DialogResult.OK)
{
textBox2.Text = folderBrowserDialog1.SelectedPath;
}
}
/// <summary>
/// 确定添加按钮
/// </summary>
/// 首先保证选择了图片的保存位置,保证水印内容不能为空
private void button4_Click(object sender, EventArgs e)
{
if (textBox2.Text!="" && textBox1.Text!="")
{
for (int i = 0; i < listBox1.Items.Count; i++)
{
AddFontWaterMark(listBox1.Items[i].ToString(),1);
}
MessageBox.Show("添加水印成功", "提示!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
}
}