C#设置图片水印(案例)

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;
        /// <summary>
        /// 选择图片
        /// </summary>
        private void button1_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog()==DialogResult.OK)
            {
                listBox1.Items.Clear();
                //获取所有选中文件的文件名
                FileList = openFileDialog1.FileNames;
                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;
       /// <summary>
       /// 字体样式
       /// </summary>
        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;
        //添加水印的方法
        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.Gainsboro);
            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()==".jepg")
                {
                    targetImg.Save(textBox2.Text + "\\_" + file.Name, ImageFormat.Jpeg);
                }
                else if (hou.ToLower() == ".png")
                {
                    targetImg.Save(textBox2.Text + "\\_" + file.Name, ImageFormat.Png);
                }
            }
        }
        /// <summary>
        /// 输入的水印文字文本框
        /// </summary>
        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);
            }
        }
        /// <summary>
        /// 浏览保存路径
        /// </summary>
        private void button3_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog()==DialogResult.OK)
            {
                textBox2.Text = folderBrowserDialog1.SelectedPath;
            }
        }
        /// <summary>
        /// 确定添加按钮
        /// </summary>
        /// 首先保证选择了图片的保存位置,保证水印内容不能为空
        private void button4_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);
            }
        }
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值