C#实现批量给图片添加水印的示例代码

目录
  • 实践过程
    • 效果
    • 代码

实践过程

效果

代码

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

        #region 获取系统字体

        private void GetSystemFont(ToolStripComboBox cb)
        {
            InstalledFontCollection myFont = new InstalledFontCollection();
            foreach (FontFamily ff in myFont.Families)
            {
                cb.Items.Add(ff.Name);
            }

            cb.SelectedItem = "宋体";
        }

        #endregion

        private void Form1_Load(object sender, EventArgs e)
        {
            cbbPosition.SelectedIndex = 0;
        }

        string[] ImgArray = null;
        string ImgDirectoryPath = null;

        private void btnLoadImg_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                lbImgList.Items.Clear();
                ImgArray = openFileDialog1.FileNames;
                string ImgP = ImgArray[0].ToString();
                ImgP = ImgP.Remove(ImgP.LastIndexOf("\\"));
                ImgDirectoryPath = ImgP;
                for (int i = 0; i < ImgArray.Length; i++)
                {
                    string ImgPath = ImgArray[i].ToString();
                    string ImgName = ImgPath.Substring(ImgPath.LastIndexOf("\\") + 1,
                        ImgPath.Length - ImgPath.LastIndexOf("\\") - 1);
                    lbImgList.Items.Add(ImgName);
                }

                tsslStatus.Text = "图片总数:" + lbImgList.Items.Count;
            }
        }

        private void lbImgList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lbImgList.SelectedItems.Count > 0)
            {
                tsslText.Text = "图片位置:" + ImgDirectoryPath + "\\" + lbImgList.SelectedItems[0].ToString();
            }
        }

        private void btnPreview_Click(object sender, EventArgs e)
        {
            if (lbImgList.Items.Count > 0)
            {
                if (rbTxt.Checked)
                {
                    if (txtWaterMarkFont.Text != "" && txtSavaPath.Text.Trim() != "")
                    {
                        AddFontWatermark(txtWaterMarkFont.Text.Trim(), lbImgList.Items[0].ToString(), 0);
                        Form2 frm2 = new Form2();
                        frm2.ig = BigBt;
                        frm2.ShowDialog();
                    }
                }
                else
                {
                    if (txtWaterMarkImg.Text != "" && txtSavaPath.Text != "")
                    {
                        ChangeAlpha();
                        AddFontWatermark(txtWaterMarkFont.Text.Trim(), lbImgList.Items[0].ToString(), 2);
                        Form2 frm2 = new Form2();
                        frm2.ig = BigBt;
                        frm2.ShowDialog();
                    }
                }
            }
        }

        private void trackBar1_Enter(object sender, EventArgs e)
        {
            lbImgList.Focus();
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            if (openFileDialog2.ShowDialog() == DialogResult.OK)
            {
                txtWaterMarkImg.Text = openFileDialog2.FileName;
                if (rbPIC.Checked == true)
                {
                    ChangeAlpha();
                    pbImgPreview.Image = Image.FromFile(txtWaterMarkImg.Text.Trim());
                }
            }
        }

        private void rbTxt_CheckedChanged(object sender, EventArgs e)
        {
            trackBar1.Enabled = false;
            if (rbPIC.Checked)
                pbImgPreview.Image = null;
        }

        private void rbPIC_CheckedChanged(object sender, EventArgs e)
        {
            trackBar1.Enabled = true;
            if (rbTxt.Checked)
            {
                pbImgPreview.Image = null;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (lbImgList.Items.Count > 0)
            {
                fontDialog1.ShowColor = true;
                fontDialog1.ShowHelp = false;
                fontDialog1.ShowApply = false;
                if (fontDialog1.ShowDialog() == DialogResult.OK)
                {
                    FontF = fontDialog1.Font.FontFamily;
                    fontColor = fontDialog1.Color;
                    fontSize = fontDialog1.Font.Size;
                    fontStyle = fontDialog1.Font.Style;
                    AddFontWatermark(txtWaterMarkFont.Text.Trim(), lbImgList.Items[0].ToString(), 0);
                    pbImgPreview.Image = bt;
                }
            }
        }

        Bitmap bt = null;
        float fontSize = 8;
        Color fontColor = Color.Black;
        FontFamily FontF = null;
        FontStyle fontStyle = FontStyle.Regular;
        int Fwidth;
        int Fheight;
        Bitmap BigBt;
        Font f;
        Brush b;

        private void AddFontWatermark(string txt, string Iname, int i) //预览
        {
            b = new SolidBrush(fontColor);
            bt = new Bitmap(368, 75);
            BigBt = new Bitmap(Image.FromFile(ImgDirectoryPath + "\\" + Iname));
            Graphics g = Graphics.FromImage(bt);
            Graphics g1 = Graphics.FromImage(BigBt);
            g.Clear(Color.Gainsboro);
            pbImgPreview.Image = bt;
            if (FontF == null)
            {
                f = new Font(txt, fontSize);
                SizeF XMaxSize = g.MeasureString(txt, f);
                Fwidth = (int) XMaxSize.Width;
                Fheight = (int) XMaxSize.Height;
                g.DrawString(txt, f, b, (int) (368 - Fwidth) / 2, (int) (75 - Fheight) / 2);
                if (cbbPosition.SelectedIndex == 0) //正中
                {
                    g1.DrawString(txt, f, b, (int) (BigBt.Width - Fwidth) / 2, (int) (BigBt.Height - Fheight) / 2);
                }

                if (cbbPosition.SelectedIndex == 1) //左上
                {
                    g1.DrawString(txt, f, b, 30, 30);
                }

                if (cbbPosition.SelectedIndex == 2) //左下
                {
                    g1.DrawString(txt, f, b, 30, (int) (BigBt.Height - Fheight) - 30);
                }

                if (cbbPosition.SelectedIndex == 3) //右上
                {
                    g1.DrawString(txt, f, b, (int) (BigBt.Width - Fwidth), 30);
                }

                if (cbbPosition.SelectedIndex == 4) //右下
                {
                    g1.DrawString(txt, f, b, (int) (BigBt.Width - Fwidth), (int) (BigBt.Height - Fheight) - 30);
                }
            }
            else
            {
                f = new Font(FontF, fontSize, fontStyle);
                SizeF XMaxSize = g.MeasureString(txt, f);
                Fwidth = (int) XMaxSize.Width;
                Fheight = (int) XMaxSize.Height;
                g.DrawString(txt, new Font(FontF, fontSize, fontStyle), b, (int) (368 - Fwidth) / 2,
                    (int) (75 - Fheight) / 2);
                if (cbbPosition.SelectedIndex == 0) //正中
                {
                    g1.DrawString(txt, new Font(FontF, fontSize, fontStyle), b, (int) (BigBt.Width - Fwidth) / 2,
                        (int) (BigBt.Height - Fheight) / 2);
                }

                if (cbbPosition.SelectedIndex == 1) //左上
                {
                    g1.DrawString(txt, new Font(FontF, fontSize, fontStyle), b, 30, 30);
                }

                if (cbbPosition.SelectedIndex == 2) //左下
                {
                    g1.DrawString(txt, new Font(FontF, fontSize, fontStyle), b, 30,
                        (int) (BigBt.Height - Fheight) - 30);
                }

                if (cbbPosition.SelectedIndex == 3) //右上
                {
                    g1.DrawString(txt, new Font(FontF, fontSize, fontStyle), b, (int) (BigBt.Width - Fwidth), Fheight);
                }

                if (cbbPosition.SelectedIndex == 4) //右下
                {
                    g1.DrawString(txt, new Font(FontF, fontSize, fontStyle), b, (int) (BigBt.Width - Fwidth),
                        (int) (BigBt.Height - Fheight) - 30);
                }
            }

            if (i == 1)
            {
                string ipath;
                if (NewFolderPath.Length == 3)
                    ipath = NewFolderPath.Remove(NewFolderPath.LastIndexOf(":") + 1);
                else
                    ipath = NewFolderPath;
                string imgstype =
                    Iname.Substring(Iname.LastIndexOf(".") + 1, Iname.Length - 1 - Iname.LastIndexOf("."));
                if (imgstype.ToLower() == "jpeg" || imgstype.ToLower() == "jpg")
                {
                    BigBt.Save(ipath + "\\_" + Iname, ImageFormat.Jpeg);
                }

                if (imgstype.ToLower() == "png")
                {
                    BigBt.Save(ipath + "\\_" + Iname, ImageFormat.Png);
                }

                if (imgstype.ToLower() == "bmp")
                {
                    BigBt.Save(ipath + "\\_" + Iname, ImageFormat.Bmp);
                }

                if (imgstype.ToLower() == "gif")
                {
                    BigBt.Save(ipath + "\\_" + Iname, ImageFormat.Gif);
                }

                g1.Dispose();
                BigBt.Dispose();
            }

            if (i == 2)
            {
                if (cbbPosition.SelectedIndex == 0) //正中
                {
                    g1.DrawImage(effect, (int) (BigBt.Width - effect.Width) / 2,
                        (int) (BigBt.Height - effect.Height) / 2);
                }

                if (cbbPosition.SelectedIndex == 1) //左上
                {
                    g1.DrawImage(effect, 30, 30);
                }

                if (cbbPosition.SelectedIndex == 2) //左下
                {
                    g1.DrawImage(effect, 30, (int) (BigBt.Height - effect.Height) - 30);
                }

                if (cbbPosition.SelectedIndex == 3) //右上
                {
                    g1.DrawImage(effect, (int) (BigBt.Width - effect.Width) - 30, 30);
                }

                if (cbbPosition.SelectedIndex == 4) //右下
                {
                    g1.DrawImage(effect, (int) (BigBt.Width - effect.Width) - 30,
                        (int) (BigBt.Height - effect.Height) - 30);
                }
            }

            if (i == 3)
            {
                if (cbbPosition.SelectedIndex == 0) //正中
                {
                    g1.DrawImage(effect, (int) (BigBt.Width - effect.Width) / 2,
                        (int) (BigBt.Height - effect.Height) / 2);
                }

                if (cbbPosition.SelectedIndex == 1) //左上
                {
                    g1.DrawImage(effect, 30, 30);
                }

                if (cbbPosition.SelectedIndex == 2) //左下
                {
                    g1.DrawImage(effect, 30, (int) (BigBt.Height - effect.Height) - 30);
                }

                if (cbbPosition.SelectedIndex == 3) //右上
                {
                    g1.DrawImage(effect, (int) (BigBt.Width - effect.Width), 30);
                }

                if (cbbPosition.SelectedIndex == 4) //右下
                {
                    g1.DrawImage(effect, (int) (BigBt.Width - effect.Width), (int) (BigBt.Height - effect.Height) - 30);
                }

                string ipath;
                if (NewFolderPath.Length == 3)
                    ipath = NewFolderPath.Remove(NewFolderPath.LastIndexOf(":") + 1);
                else
                    ipath = NewFolderPath;
                string imgstype =
                    Iname.Substring(Iname.LastIndexOf(".") + 1, Iname.Length - 1 - Iname.LastIndexOf("."));
                if (imgstype.ToLower() == "jpeg" || imgstype.ToLower() == "jpg")
                {
                    BigBt.Save(ipath + "\\_" + Iname, ImageFormat.Jpeg);
                }

                if (imgstype.ToLower() == "png")
                {
                    BigBt.Save(ipath + "\\_" + Iname, ImageFormat.Png);
                }

                if (imgstype.ToLower() == "bmp")
                {
                    BigBt.Save(ipath + "\\_" + Iname, ImageFormat.Bmp);
                }

                if (imgstype.ToLower() == "gif")
                {
                    BigBt.Save(ipath + "\\_" + Iname, ImageFormat.Gif);
                }
            }
        }

        private void txtWaterMarkFont_TextChanged(object sender, EventArgs e)
        {
            if (lbImgList.Items.Count <= 0)
            {
                MessageBox.Show("请加载图片", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else
            {
                AddFontWatermark(txtWaterMarkFont.Text.Trim(), lbImgList.Items[0].ToString(), 0);
                pbImgPreview.Image = bt;
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        /// <summary>
        /// 调节透明度
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        Bitmap effect;

        Bitmap source;
        Image new_img;

        private void ChangeAlpha()
        {
            pbImgPreview.Refresh();
            source = new Bitmap(Image.FromFile(txtWaterMarkImg.Text.Trim()));
            if (source.Width <= 368)
                effect = new Bitmap(368, 75);
            else
            {
                Image.GetThumbnailImageAbort callb = null;
                //对水印图片生成缩略图,缩小到原图得1/4
                new_img = source.GetThumbnailImage(source.Width / 4, source.Width / 4, callb, new System.IntPtr());
                effect = new Bitmap(this.new_img.Width, this.new_img.Height);
            }

            Graphics _effect = Graphics.FromImage(effect);
            float[][] matrixItems =
            {
                new float[] {1, 0, 0, 0, 0},
                new float[] {0, 1, 0, 0, 0},
                new float[] {0, 0, 1, 0, 0},
                new float[] {0, 0, 0, 0, 0},
                new float[] {0, 0, 0, trackBar1.Value / 255f, 1}
            };
            ColorMatrix imgMatrix = new ColorMatrix(matrixItems);
            ImageAttributes imgEffect = new ImageAttributes();
            imgEffect.SetColorMatrix(imgMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
            if (source.Width <= 368)
            {
                _effect.DrawImage(source, new Rectangle(0, 0, 368, 75), 0, 0, 368, 75, GraphicsUnit.Pixel, imgEffect);
            }
            else
            {
                _effect.DrawImage(new_img, new Rectangle(0, 0, new_img.Width, new_img.Height), 0, 0, new_img.Width,
                    new_img.Height, GraphicsUnit.Pixel, imgEffect);
            }

            pbImgPreview.Image = effect;
        }

        private void trackBar1_ValueChanged(object sender, EventArgs e)
        {
            if (rbPIC.Checked && txtWaterMarkImg.Text.Trim() != "")
                ChangeAlpha();
        }

        private void btnPerform_Click(object sender, EventArgs e)
        {
            if (rbTxt.Checked && txtSavaPath.Text != "" && txtWaterMarkFont.Text != "")
            {
                for (int i = 0; i < lbImgList.Items.Count; i++)
                {
                    AddFontWatermark(txtWaterMarkFont.Text.Trim(), lbImgList.Items[i].ToString(), 1);
                }

                MessageBox.Show("添加水印成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }

            if (rbPIC.Checked && txtSavaPath.Text != "" && pbImgPreview.Image != null)
            {
                for (int i = 0; i < lbImgList.Items.Count; i++)
                {
                    AddFontWatermark(txtWaterMarkFont.Text.Trim(), lbImgList.Items[i].ToString(), 3);
                }

                MessageBox.Show("添加水印成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        string NewFolderPath;

        private void button2_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                txtSavaPath.Text = folderBrowserDialog1.SelectedPath;
                NewFolderPath = txtSavaPath.Text.Trim();
            }
        }
    }
partial class Form1
    {
        /// <summary>
        /// 必需的设计器变量。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows 窗体设计器生成的代码

        /// <summary>
        /// 设计器支持所需的方法 - 不要
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.colorDialog1 = new System.Windows.Forms.ColorDialog();
            this.statusStrip1 = new System.Windows.Forms.StatusStrip();
            this.tsslStatus = new System.Windows.Forms.ToolStripStatusLabel();
            this.tsslText = new System.Windows.Forms.ToolStripStatusLabel();
            this.splitContainer1 = new System.Windows.Forms.SplitContainer();
            this.groupBox1 = new System.Windows.Forms.GroupBox();
            this.lbImgList = new System.Windows.Forms.ListBox();
            this.button2 = new System.Windows.Forms.Button();
            this.txtSavaPath = new System.Windows.Forms.TextBox();
            this.label5 = new System.Windows.Forms.Label();
            this.btnPreview = new System.Windows.Forms.Button();
            this.btnExit = new System.Windows.Forms.Button();
            this.btnPerform = new System.Windows.Forms.Button();
            this.btnLoadImg = new System.Windows.Forms.Button();
            this.groupBox2 = new System.Windows.Forms.GroupBox();
            this.label6 = new System.Windows.Forms.Label();
            this.label4 = new System.Windows.Forms.Label();
            this.cbbPosition = new System.Windows.Forms.ComboBox();
            this.label2 = new System.Windows.Forms.Label();
            this.button1 = new System.Windows.Forms.Button();
            this.trackBar1 = new System.Windows.Forms.TrackBar();
            this.rbPIC = new System.Windows.Forms.RadioButton();
            this.rbTxt = new System.Windows.Forms.RadioButton();
            this.txtWaterMarkFont = new System.Windows.Forms.TextBox();
            this.label1 = new System.Windows.Forms.Label();
            this.txtWaterMarkImg = new System.Windows.Forms.TextBox();
            this.label3 = new System.Windows.Forms.Label();
            this.btnSelect = new System.Windows.Forms.Button();
            this.pbImgPreview = new System.Windows.Forms.PictureBox();
            this.fontDialog1 = new System.Windows.Forms.FontDialog();
            this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
            this.openFileDialog2 = new System.Windows.Forms.OpenFileDialog();
            this.folderBrowserDialog1 = new System.Windows.Forms.FolderBrowserDialog();
            this.statusStrip1.SuspendLayout();
            this.splitContainer1.Panel1.SuspendLayout();
            this.splitContainer1.Panel2.SuspendLayout();
            this.splitContainer1.SuspendLayout();
            this.groupBox1.SuspendLayout();
            this.groupBox2.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.trackBar1)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.pbImgPreview)).BeginInit();
            this.SuspendLayout();
            //
            // statusStrip1
            //
            this.statusStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.tsslStatus,
            this.tsslText});
            this.statusStrip1.Location = new System.Drawing.Point(0, 368);
            this.statusStrip1.Name = "statusStrip1";
            this.statusStrip1.Size = new System.Drawing.Size(601, 22);
            this.statusStrip1.TabIndex = 0;
            this.statusStrip1.Text = "statusStrip1";
            //
            // tsslStatus
            //
            this.tsslStatus.Name = "tsslStatus";
            this.tsslStatus.Size = new System.Drawing.Size(53, 17);
            this.tsslStatus.Text = "准备就绪";
            //
            // tsslText
            //
            this.tsslText.Name = "tsslText";
            this.tsslText.Size = new System.Drawing.Size(0, 17);
            //
            // splitContainer1
            //
            this.splitContainer1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            this.splitContainer1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.splitContainer1.IsSplitterFixed = true;
            this.splitContainer1.Location = new System.Drawing.Point(0, 0);
            this.splitContainer1.Name = "splitContainer1";
            //
            // splitContainer1.Panel1
            //
            this.splitContainer1.Panel1.Controls.Add(this.groupBox1);
            //
            // splitContainer1.Panel2
            //
            this.splitContainer1.Panel2.Controls.Add(this.button2);
            this.splitContainer1.Panel2.Controls.Add(this.txtSavaPath);
            this.splitContainer1.Panel2.Controls.Add(this.label5);
            this.splitContainer1.Panel2.Controls.Add(this.btnPreview);
            this.splitContainer1.Panel2.Controls.Add(this.btnExit);
            this.splitContainer1.Panel2.Controls.Add(this.btnPerform);
            this.splitContainer1.Panel2.Controls.Add(this.btnLoadImg);
            this.splitContainer1.Panel2.Controls.Add(this.groupBox2);
            this.splitContainer1.Size = new System.Drawing.Size(601, 368);
            this.splitContainer1.SplitterDistance = 206;
            this.splitContainer1.SplitterWidth = 2;
            this.splitContainer1.TabIndex = 1;
            //
            // groupBox1
            //
            this.groupBox1.Controls.Add(this.lbImgList);
            this.groupBox1.Location = new System.Drawing.Point(7, 2);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new System.Drawing.Size(189, 359);
            this.groupBox1.TabIndex = 0;
            this.groupBox1.TabStop = false;
            this.groupBox1.Text = "加水印图片列表";
            //
            // lbImgList
            //
            this.lbImgList.FormattingEnabled = true;
            this.lbImgList.ItemHeight = 12;
            this.lbImgList.Location = new System.Drawing.Point(6, 13);
            this.lbImgList.Name = "lbImgList";
            this.lbImgList.Size = new System.Drawing.Size(177, 340);
            this.lbImgList.TabIndex = 0;
            this.lbImgList.SelectedIndexChanged += new System.EventHandler(this.lbImgList_SelectedIndexChanged);
            //
            // button2
            //
            this.button2.Location = new System.Drawing.Point(321, 291);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(65, 23);
            this.button2.TabIndex = 8;
            this.button2.Text = "浏览...";
            this.button2.UseVisualStyleBackColor = true;
            this.button2.Click += new System.EventHandler(this.button2_Click);
            //
            // txtSavaPath
            //
            this.txtSavaPath.BackColor = System.Drawing.Color.White;
            this.txtSavaPath.Enabled = false;
            this.txtSavaPath.Location = new System.Drawing.Point(78, 292);
            this.txtSavaPath.Name = "txtSavaPath";
            this.txtSavaPath.ReadOnly = true;
            this.txtSavaPath.Size = new System.Drawing.Size(239, 21);
            this.txtSavaPath.TabIndex = 7;
            //
            // label5
            //
            this.label5.AutoSize = true;
            this.label5.Location = new System.Drawing.Point(13, 296);
            this.label5.Name = "label5";
            this.label5.Size = new System.Drawing.Size(65, 12);
            this.label5.TabIndex = 6;
            this.label5.Text = "保存位置:";
            //
            // btnPreview
            //
            this.btnPreview.Location = new System.Drawing.Point(114, 327);
            this.btnPreview.Name = "btnPreview";
            this.btnPreview.Size = new System.Drawing.Size(75, 23);
            this.btnPreview.TabIndex = 5;
            this.btnPreview.Text = "水印预览";
            this.btnPreview.UseVisualStyleBackColor = true;
            this.btnPreview.Click += new System.EventHandler(this.btnPreview_Click);
            //
            // btnExit
            //
            this.btnExit.Location = new System.Drawing.Point(301, 327);
            this.btnExit.Name = "btnExit";
            this.btnExit.Size = new System.Drawing.Size(75, 23);
            this.btnExit.TabIndex = 4;
            this.btnExit.Text = "关闭退出";
            this.btnExit.UseVisualStyleBackColor = true;
            this.btnExit.Click += new System.EventHandler(this.btnExit_Click);
            //
            // btnPerform
            //
            this.btnPerform.Location = new System.Drawing.Point(207, 327);
            this.btnPerform.Name = "btnPerform";
            this.btnPerform.Size = new System.Drawing.Size(75, 23);
            this.btnPerform.TabIndex = 3;
            this.btnPerform.Text = "开始执行";
            this.btnPerform.UseVisualStyleBackColor = true;
            this.btnPerform.Click += new System.EventHandler(this.btnPerform_Click);
            //
            // btnLoadImg
            //
            this.btnLoadImg.Location = new System.Drawing.Point(14, 327);
            this.btnLoadImg.Name = "btnLoadImg";
            this.btnLoadImg.Size = new System.Drawing.Size(75, 23);
            this.btnLoadImg.TabIndex = 2;
            this.btnLoadImg.Text = "加载图片";
            this.btnLoadImg.UseVisualStyleBackColor = true;
            this.btnLoadImg.Click += new System.EventHandler(this.btnLoadImg_Click);
            //
            // groupBox2
            //
            this.groupBox2.Controls.Add(this.label6);
            this.groupBox2.Controls.Add(this.label4);
            this.groupBox2.Controls.Add(this.cbbPosition);
            this.groupBox2.Controls.Add(this.label2);
            this.groupBox2.Controls.Add(this.button1);
            this.groupBox2.Controls.Add(this.trackBar1);
            this.groupBox2.Controls.Add(this.rbPIC);
            this.groupBox2.Controls.Add(this.rbTxt);
            this.groupBox2.Controls.Add(this.txtWaterMarkFont);
            this.groupBox2.Controls.Add(this.label1);
            this.groupBox2.Controls.Add(this.txtWaterMarkImg);
            this.groupBox2.Controls.Add(this.label3);
            this.groupBox2.Controls.Add(this.btnSelect);
            this.groupBox2.Controls.Add(this.pbImgPreview);
            this.groupBox2.Location = new System.Drawing.Point(3, 3);
            this.groupBox2.Name = "groupBox2";
            this.groupBox2.Size = new System.Drawing.Size(383, 274);
            this.groupBox2.TabIndex = 0;
            this.groupBox2.TabStop = false;
            this.groupBox2.Text = "水印设置";
            //
            // label6
            //
            this.label6.AutoSize = true;
            this.label6.Location = new System.Drawing.Point(10, 171);
            this.label6.Name = "label6";
            this.label6.Size = new System.Drawing.Size(257, 12);
            this.label6.TabIndex = 16;
            this.label6.Text = "注意:水印图片建议使用分辨率为368*75的图片";
            //
            // label4
            //
            this.label4.AutoSize = true;
            this.label4.Location = new System.Drawing.Point(244, 145);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(65, 12);
            this.label4.TabIndex = 4;
            this.label4.Text = "水印位置:";
            //
            // cbbPosition
            //
            this.cbbPosition.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.cbbPosition.FormattingEnabled = true;
            this.cbbPosition.Items.AddRange(new object[] {
            "正中",
            "左上",
            "左下",
            "右上",
            "右下"});
            this.cbbPosition.Location = new System.Drawing.Point(315, 141);
            this.cbbPosition.Name = "cbbPosition";
            this.cbbPosition.Size = new System.Drawing.Size(65, 20);
            this.cbbPosition.TabIndex = 15;
            //
            // label2
            //
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(8, 147);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(65, 12);
            this.label2.TabIndex = 14;
            this.label2.Text = "透明设置:";
            //
            // button1
            //
            this.button1.Location = new System.Drawing.Point(315, 48);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(65, 23);
            this.button1.TabIndex = 13;
            this.button1.Text = "字体设置";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            //
            // trackBar1
            //
            this.trackBar1.AutoSize = false;
            this.trackBar1.Enabled = false;
            this.trackBar1.LargeChange = 1;
            this.trackBar1.Location = new System.Drawing.Point(69, 140);
            this.trackBar1.Maximum = 255;
            this.trackBar1.Name = "trackBar1";
            this.trackBar1.Size = new System.Drawing.Size(179, 22);
            this.trackBar1.TabIndex = 12;
            this.trackBar1.Value = 255;
            this.trackBar1.ValueChanged += new System.EventHandler(this.trackBar1_ValueChanged);
            this.trackBar1.Enter += new System.EventHandler(this.trackBar1_Enter);
            //
            // rbPIC
            //
            this.rbPIC.AutoSize = true;
            this.rbPIC.Location = new System.Drawing.Point(10, 82);
            this.rbPIC.Name = "rbPIC";
            this.rbPIC.Size = new System.Drawing.Size(95, 16);
            this.rbPIC.TabIndex = 11;
            this.rbPIC.Text = "添加图片水印";
            this.rbPIC.UseVisualStyleBackColor = true;
            this.rbPIC.CheckedChanged += new System.EventHandler(this.rbPIC_CheckedChanged);
            //
            // rbTxt
            //
            this.rbTxt.AutoSize = true;
            this.rbTxt.Checked = true;
            this.rbTxt.Location = new System.Drawing.Point(8, 22);
            this.rbTxt.Name = "rbTxt";
            this.rbTxt.Size = new System.Drawing.Size(95, 16);
            this.rbTxt.TabIndex = 10;
            this.rbTxt.TabStop = true;
            this.rbTxt.Text = "添加文字水印";
            this.rbTxt.UseVisualStyleBackColor = true;
            this.rbTxt.CheckedChanged += new System.EventHandler(this.rbTxt_CheckedChanged);
            //
            // txtWaterMarkFont
            //
            this.txtWaterMarkFont.Location = new System.Drawing.Point(75, 49);
            this.txtWaterMarkFont.Name = "txtWaterMarkFont";
            this.txtWaterMarkFont.Size = new System.Drawing.Size(239, 21);
            this.txtWaterMarkFont.TabIndex = 2;
            this.txtWaterMarkFont.TextChanged += new System.EventHandler(this.txtWaterMarkFont_TextChanged);
            //
            // label1
            //
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(8, 53);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(65, 12);
            this.label1.TabIndex = 1;
            this.label1.Text = "水印文字:";
            //
            // txtWaterMarkImg
            //
            this.txtWaterMarkImg.BackColor = System.Drawing.Color.White;
            this.txtWaterMarkImg.Enabled = false;
            this.txtWaterMarkImg.Location = new System.Drawing.Point(75, 109);
            this.txtWaterMarkImg.Name = "txtWaterMarkImg";
            this.txtWaterMarkImg.ReadOnly = true;
            this.txtWaterMarkImg.Size = new System.Drawing.Size(239, 21);
            this.txtWaterMarkImg.TabIndex = 1;
            //
            // label3
            //
            this.label3.AutoSize = true;
            this.label3.Location = new System.Drawing.Point(8, 114);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(65, 12);
            this.label3.TabIndex = 0;
            this.label3.Text = "水印图片:";
            //
            // btnSelect
            //
            this.btnSelect.Location = new System.Drawing.Point(315, 108);
            this.btnSelect.Name = "btnSelect";
            this.btnSelect.Size = new System.Drawing.Size(65, 23);
            this.btnSelect.TabIndex = 2;
            this.btnSelect.Text = "浏览...";
            this.btnSelect.UseVisualStyleBackColor = true;
            this.btnSelect.Click += new System.EventHandler(this.btnSelect_Click);
            //
            // pbImgPreview
            //
            this.pbImgPreview.BackColor = System.Drawing.Color.WhiteSmoke;
            this.pbImgPreview.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            this.pbImgPreview.Location = new System.Drawing.Point(9, 191);
            this.pbImgPreview.Name = "pbImgPreview";
            this.pbImgPreview.Size = new System.Drawing.Size(368, 75);
            this.pbImgPreview.SizeMode = System.Windows.Forms.PictureBoxSizeMode.CenterImage;
            this.pbImgPreview.TabIndex = 3;
            this.pbImgPreview.TabStop = false;
            //
            // openFileDialog1
            //
            this.openFileDialog1.Filter = "图片文件|*.jpeg;*.jpg;*.png;*.bmp;*.gif";
            this.openFileDialog1.Multiselect = true;
            //
            // openFileDialog2
            //
            this.openFileDialog2.Filter = "图片文件|*.jpeg;*.jpg;*.png;*.bmp";
            //
            // Form1
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(601, 390);
            this.Controls.Add(this.splitContainer1);
            this.Controls.Add(this.statusStrip1);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
            this.MaximizeBox = false;
            this.Name = "Form1";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "图片批量加水印";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.statusStrip1.ResumeLayout(false);
            this.statusStrip1.PerformLayout();
            this.splitContainer1.Panel1.ResumeLayout(false);
            this.splitContainer1.Panel2.ResumeLayout(false);
            this.splitContainer1.Panel2.PerformLayout();
            this.splitContainer1.ResumeLayout(false);
            this.groupBox1.ResumeLayout(false);
            this.groupBox2.ResumeLayout(false);
            this.groupBox2.PerformLayout();
            ((System.ComponentModel.ISupportInitialize)(this.trackBar1)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.pbImgPreview)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.ColorDialog colorDialog1;
        private System.Windows.Forms.StatusStrip statusStrip1;
        private System.Windows.Forms.SplitContainer splitContainer1;
        private System.Windows.Forms.GroupBox groupBox1;
        private System.Windows.Forms.ListBox lbImgList;
        private System.Windows.Forms.GroupBox groupBox2;
        private System.Windows.Forms.TextBox txtWaterMarkFont;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.FontDialog fontDialog1;
        private System.Windows.Forms.Button btnSelect;
        private System.Windows.Forms.TextBox txtWaterMarkImg;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.PictureBox pbImgPreview;
        private System.Windows.Forms.Label label4;
        private System.Windows.Forms.Button btnLoadImg;
        private System.Windows.Forms.Button btnPreview;
        private System.Windows.Forms.Button btnExit;
        private System.Windows.Forms.Button btnPerform;
        private System.Windows.Forms.RadioButton rbPIC;
        private System.Windows.Forms.RadioButton rbTxt;
        private System.Windows.Forms.OpenFileDialog openFileDialog1;
        private System.Windows.Forms.ToolStripStatusLabel tsslStatus;
        private System.Windows.Forms.ToolStripStatusLabel tsslText;
        private System.Windows.Forms.TrackBar trackBar1;
        private System.Windows.Forms.OpenFileDialog openFileDialog2;
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.ComboBox cbbPosition;
        private System.Windows.Forms.Button button2;
        private System.Windows.Forms.TextBox txtSavaPath;
        private System.Windows.Forms.Label label5;
        private System.Windows.Forms.FolderBrowserDialog folderBrowserDialog1;
        private System.Windows.Forms.Label label6;
    }
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
        public Image ig = null;
        private void Form2_Load(object sender, EventArgs e)
        {
            pictureBox1.Image = ig;
        }
    }
  partial class Form2
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.pictureBox1 = new System.Windows.Forms.PictureBox();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
            this.SuspendLayout();
            //
            // pictureBox1
            //
            this.pictureBox1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.pictureBox1.Location = new System.Drawing.Point(0, 0);
            this.pictureBox1.Name = "pictureBox1";
            this.pictureBox1.Size = new System.Drawing.Size(630, 448);
            this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
            this.pictureBox1.TabIndex = 0;
            this.pictureBox1.TabStop = false;
            //
            // Form2
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(630, 448);
            this.Controls.Add(this.pictureBox1);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
            this.MaximizeBox = false;
            this.Name = "Form2";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "水印预览";
            this.Load += new System.EventHandler(this.Form2_Load);
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.PictureBox pictureBox1;
    }

以上就是C#实现批量给图片添加水印的示例代码的详细内容,更多关于C#图片添加水印的资料请关注我们其它相关文章!

(0)

相关推荐

  • c#图片添加水印的实例代码

    复制代码 代码如下: using System;using System.Drawing;using System.Drawing.Drawing2D;using System.Drawing.Imaging;using System.IO;namespace ImageDrawing{ /// <summary> /// 图片修改类,主要是用来保护图片版权的 /// </summary> public class ImageModification {  #region &quo

  • C# 添加图片水印类实现代码

    复制代码 代码如下: using System; using System.Collections.Generic; using System.Text; using System.Drawing; using System.IO; using System.Drawing.Imaging; using System.Web; using System.Drawing.Drawing2D; using System.Reflection; namespace Chen { public clas

  • C#实现给图片加水印的方法

    本文实例讲述了C#实现给图片加水印的方法.分享给大家供大家参考,具体如下: using System; using System.Drawing; using System.Drawing.Imaging; using System.Drawing.Drawing2D; namespace Tutorial { class WaterMark { [STAThread] static void Main(string[] args) { //set a working directory str

  • C#给图片加水印的简单实现方法

    本文实例讲述了C#给图片加水印的简单实现方法.分享给大家供大家参考.具体分析如下: 这里实现本网站图片保护功能类: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Drawing;//image的命名空间 namespace 实现本网站图片保护功能 { public class yanzhengma:IHttpHandler { public boo

  • C#给图片添加水印完整实例

    本文实例讲述了C#给图片添加水印的方法.分享给大家供大家参考,具体如下: using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using Syste

  • C#图片添加水印的实现代码

    本文实例介绍了C#图片添加水印的实现方法,可以为图片加文字水印,及判断是否是图片文件,分享给大家供大家参考,具体内容如下 效果图: 以下是HovercWarter类的代码: using System.Drawing; using System.Drawing.Imaging; using System.IO; namespace HoverTreeBatch.HovercFrame { public class HovercWarter { public static Image AddText

  • C#实现批量给图片添加水印的示例代码

    目录 实践过程 效果 代码 实践过程 效果 代码 public partial class Form1 : Form { public Form1() { InitializeComponent(); } #region 获取系统字体 private void GetSystemFont(ToolStripComboBox cb) { InstalledFontCollection myFont = new InstalledFontCollection(); foreach (FontFami

  • Python批量改变图片名字的示例代码

    目录 一.系统自带的快捷改名 二.用Python按要求改名 三.在原有的图片上进行修改 总结 一.系统自带的快捷改名 1.打开对应的图片文件 2.全选后右击重命名 3.输入:Cat 注意: 默认你输入的Cat与(1)之间有一个空格 二.用Python按要求改名 1.代码如下: import os.path def rename(img_folder,num): for img_name in os.listdir(img_folder): # os.listdir(): 列出路径下所有的文件 #

  • C#实现批量下载图片到本地示例代码

    一.概述 批量下载图片是我们在日常开发中经常会遇到的一个需求,这不,最近工作中就需要批量下载图片到本地,先是通过Excel拼接生成了所有链接地址,然后想到的是通过下载软件来批量下载.可是一想到又要花时间去查找.安装.研究软件,不如自己写个来的快. 以下是使用C#开发的控制台程序代码,通过循环读取文本文件中每一行地址字符串,执行下载并保存到本地文件夹中. 下面话不多说了,来一起看看详细的介绍吧 二.C#实例代码 //using System; //using System.Net; //using

  • ORACLE批量导入图片到BLOB字段代码示例

    要插入图片的表不是固定的,而且是批量插入很多张,还要考虑到因为图片的文件名错误,修改后要再次插入,此时应避免已经插入的重复执行操作, 浪费时间. 所以就选择先用一张临时表来暂时保存从文件系统读取的照片,用DBMS_LOB的方法来处理. 第一步:创建一个数据库可以访问的目录(注意:这个目录是数据库服务器上的目录,不是客户机上的) -- Create directory create or replace directory 图片目录 as 'E:\照片'; 第二步:将图片文件放入刚建好的目录下面,

  • Python实现为图片添加水印的示例详解

    目录 1.引言 2.filestools介绍 2.1 安装 2.2 filestools 功能介绍 2.3 watermarker模块介绍 2.4 代码实例 补充 1.引言 小屌丝:鱼哥,这个周末过得咋样 小鱼:酸爽~ ~ 小屌丝:额~~ 我能想到的,是这样吗? 小鱼:有多远你走多远. 小屌丝:唉,鱼哥,你别说,我觉得这个图片,跟你平时的表情挺贴切的. 小鱼:你想咋的!!!! 小屌丝:突然想到,能不能给你来一个专属的图片,例如追加水印啥的,让别人无图可盗!! 小鱼:嘿~ 你别说,还真的可以哈,

  • Python实现Gif图片分解的示例代码

    目录 1.模块安装 2.模块导入 3.分解动图 4.批量分解 5.生成效果 在前面做过一个Gif图片合成的小工具,老朋友应该有所印象.但是,近段时间有人反映合成了的Gif图片该如何进行分解呢? 于是,再次了解了一下PIL模块的Image,果真是可以做动图分解的. 1.模块安装 使用PIL库,实际上是来源于pillow库的兼容,因此需要安装pillow非标准库. pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pillow 2.模块导

  • 鼠标滚轮改变图片大小的示例代码

    鼠标滚轮改变图片大小的示例代码<script> function onWheelZoom(obj){         zoom = parseFloat(obj.style.zoom);         tZoom = zoom + (event.wheelDelta>0 ? 0.05 : -0.05);         if( tZoom > 1 || tZoom<0.1 ) return true;         obj.style.zoom=tZoom;       

  • vue实现图片滚动的示例代码(类似走马灯效果)

    上次写了一个简单的图片轮播,这个相当于在上面的一些改进.这个组件除了可以进行图片滚动外,也可以嵌入任何内容的标签进行滚动,里面用了slot进行封装. 父: <template> <div id="app"> <er-carousel-index :typeNumber=2 :pageNumber=3 :timeSpace=2 :duration=2 :isOrNotCircle="true" url="/src/js/inde

  • Python批量修改图片分辨率的实例代码

    前言:处理图片需要,需把图片都转换成1920*1280的大小, python实现很方便,需要导入图片处理的Image包和匹配的glob包,很简单,代码如下: img_path = glob.glob("D:/chosed/*.jpg") path_save = "D:/closedd" for file in img_path: name = os.path.join(path_save, file) im = Image.open(file) im.thumbna

随机推荐