给图片加水印

时间:2016-03-31 03:53:32
【文件属性】:
文件名称:给图片加水印
文件大小:49KB
文件格式:ZIP
更新时间:2016-03-31 03:53:32
加水印源码 可以轻松给图片加文字水印 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Drawing.Drawing2D; using System.Drawing.Imaging; using System.IO; namespace WindowsApplication7 { public partial class FrmAddWave : Form { public FrmAddWave() { InitializeComponent(); } private void MakeWaterMark(string IconImg) { //定义输出位图 Bitmap bmp = new Bitmap(1000, 700); //定义绘图对象 Graphics g = Graphics.FromImage(bmp); //描述绘图矩形区域 Rectangle newRect = new Rectangle(0, 0, 1000, 700); //设置背景色 g.FillRectangle(new SolidBrush(Color.White), newRect); //获取要生成水印的图片 Bitmap newBitmap = new Bitmap(IconImg); g.DrawImage(newBitmap, 16, 16); //定义水印字的字体 Font font = new Font( "Arial", 12, FontStyle.Bold ); //定义水印字的格式刷 LinearGradientBrush brush = new LinearGradientBrush( new Rectangle(600, 600, 71, 23), Color.Red, Color.Blue, 30f, true ); //给照片打上水印 g.DrawString("copyright by test. " + System.DateTime.Now.ToShortDateString(), font, brush, 0, 0); //保存为新的输出图片 System.IO.MemoryStream ms = new System.IO.MemoryStream(); bmp.Save(@"c:\a1.jpg", ImageFormat.Jpeg); ////输出图片 //Response.ClearContent(); //Response.ContentType = "image/jpeg"; //Response.BinaryWrite(ms.ToArray()); } private void button1_Click(object sender, EventArgs e) { string strsavepath = txtsavepath.Text.ToString(); string strfilepath =textBox1.Text; if (textBox2.Text.Trim() == "") { MessageBox.Show("请输入要显示的文字"); textBox2.Focus(); return; } if (textBox1.Text.Trim() == "") { MessageBox.Show("请选择要处理的图片路径,图片必须是JPG格式"); return; } if (txtsavepath.Text.Trim() == "") { MessageBox.Show("请选择处理后的图片保存路径"); return; } if (textBox1.Text == strsavepath) { MessageBox.Show("处理后的图片保存目录不能和原目录相同"); txtsavepath.Text = ""; return ; } try { string[] font1 = txtfont.Text.Split(','); DirectoryInfo TheFolder = new DirectoryInfo(textBox1.Text); //遍历文件夹 int count = TheFolder.GetFiles().Length; int scount = 1; if (count > 0) { pb1.Maximum = count; pb1.Minimum = 1; pb1.Visible = true; } //遍历文件 foreach (FileInfo NextFile in TheFolder.GetFiles()) { pb1.Value = scount; string[] p1 = NextFile.Name.ToString().Split('.'); if (p1[1].ToString().ToLower() == "jpg" || p1[1] == "jpeg") { Makewatermark(NextFile.FullName, strsavepath + "\\" + NextFile.Name.ToString(), textBox2.Text, font1[0].ToString(), float.Parse(font1[1])); } scount++; } MessageBox.Show("处理完成!"); pb1.Visible = false; } catch (Exception ex) { MessageBox.Show(ex.Message); } } public void Makewatermark(string FilePath, string SavePath, string watermarkText,string fontname,float fontsize) { try { System.Drawing.Image image = System.Drawing.Image.FromFile(FilePath); int x = image.Width; int y = image.Height; Graphics g = Graphics.FromImage(image); g.DrawImage(image, 0, 0, image.Width, image.Height); Font f = new Font(fontname, fontsize ); Brush b = new SolidBrush(Color.Red ); string addText = watermarkText.Trim(); //g.DrawString(addText, f, b, x - 500, y - 50); g.DrawString(addText, f, b, x /4, y /2); g.Dispose(); image.Save(SavePath); image.Dispose(); //if (File.Exists(SavePath)) //{ // File.Delete(SavePath); //} } catch (Exception ex) { throw ex; } } private void butpath_Click(object sender, EventArgs e) { if (folderBrowserDialog1.ShowDialog() == DialogResult.OK ) { textBox1.Text = folderBrowserDialog1.SelectedPath; } } private void butsave_Click(object sender, EventArgs e) { if (folderBrowserDialog1.ShowDialog() == DialogResult.OK) { txtsavepath.Text = folderBrowserDialog1.SelectedPath; } } private void button2_Click(object sender, EventArgs e) { if (fontDialog1.ShowDialog() == DialogResult.OK) { txtfont.Text = fontDialog1.Font.Name.ToString()+","+ fontDialog1.Font.Size.ToString(); } } private void button3_Click(object sender, EventArgs e) { this.Close(); } } }
【文件预览】:
WinAddWave
----WinAddWave.sln(930B)
----WindowsApplication7()
--------bin()
--------Form1.Designer.cs(10KB)
--------Program.cs(483B)
--------obj()
--------Form1.cs(7KB)
--------Form1.resx(6KB)
--------ProjAddWave.csproj(3KB)
--------Properties()
----WinAddWave.suo(15KB)

网友评论