应用DEV第三方界面控件制作批量添加水印程序
本次应用DevExpress和C#语言制作了一个批量添加水印的程序,看界面效果图:
界面中既可以进行文字水印添加,也可以图片水印添加,同时还可以对水印的位置进行设置,比较实用!
文字水印的具体添加情况,看图:
还可以文字的预览:
整个文字水印的预览:
同时图片的水印预览:
最后显示下图片的水印效果:
核心代码部分:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Drawing.Imaging; using System.Drawing.Text; using System.Linq; using System.Text; using System.Windows.Forms; namespace 添加水印_程序 { public partial class Form1 : DevExpress.XtraEditors.XtraForm { 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 = 1; } string[] ImgArray = null; string ImgDirectoryPath = null; private void simpleButton4_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); //MessageBox.Show(ImgName); lbImgList.Items.Add(ImgName); } tsslStatus.Text = "图片总数:" + lbImgList.Items.Count; } } private void listBoxControl1_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); Frm_Browser frm2 = new Frm_Browser(); frm2.ig = BigBt; frm2.ShowDialog(); } } else { if (txtWaterMarkImg.Text != "" && txtSavaPath.Text != "") { ChangeAlpha(); AddFontWatermark(txtWaterMarkFont.Text.Trim(), lbImgList.Items[0].ToString(), 2); Frm_Browser frm2 = new Frm_Browser(); 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; //MessageBox.Show(txtWaterMarkImg.Text); 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 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 simpleButton7_Click(object sender, EventArgs e) { Application.Exit(); } 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(); } }
有兴趣的可自己尝试下!!!