C# (Graphics绘制)图片批量打水印 批量保存导出小工具

Posted 生产队的驴.

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C# (Graphics绘制)图片批量打水印 批量保存导出小工具相关的知识,希望对你有一定的参考价值。

效果图:

界面图:

非常适合一些写文章的公众号等…防止图片给偷…几百张图 一两分钟就可以搞定


公共的对象:

 List<string> name = new List<string>(); 
 //保存路径
        string[] photopath;
//保存路径(因为列表不能直接通过点击数组来获取对象) 
        int hight;
//图片的高度        
        int wide;
 //图片的宽度           

图片导入列表:

导入按钮是一个标签 ,列表控件是一个ListBox
点击导入时跳出 文件选择对话框

选择对话框:

                OpenFileDialog filedialog = new OpenFileDialog();
                //创建打开对话框对象
                filedialog.Filter = "图片|*.jpg*|图片|*.png";
                //图片的类型
                filedialog.Multiselect = true;
                //是否可以多选
                filedialog.ShowDialog();
                //显示对话框
                photopath = filedialog.FileNames;
                //文件路径 路径都放在字符串数组里
             
                if (filedialog.FileName=="")
                
              //是否选择了图片
                    return;
                  //没有选择就返回不然抛异常
                

把选择的图片路径都导入 列表
图片导入列表:

 for (int i = 0; i < photopath.Length; i++)
    
    //循环添加图片
    
        if (listBox1.Items.Contains(Path.GetFileNameWithoutExtension(photopath[i])))
        
     //判断图片是否存在   

            continue;
     //如果已存在就跳过 本次循环
        
        name.Add(photopath[i]);
  //添加路径到集合(因为后面需要通过点击显示图片)
       listBox1.Items.Add(Path.GetFileNameWithoutExtension(photopath[i]));
 // Paht的方法是把路径转成 图片名称  添加进列表控件
    
   listBox1.SelectedIndex = 0;
 //图片默认显示为第一张图片(从0开始)
    filedialog.Dispose();
    //释放内存

文字颜色对话框:

 ColorDialog color = new ColorDialog();       
            color.ShowDialog();
            label4.ForeColor = color.Color;

图片打水印:
方便使用 写成了一个方法

public void size(string text, int fontsize, Color color, string textefect, int x, int y)       
      //文字内容     文字大学       文字颜色      文字字体        x位置 
        

            try
            
                //  Font f = new Font(textefect, fontsize,FontStyle.Regular);
                Font f = new Font(textefect, fontsize, FontStyle.Regular);
                 //设置文字         内容      大小       字体
                Point p = new Point(x, y);
                 //文字的文字 x  y
                Color c = color;
              //颜色
                Brush b = new SolidBrush(c);
               //颜色刷子
               
                Image im = Image.FromFile(name[listBox1.SelectedIndex]);
               //创建图片对象 来源图片路径 路径为列表点击的图片


                Graphics gp = Graphics.FromImage(im);
            //创建绘制对象
                gp.DrawString(text, f, b, p);
            // 绘制文字        内容 文字 颜色  位置
                pictureBox1.Image = im;
          //绘制好显示在图片控件
          
                hight = im.Height;
                //获取图片高度
                wide = im.Width;
                // //获取图片宽度                 
                label9.Text = "高度:" +hight.ToString();
                label10.Text = "宽度:"+ wide.ToString();

                // im.Dispose();
                gp.Dispose();
                 //释放内存
        
                //保存图片
                label5.Enabled = true;
                GC.Collect();
                //释放内存
            
            catch  
            

点击图片显示:

点击图片时 显示在PictureBox控件上

直接调用方法就可以了


 pictureBox1.Image = Image.FromFile(name[listBox1.SelectedIndex]);
 size(textBox1.Text, Convert.ToInt32(textBox2.Text), label4.ForeColor, comboBox1.Text, Convert.ToInt32(textBox3.Text), Convert.ToInt32(textBox4.Text));

批量导出图片:

try
    
    string selectpath;
    //选择保存的路径
    FolderBrowserDialog f = new FolderBrowserDialog();
    //打开对话框
    f.ShowDialog();
    //显示对话框
    selectpath = f.SelectedPath + "\\\\"; 
    label7.Text = "批量保存路径:" + selectpath;
    //======================================
    listBox1.SelectedIndex = 0;
    //默认选择第一张图片
    
    progressBar1.Maximum = name.Count;
    //进度条 最大值
    
    for (int i = 0; i < name.Count; i++)
    
        listBox1.SelectedIndex = i;
        //循环来保存
        Bitmap b = new Bitmap(pictureBox1.Image);
        //保存对象
        Bitmap save = new Bitmap(b, wide, hight);
        //保存             保存的图片  宽带 高度
        save.Save(selectpath + Path.GetRandomFileName() + ".jpg");
        //保存 并且随机生成文件名 
        progressBar1.Value += 1;
    
   MessageBox.Show("完成");
    progressBar1.Value = 0;
    //进度条值 为0

catch  

单个保存:

 try
            
       SaveFileDialog save = new SaveFileDialog();
     save.Filter = "图片|*.jpg|图片|*.png";
       save.ShowDialog();
     Bitmap b = new Bitmap(pictureBox1.Image);

     Bitmap filesvae = new Bitmap(b, wide, hight);

    filesvae.Save(save.FileName);

            
            catch 
            

完整代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TextPhoto

    public partial class Form1 : Form
    
        public Form1()
        
            InitializeComponent();
        

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        

           
            try
            
                pictureBox1.Image = Image.FromFile(name[listBox1.SelectedIndex]);
                size(textBox1.Text, Convert.ToInt32(textBox2.Text), label4.ForeColor, comboBox1.Text, Convert.ToInt32(textBox3.Text), Convert.ToInt32(textBox4.Text));
            
            catch  
        

        List<string> name = new List<string>(); 
        string[] photopath;
        int hight;
        int wide;


        private void label1_Click(object sender, EventArgs e)
        
            try
            
                OpenFileDialog filedialog = new OpenFileDialog();
                filedialog.Filter = "图片|*.jpg*|图片|*.png";
                filedialog.Multiselect = true;
                filedialog.ShowDialog();
                photopath = filedialog.FileNames;//文件路径


                if (filedialog.FileName=="")
                

                    return;
                





                for (int i = 0; i < photopath.Length; i++)
                
                    if (listBox1.Items.Contains(Path.GetFileNameWithoutExtension(photopath[i])))
                    

                        continue;
                    

                    name.Add(photopath[i]);
                    listBox1.Items.Add(Path.GetFileNameWithoutExtension(photopath[i]));

                

                listBox1.SelectedIndex = 0;



                filedialog.Dispose();

            
            catch  


            //内容
            textBox1.Enabled = true;

            textBox2.Enabled = true;

            comboBox1.Enabled = true;


            label4.Enabled = true;

            label8.Enabled = true;
            textBox3.Enabled = true;
            textBox4.Enabled = true;
            label11.Enabled = true;
            label12.Enabled = true;

            label13.Text = "数量:"+name.Count; 

        

   

        public void size(string text, int fontsize, Color color, string textefect, int x, int y)
        

            try
            
                //  Font f = new Font(textefect, fontsize,FontStyle.Regular);
                Font f = new Font(textefect, fontsize, FontStyle.Regular);

                Point p = new Point(x, y);

                Color c = color;

                Brush b = new SolidBrush(c);

                Image im = Image.FromFile(name[listBox1.SelectedIndex]);



                Graphics gp = Graphics.FromImage(im);

                gp.DrawString(text, f, b, p);

                pictureBox1.Image = im;


                hight = im.Height;
                wide = im.Width;

            //    SizeF font = gp.MeasureString(text,f);

                label9.Text = "高度:" +hight.ToString();
                label10.Text = "宽度:"+ wide.ToString();

                // im.Dispose();
                gp.Dispose();

             //   fonthight = (int)font.Height;
              //  fonthight = (int)font.Width;

                //保存图片
                label5.Enabled = true;
                GC.Collect();
            
            catch  
            
            

        private void Form1_Load(object sender, EventArgs e)
        

            comboBox1.Items.Add("微软雅黑");
            comboBox1.Items.Add("宋体");
                comboBox1.Items.Add("黑体");
                comboBox1.Items.Add("楷体");
                comboBox1.Items.Add("新宋体");
                comboBox1.Items.Add("等线");
                comboBox1.Items.Add("新宋体");
             
        

 
        private void label4_Click(object sender, EventArgs e)
        
            ColorDialog color = new ColorDialog();
            //  color.AllowFullOpen = false;
           // color.FullOpen = false;
          //  color.SolidColorOnly = true;
            color.ShowDialog();
            label4.ForeColor = color.Color;


            try
            
                size(textBox1.Text, Convert.ToInt32(textBox2.Text), label4.ForeColor, comboBox1.Text, Convert.ToInt32(textBox3.Text), Convert.ToInt32(textBox4.Text));
            
            catch 

              
            
        

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        
            try
            
                size(textBox1.Text, Convert.ToInt32(textBox2.Text), label4.ForeColor, comboBox1.Text, Convert.ToInt32(textBox3.Text), Convert.ToInt32(textBox4.Text));
            
            catch  
        

        private void textBox2_TextChanged(object sender, EventArgs e)
        
            try
            
                size(textBox1.Text, Convert.ToInt32(textBox2.Text), label4.ForeColor, comboBox1.Text, Convert.ToInt32(textBox3.Text), Convert.ToInt32(textBox4.Text));
            
            catch  
        

        private void textBox1_TextChanged(object sender, EventArgs e)
        
            try
            
                size(textBox1.Text, Convert.ToInt32(textBox2.Text), label4.ForeColor, comboBox1.Text, Convert.ToInt32(textBox3), Convert.ToInt32(textBox4.Text));
            
            catch 

               // MessageBox.Show("内容是空的");
            
            

        private void label5_Click_1(object sender, EventArgs e)
        
            try
            
                SaveFileDialog save = new SaveFileDialog();
                save.Filter = "图片|*.jpg|图片|*.png";
                save.ShowDialog();
                Bitmap b = new Bitmap(pictureBox1.Image);

                Bitmap filesvae = new Bitmap(b, wide, hight);

                filesvae.Save(save.FileName);

            
            catch 
            
        

      
        private void textBox4_TextChanged(object sender, EventArgs e)
        
            try
            
                size(textBox1.Text, Convert.ToInt32(textBox2.Text), label4.ForeColor, comboBox1.Text, Convert.ToInt32(textBox3.Text), Convert.ToInt32(textBox4.Text));
            
            catch  
        

        private void textBox3_TextChanged_1(object sender, EventArgs e)
        
            try
                           
         
                size(textBox1.Text, Convert.ToInt32(textBox2.TextC# (Graphics绘制)图片批量打水印 批量保存导出小工具

Photoshop 如何自动批量添加图片边框,图片logo, 底部水印

图片怎么快速批量去水印?

如何批量去除图片水印

怎样批量去除水印

如何无痕批量去除图片水印?