c#WinForm给图⽚加⽂字⽔印
设计界⾯:
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);//选择绘制的图
htmlbordergraphics.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;//填充拉伸
{
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("请选择路径!或检差原图是否存在!");
}
}
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论