设计界面:
cs:
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 Form6 : Form
{
public Form6()
{
InitializeComponent();
}
private void Form6_Load(object sender, EventArgs e)
{
pictureBox1.BorderStyle = BorderStyle.FixedSingle;
pictureBox2.BorderStyle = BorderStyle.FixedSingle;
pictureBox3.BorderStyle = BorderStyle.FixedSingle;
}
string[] FileList;
string dirFilepath;
private void button1_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog()==DialogResult.OK)
{
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")
{
textBox1.Text = info.Name;//获取点击的照片的名字
}
else
{
MessageBox.Show("选择类型有误!请重新选择");
}
}
}
string filepath = openFileDialog1.FileName;//获取其选择的照片的名字
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;//拉伸填充
pictureBox1.ImageLocation = filepath;//展示图片
}
FontFamily fontFamily = null;
FontStyle fontStyle = FontStyle.Regular;//设置字体格式
float size = 10;//字体大小
Color color = Color.Black;//字体颜色
bool x;
private void button2_Click(object sender, EventArgs e)
{
if (pictureBox1.Image == null)
{
x = true;
MessageBox.Show("请先选择照片!");
}
else
{
fontDialog1.ShowHelp = false;//去掉帮助按钮
fontDialog1.ShowColor = true;//进行颜色选择
if (fontDialog1.ShowDialog()==DialogResult.OK)
{
fontFamily = fontDialog1.Font.FontFamily;//应用字体
fontStyle = fontDialog1.Font.Style;//字体边框
size = fontDialog1.Font.Size;//字体大小
color = fontDialog1.Color;//颜色
AddFontWaterMark("",0);
}
}
}
string watermark = "";
Font font;
Brush Brush;
private void AddFontWaterMark(string imgName,int i)
{
//设置水印文字
Brush = new SolidBrush(color);//颜色为选中的颜色
watermark = textBox2.Text.Trim();//移除空白
//创建预览图片
Bitmap bitmap = new Bitmap(200,50);//定义初始大小
Graphics graphics = Graphics.FromImage(bitmap);//选择绘制的图
graphics.Clear(Color.Wheat);//背景颜色
font = new Font("楷体",size,fontStyle);//大小 颜色 字体
SizeF maxsize = graphics.MeasureString(watermark,font);//绘图大小
Width = (int)maxsize.Width;
Height = (int)maxsize.Height;
graphics.DrawString(watermark,font,Brush,0,0);//样式大小位置
pictureBox2.Image = bitmap;//展示图片
pictureBox2.SizeMode = PictureBoxSizeMode.StretchImage;//填充拉伸
if (i==1)
{
string fullpath = dirFilepath + "\\" + imgName;//创建添加水印的照片
Bitmap bitmap1 = new Bitmap(Image.FromFile(fullpath));
Graphics graphics1 = Graphics.FromImage(bitmap1);//重新绘图
graphics1.DrawString(watermark,font,Brush,100,100);//颜色位置都不变
pictureBox3.Image = bitmap1;
pictureBox3.SizeMode = PictureBoxSizeMode.StretchImage;
FileInfo file = new FileInfo(fullpath);//路径
string h = file.Extension;//获取扩展名
//判断扩展名是否一致
if (h.ToLower()==".jpg"||h.ToLower()==".jpeg")
{
bitmap1.Save(txtNewPath.Text+"\\_"+file.Name,ImageFormat.Jpeg);//保存至选择的路径并显示名字
}
else if (h.ToLower()==".png")
{
bitmap1.Save(txtNewPath.Text+"\\_"+file.Name,ImageFormat.Png);
}
}
}
private void button5_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!=""&&textBox2.Text!=""&&pictureBox1.Image!=null)
{
AddFontWaterMark(textBox1.Text,1);
MessageBox.Show("添加成功");
}
else
{
MessageBox.Show("请选择路径!或检差原图是否存在!");
}
}
}
}