真爱无限的知识驿站

学习积累技术经验,提升自身能力

C#winform批量给图片加水印代码

//文件下载,里面有exe可直接运行

http://download.csdn.net/detail/pukuimin1226/6001759


//form后台


//加密类注意要引用 System.Web

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO;
using 图片加水印工具.BLL;
namespace 图片加水印工具
{
    public partial class Form1 : Form
    {
        List<string> listExtention = new List<string>();
        public Form1()
        {
            InitializeComponent();
            Form.CheckForIllegalCrossThreadCalls = false;
            listExtention.AddRange(new string[] { ".jpg", ".gif", ".png" });
            ConfigFile.Instanse.fileName = AppDomain.CurrentDomain.BaseDirectory + "图片加水印工具.ini";
        }
       
        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
            Application.ExitThread();
        }
        #region 在新线程中运行函数
        /// <summary>
        /// 在新线程中运行函数
        /// </summary>
        /// <param name="func">传入 函数名(无参、无返回值)</param>
        /// <param name="IsBackground">是否为后台线程(后台线程,窗口关闭后就终止线程)</param>
        public static void ThreadNew(VoidFunction func, bool IsBackground)
        {
            Thread th1 = new Thread(new ThreadStart(func));
            th1.IsBackground = IsBackground;//后台线程,窗口关闭后就终止线程
            th1.Start();
        }
        /// <summary>
        /// 在新线程中运行函数
        /// </summary>
        /// <param name="func">传入 函数名(有一个参数、无返回值)</param>
        /// <param name="para">object参数</param>
        /// <param name="IsBackground">是否为后台线程(后台线程,窗口关闭后就终止线程)</param>
        public static Thread ThreadNew(ParamFunction func, object para, bool IsBackground)
        {
            Thread th1 = new Thread(new ParameterizedThreadStart(func));
            //判断状态
            //((int)th1.ThreadState &((int)ThreadState.Running | (int)ThreadState.Suspended) ) == 0
            th1.IsBackground = IsBackground;
            th1.Start(para);
            return th1;
        }
        /// <summary>
        /// 允许线程之间进行操作
        /// </summary>
        public static void OprateBetweenThread()
        {
            System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
        }
        /// <summary>
        /// 无参的、返回值为void的委托,可以用来做参数名
        /// </summary>
        public delegate void VoidFunction();
        /// <summary>
        /// 有一个参数的、返回值为void的委托,可以用来做参数名
        /// </summary>
        public delegate void ParamFunction(object para);
        #endregion
        private void lb_selectDir_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            if (fbd.ShowDialog() == DialogResult.OK)
            {
                txtDir.Text = fbd.SelectedPath;
                ConfigFile.Instanse["txtDir"] = txtDir.Text;                
            }
        }
        private void lb_selectMark_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            //设置文件类型
            ofd.Filter = "png(*.png)|*.png|jpg(*.jpg)|*.jpg|gif(*.gif)|*.gif";
            //设置默认文件类型显示顺序
            ofd.FilterIndex = 1;
            //保存对话框是否记忆上次打开的目录
            ofd.RestoreDirectory = true;
            //点了保存按钮进入
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                txtMark.Text = ofd.FileName.ToString();
                ConfigFile.Instanse["txtMark"]=txtMark.Text;
            }
        }
        int success = 0; //成功
        int falure = 0; //失败
        int total = 0;
        private void MakeWaterMark()
        {
            success = 0;
            falure = 0;
            total = 0;
            string errmsg = "";
            string markPicPath = txtMark.Text.Trim();
            string strtxtDir = txtDir.Text.Trim();
            if (strtxtDir == "" || markPicPath == "")
            {
                MessageBox.Show("请选择目录和水印文件!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }
            else if (Directory.Exists(strtxtDir) == false)
            {
                MessageBox.Show("文件夹不存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }
            else
            {
                btnExec.Enabled = false;
                List<string> PictureList = new List<string>();
                lb_statusInfo.Text = "状态:正在检索图片…";
                SearchFile(txtDir.Text.Trim(), ref PictureList);
                foreach (string s in PictureList)
                {
                    try
                    {
                        MakeWaterPic(s, "", markPicPath, "");
                        success++;
                    }
                    catch (Exception er)
                    {
                        falure++;
                        errmsg += er.Message;
                    }
                    total++;
                    lb_statusInfo.Text = "状态:正在为第" + (total + 1) + "张图片加水印…";
                }
                lb_statusInfo.Text = "状态:完成!共" + total + ",成功" + success + ",失败" + falure;
                btnExec.Enabled = true;
                if (errmsg != "") MessageBox.Show(errmsg, "执行完成,部分文件出错信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        public void SearchFile(string parentDir, ref List<string> PictureList)
        {
            try
            {
                string[] subFiles = Directory.GetFiles(parentDir);
            &nbs

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

Powered By Z-BlogPHP 1.7.3

Copyright 2024-2027 pukuimin Rights Reserved.
粤ICP备17100155号