winform图片添加水印

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)
        {

        }
    }
}

运行结果:

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

WinForm添加图片水印可以通过以下步骤完成: 1. 首先,我们需要将图片加载到PictureBox控件上,可以使用PictureBox控件的Image属性来实现。例如,可以使用如下代码将图片加载到PictureBox控件上: ``` pictureBox1.Image = Image.FromFile("图片路径"); ``` 2. 接下来,我们需要在图片添加水印。可以通过创建一个位图对象,并使用Graphics对象在位图上绘制水印来实现。例如,可以使用如下代码在图片上绘制文本水印: ``` Bitmap bitmap = new Bitmap(pictureBox1.Image); Graphics graphics = Graphics.FromImage(bitmap); Font font = new Font("宋体", 20, FontStyle.Bold); SolidBrush brush = new SolidBrush(Color.Red); string watermarkText = "这里是水印"; graphics.DrawString(watermarkText, font, brush, new PointF(10, 10)); graphics.Dispose(); pictureBox1.Image = bitmap; ``` 上述代码中,我们首先创建一个位图对象,并使用Graphics.FromImage方法获取位图的Graphics对象。然后,定义一个字体、画刷和水印文本。最后,使用Graphics对象的DrawString方法在位图上绘制水印,并更新PictureBox控件的Image属性为添加水印后的位图。 3. 最后,记得在不需要时释放使用的资源。因为在对图片进行操作时创建了Graphics对象,使用完毕后需要手动释放。可以通过调用Graphics对象的Dispose方法实现。 总结起来,在WinForm添加图片水印,主要是将图片加载到PictureBox控件上,然后创建一个位图对象,在位图上使用Graphics对象绘制水印,最后更新PictureBox控件的Image属性为添加水印后的位图。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IT-wanghanghang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值