给一张图片加水印

可以 选择批量图片添加水印

效果图如下所示

 

代码如下:

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 Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
        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 button2_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;
        private 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 button3_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 != "" && textbox1.Text != "")
            {
                for (int i = 0; i < listBox1.Items.Count; i++)
                {
                    AddFontWaterMark(listBox1.Items[i].ToString(), 1);
                }
                MessageBox.Show("添加水印成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }
    }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值