using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.IO; namespace waterMark { public partial class Form1 : Form { string picName;//为了图片批处理,记录多图片路径和名称z public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { this.txtPicName.Visible = false;///加载系统时用于记录多张图片的txtbox不显示 } /// <summary> /// 选择图片 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnScan_Click(object sender, EventArgs e) { picName = "";//防止选择多张图片后在选择一张图片造成图名路径叠加到原来的上面 this.txtPicName.Text = "";//防止选择多张图片后在选择多张造成textbox都是双份的 string sourcePath; OpenFileDialog openFileDialog1 = new OpenFileDialog(); openFileDialog1.Multiselect = true; openFileDialog1.Filter = " TIF文件 (*.tif)|*.tif| JPG文件 (*.jpg)|*.jpg"; //openFileDialog1.Filter = "All files (*.*)|*.*|TIF文件 (*.tif)|*.tif| JPG文件 (*.jpg)|*.jpg"; openFileDialog1.FilterIndex = 1; openFileDialog1.RestoreDirectory = true; if (openFileDialog1.ShowDialog() == DialogResult.OK) { //处理单张图片 //this.txtAddress.Text = openFileDialog1.FileName; //sourcePath = this.txtAddress.Text; //System.Drawing.Image image = System.Drawing.Image.FromFile(sourcePath); //resultImage.Image = image; for (int i = 0; i < openFileDialog1.FileNames.Length; i++) { picName += openFileDialog1.FileNames.GetValue(i).ToString() + "|"; } picName = picName.Remove(picName.Length - 1); if (openFileDialog1.FileNames.Length == 1) { ///浏览时选择单张图片 this.txtPicName.Visible = false; this.resultImage.Visible = true; this.txtAddress.Text = openFileDialog1.FileName; sourcePath = this.txtAddress.Text; System.Drawing.Image image = System.Drawing.Image.FromFile(sourcePath); resultImage.Image = image; } else { ///浏览时选择多张图片 this.resultImage.Visible = false; this.txtPicName.Visible = true; string[] picPath = picName.Split(new Char[] { '|'}); for (int j = 0; j < picPath.Length; j++) { this.txtPicName.Text += picPath[j].ToString() + Environment.NewLine; } } } } /// <summary> /// 给选择的图片加盖水印按钮单击事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnSure_Click(object sender, EventArgs e) { string addText = "江西省地质资料馆"; if (this.txtAddress.Text == "" && this.txtPicName.Text == "") { MessageBox.Show("请选择图片!"); } else if(this.txtAddress.Text != "") { //处理单个图片方法(为单个图片加水印) String sourcePath = this.txtAddress.Text; System.Drawing.Image image = System.Drawing.Image.FromFile(sourcePath); System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(image); //graphics.DrawImage(image, 0, 0, image.Width, image.Height); //System.Drawing.Font font = new System.Drawing.Font("Italic", 15);//斜体 //System.Drawing.Brush brush = new System.Drawing.SolidBrush(System.Drawing.Color.Black); //graphics.DrawString(addText, font, brush, 150, 250); addWatermarkText(graphics, addText, "WM_TOP_LEFT", 130, 200); resultImage.Image = image; graphics.Dispose(); MessageBox.Show("水印添加成功!"); } else { ///以下将图片批量加水印并保存 SaveFileDialog saveFile = new SaveFileDialog(); saveFile.Filter = "TIF文件 (*.tif)|*.tif| JPG文件 (*.jpg)|*.jpg"; string[] picLines = this.txtPicName.Lines; int count = picLines.Length - 1; for (int i = 0; i < count; i++) { String sourcePath = picLines[i].ToString(); System.Drawing.Image image = System.Drawing.Image.FromFile(sourcePath); System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(image); addWatermarkText(graphics, addText, "WM_TOP_LEFT", 130, 200); resultImage.Image = image; if (saveFile.ShowDialog() == DialogResult.OK) { saveFile.Title = "另存为"; saveFile.InitialDirectory = saveFile.FileName; saveFile.AddExtension = true; this.resultImage.Image.Save(saveFile.FileName); MessageBox.Show("图片保存成功!"); } } this.txtPicName.Text = ""; } } /// <summary> /// 图片保存单击事件,处理单个图片 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnSave_Click(object sender, EventArgs e) { try { SaveFileDialog saveFile = new SaveFileDialog(); if (this.txtAddress.Text == "" && txtPicName.Text == "") { MessageBox.Show("请选择图片!"); } else if (txtAddress.Text != "") { saveFile.Filter = " TIF文件 (*.tif)|*.tif| JPG文件 (*.jpg)|*.jpg"; if (saveFile.ShowDialog() == DialogResult.OK) { saveFile.Title = "另存为"; saveFile.InitialDirectory = saveFile.FileName; saveFile.AddExtension = true; this.resultImage.Image.Save(saveFile.FileName); MessageBox.Show("图片保存成功!"); this.txtAddress.Text = ""; } } } catch (Exception ex) { MessageBox.Show(ex.Message); } } /// <summary> /// 加水印文字方法,网上找的方法,优点在于水印位置定位比较准确 /// </summary> /// <param name="picture">imge 对象</param> /// <param name="_watermarkText">水印文字内容</param> /// <param name="_watermarkPosition">水印位置</param> /// <param name="_width">被加水印图片的宽</param> /// <param name="_height">被加水印图片的高</param> private void addWatermarkText(Graphics picture, string _watermarkText, string _watermarkPosition, int _width, int _height) { int[] sizes = new int[] { 16, 16, 16, 16, 16, 16, 16 }; Font crFont = null; SizeF crSize = new SizeF(); for (int i = 0; i < 7; i++) { crFont = new Font("arial", sizes[i], FontStyle.Bold); crSize = picture.MeasureString(_watermarkText, crFont); if ((ushort)crSize.Width < (ushort)_width) break; } float xpos = 0; float ypos = 0; switch (_watermarkPosition)//水印位置 { case "WM_TOP_LEFT": xpos = ((float)_width * (float).01) + (crSize.Width / 2); ypos = (float)_height * (float).01; break; case "WM_TOP_RIGHT": xpos = ((float)_width * (float).99) - (crSize.Width / 2); ypos = (float)_height * (float).01; break; case "WM_BOTTOM_RIGHT": xpos = ((float)_width * (float).99) - (crSize.Width / 2); ypos = ((float)_height * (float).99) - crSize.Height; break; case "WM_BOTTOM_LEFT": xpos = ((float)_width * (float).01) + (crSize.Width / 2); ypos = ((float)_height * (float).99) - crSize.Height; break; } StringFormat StrFormat = new StringFormat(); StrFormat.Alignment = StringAlignment.Center; //SolidBrush semiTransBrush2 = new SolidBrush(Color.FromArgb(153, 0, 0, 0));//加阴影 //picture.DrawString(_watermarkText, crFont, xpos + 1, ypos + 1, StrFormat); SolidBrush semiTransBrush = new SolidBrush(Color.FromArgb(0,0,0)); picture.DrawString(_watermarkText, crFont, semiTransBrush, xpos, ypos, StrFormat); //semiTransBrush2.Dispose(); semiTransBrush.Dispose(); } /// <summary> /// 批量处理图片水印 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void txtPicName_TextChanged(object sender, EventArgs e) { if (this.txtPicName.Text != "") { this.btnSave.Visible = false; this.btnSure.Text = "批量加水印并保存"; } else { this.btnSave.Visible = true; this.btnSure.Text = "加水印"; } } } } 以上方法支持图片批量处理 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.IO; namespace waterMark { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } /// <summary> /// 选择图片 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnScan_Click(object sender, EventArgs e) { string sourcePath; OpenFileDialog openFileDialog1 = new OpenFileDialog(); openFileDialog1.Multiselect = true; openFileDialog1.Filter = "All files (*.*)|*.*|TIF文件 (*.tif)|*.tif| JPG文件 (*.jpg)|*.jpg"; openFileDialog1.FilterIndex = 1; openFileDialog1.RestoreDirectory = true; if (openFileDialog1.ShowDialog() == DialogResult.OK) { this.txtAddress.Text = openFileDialog1.FileName; sourcePath = this.txtAddress.Text; System.Drawing.Image image = System.Drawing.Image.FromFile(sourcePath); resultImage.Image = image; } } /// <summary> /// 给选择的图片加盖水印 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnSure_Click(object sender, EventArgs e) { if (this.txtAddress.Text == "") { MessageBox.Show("请选择图片!"); } else { String sourcePath = this.txtAddress.Text; System.Drawing.Image image = System.Drawing.Image.FromFile(sourcePath); System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(image); //graphics.DrawImage(image, 0, 0, image.Width, image.Height); //System.Drawing.Font font = new System.Drawing.Font("Italic", 15);//斜体 //System.Drawing.Brush brush = new System.Drawing.SolidBrush(System.Drawing.Color.Black); string addText = "江西省地质资料馆"; //graphics.DrawString(addText, font, brush, 150, 250); addWatermarkText(graphics, addText, "WM_TOP_LEFT", 130, 200); resultImage.Image = image; graphics.Dispose(); MessageBox.Show("水印添加成功!"); } } /// <summary> /// 将图片保存 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnSave_Click(object sender, EventArgs e) { try { if (this.txtAddress.Text == "") { MessageBox.Show("请选择图片!"); } else { SaveFileDialog saveFile = new SaveFileDialog(); saveFile.Filter = "TIF文件 (*.tif)|*.tif| JPG文件 (*.jpg)|*.jpg"; if (saveFile.ShowDialog() == DialogResult.OK) { saveFile.Title = "另存为"; saveFile.InitialDirectory = saveFile.FileName; saveFile.AddExtension = true; this.resultImage.Image.Save(saveFile.FileName); MessageBox.Show("图片保存成功!"); } } } catch (Exception ex) { MessageBox.Show(ex.Message); } } /// <summary> /// 加水印文字 /// </summary> /// <param name="picture">imge 对象</param> /// <param name="_watermarkText">水印文字内容</param> /// <param name="_watermarkPosition">水印位置</param> /// <param name="_width">被加水印图片的宽</param> /// <param name="_height">被加水印图片的高</param> private void addWatermarkText(Graphics picture, string _watermarkText, string _watermarkPosition, int _width, int _height) { int[] sizes = new int[] { 16, 16, 16, 16, 16, 16, 16 }; Font crFont = null; SizeF crSize = new SizeF(); for (int i = 0; i < 7; i++) { crFont = new Font("arial", sizes[i], FontStyle.Bold); crSize = picture.MeasureString(_watermarkText, crFont); if ((ushort)crSize.Width < (ushort)_width) break; } float xpos = 0; float ypos = 0; switch (_watermarkPosition)//水印位置 { case "WM_TOP_LEFT": xpos = ((float)_width * (float).01) + (crSize.Width / 2); ypos = (float)_height * (float).01; break; case "WM_TOP_RIGHT": xpos = ((float)_width * (float).99) - (crSize.Width / 2); ypos = (float)_height * (float).01; break; case "WM_BOTTOM_RIGHT": xpos = ((float)_width * (float).99) - (crSize.Width / 2); ypos = ((float)_height * (float).99) - crSize.Height; break; case "WM_BOTTOM_LEFT": xpos = ((float)_width * (float).01) + (crSize.Width / 2); ypos = ((float)_height * (float).99) - crSize.Height; break; } StringFormat StrFormat = new StringFormat(); StrFormat.Alignment = StringAlignment.Center; //SolidBrush semiTransBrush2 = new SolidBrush(Color.FromArgb(153, 0, 0, 0));//加阴影 //picture.DrawString(_watermarkText, crFont, xpos + 1, ypos + 1, StrFormat); SolidBrush semiTransBrush = new SolidBrush(Color.FromArgb(0,0,0)); picture.DrawString(_watermarkText, crFont, semiTransBrush, xpos, ypos, StrFormat); //semiTransBrush2.Dispose(); semiTransBrush.Dispose(); } } }