您好,欢迎光临本网站![请登录][注册会员]  
文件名称: 给图片加水印
  所属分类: C#
  开发工具:
  文件大小: 49kb
  下载次数: 0
  上传时间: 2013-03-11
  提 供 者: cf7***
 详细说明: 可以轻松给图片加文字水印 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 = ne w 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(); } } } ...展开收缩
(系统自动生成,下载前可以参看下载内容)

下载文件列表

相关说明

  • 本站资源为会员上传分享交流与学习,如有侵犯您的权益,请联系我们删除.
  • 本站是交换下载平台,提供交流渠道,下载内容来自于网络,除下载问题外,其它问题请自行百度
  • 本站已设置防盗链,请勿用迅雷、QQ旋风等多线程下载软件下载资源,下载后用WinRAR最新版进行解压.
  • 如果您发现内容无法下载,请稍后再次尝试;或者到消费记录里找到下载记录反馈给我们.
  • 下载后发现下载的内容跟说明不相乎,请到消费记录里找到下载记录反馈给我们,经确认后退回积分.
  • 如下载前有疑问,可以通过点击"提供者"的名字,查看对方的联系方式,联系对方咨询.
 相关搜索: 加水印源码
 输入关键字,在本站1000多万海量源码库中尽情搜索: