方法1,使用自带的画刷进行绘制水印
示例代码
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string photoPath=string.Empty;
Bitmap image = null;
private void button1_Click(object sender, EventArgs e) //选择照片
{
OpenFileDialog dialog = new OpenFileDialog(); //打开对话框
dialog.Filter = "|*.jpg"; //打开文件过滤器
if (dialog.ShowDialog() == DialogResult.OK)
{
photoPath = dialog.FileName; //拿到用户选择文件的全路径
pictureBox1.Image = System.Drawing.Image.FromFile(photoPath) ; //显示照片
}
}
private void button2_Click(object sender, EventArgs e) //添加水印
{
image = new Bitmap(photoPath);
Graphics graphics = Graphics.FromImage(image);
string watermarkText = "测试水印"+DateTime.Now;
Font font = new Font("宋体", 12);
Brush brush = new SolidBrush(Color.Red);
Point position = new Point(10, 10); //显示的初始位置
graphics.DrawString(watermarkText, font, brush, position);
graphics.Dispose(); //释放资源
pictureBox1.Image = image;
}
private void button3_Click(object sender, EventArgs e) //保存带水印照片
{
SaveFileDialog saveFileDialog = new SaveFileDialog(); //保存文件对话框
saveFileDialog.Filter = "照片|*.jpg"; //保存的文件格式
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
string filePath = saveFileDialog.FileName; // 获取用户选择的文件全路径
image.Save(filePath);
}
}
}
前台界面