窗口设计:
在这里插入图片描述
代码:

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 _05添加水印
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        string[] FileList;
        /// <summary>
        /// 所有选中的图片的文件路径获取到
        /// </summary>
        string dirFilePath;
        private void button1_Click(object sender, EventArgs e)
        {
            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;
        private void btnFontChoose_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.Cyan);
            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 targetlmg = new Bitmap(Image.FromFile(fullpath));
                Graphics graphics = Graphics.FromImage(targetlmg);
                graphics.DrawString(waterMark, font, brush, (targetlmg.Width - FWidth) / 2, (targetlmg.Height - FHeight) / 2);

                FileInfo file = new FileInfo(fullpath);
                string hou = file.Extension;
                //保存每张图片,原图片的格式必须保证一致
                if (hou.ToLower()==".jpg"|| hou.ToLower() == ".jpeg")
                {
                    targetlmg.Save(txtNewPath.Text + "\\_" + file.Name, ImageFormat.Jpeg);
                }
                else if (hou.ToLower() == ".png")
                {
                    targetlmg.Save(txtNewPath.Text + "\\_" + file.Name, ImageFormat.Png);
                }
            }
        }

        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);
                }
            }
        }

        private void BtnLiuLan_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog()==DialogResult.OK)
            {
                txtNewPath.Text = folderBrowserDialog1.SelectedPath;
            }
        }
        //首先保证选择了图片的保存位置,保证水印内容不能为空
        private void Btnadd_Click(object sender, EventArgs e)
        {
            if (txtNewPath.Text!=""&&textBox1.Text!="")
            {
                for (int i=0;i<listBox1.Items.Count;i++)
                {
                    AddFontWaterMark(listBox1.Items[i].ToString(), 1);
                }
                MessageBox.Show("添加水印成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }
    }
}

执行效果:
在这里插入图片描述
点击添加图片,添加要添加水印的图片,选择一张图片打开
在这里插入图片描述
然后设置水印内容并选择字体
在这里插入图片描述
选择图片保存的路劲点击确定添加
在这里插入图片描述

posted on 2019-01-30 18:48  豆皮没有豆  阅读(272)  评论(0)    收藏  举报