winform图片添加水印

折月煮酒 提交于 2020-01-28 12:20:24

winform图片添加水印

布局界面:

在这里插入图片描述

代码:

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 WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        string[] filelist;
        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 button3_Click(object sender, EventArgs e)//确认添加图片
        {
            if (textBox2.Text!=""&&textBox1.Text!="")
            {
                for (int i = 0; i < listBox1.Items.Count; i++)
                {
                    addfontwatermark(listBox1.Items[i].ToString(), 1);
                }
                    MessageBox.Show("添加水印成功","提示",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
            }
        }

        private void button2_Click(object sender, EventArgs e)//选择位置
        {
            if (folderBrowserDialog1.ShowDialog()==DialogResult.OK)
            {
                textBox2.Text = folderBrowserDialog1.SelectedPath;
            }
        }

        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 button4_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 fheight;
        int fwidth;
        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.Gray);
            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 targetimg = new Bitmap(Image.FromFile(fullpath));
                Graphics graphics = Graphics.FromImage(targetimg);
                graphics.DrawString(watermark, font, brush, (targetimg.Width - fwidth) / 2, (targetimg.Height - fheight) / 2);
                FileInfo file = new FileInfo(fullpath);
                string hou = file.Extension;
                //必须保持每个图片与原图片一直
                if (hou.ToLower()==".jpg"||hou.ToLower()==".jpeg")
                {
                    targetimg.Save(textBox2.Text + "\\_" + file.Name, ImageFormat.Jpeg);
                }
                else if (hou.ToLower()==".png")
                {
                    targetimg.Save(textBox2.Text + "\\_" + file.Name, ImageFormat.Png);
                }
            }
        }
        private void pictureBox1_Click(object sender, EventArgs e)
        {
            
        }
        private void fontDialog1_Apply(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}

运行结果:

在这里插入图片描述
在这里插入图片描述

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!