設計界面:
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("請選擇路徑!或檢差原圖是否存在!");
}
}
}
}