登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

三分天注定,七分靠打拼,爱拼才会赢!

推广网易博客、广交天下朋友.品荟酸甜苦辣、道尽家长里短!

 
 
 

日志

 
 

引用 给你的图片加上水印  

2008-07-03 18:45:49|  分类: 博客技巧 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

引用

贝壳昊子给你的图片加上水印
现在许多网站的新闻图片都有自己网站的一个水印标志是就也想做一个。 
给图片加水印的原理其实很简单。建立一个涂层。这个涂层上画上你要加的标记。设置好颜色和透明度后把这个涂层和原来的图片合在一起就可以了。下面就是这个函数的代码: 

这是一个通用函数可以发在任意你需要的地方。给函数传一个原始图片的Image对象。函数会返回一个加好水印的Image对象。然后怎么办就听你的了。

System.Drawing.Image CopyRight(System.Drawing.Image originalimage){

int height=originalimage.Height;     //得到图像的长

int width=originalimage.Width;       //得到图像的宽

int FontSize=50;                     //设置字体大小

int Point_x=1;                       //水印左上角的横坐标

int Point_y=1;                       //水银左上角的纵坐标

Graphics o_layer=Graphics.FromImage(originalimage); 

SizeF sizeofstring=new SizeF(width,height);             //此对象用来测量要加的水印的长宽

while(true)    //设置合适的字体大小

{

sizeofstring=o_layer.MeasureString("CopyRight 风雨岩",new Font("宋体",FontSize,FontStyle.Bold));

if (sizeofstring.Width

     break;

}

FontSize--;

}

 

//设置水印的起始点

if (width>sizeofstring.Width)

     Point_x=width-(int)sizeofstring.Width-10;

if (height>sizeofstring.Height)

     Point_y=height-(int)sizeofstring.Height-10;

//画水印,这里画笔的颜色大家可以根据自己的喜好修改

o_layer.DrawString("CopyRight 风雨岩",new Font("宋体",FontSize,FontStyle.Bold),new SolidBrush(Color.FromArgb(40,180,180,180)),new PointF(Point_x,Point_y));

 

o_layer.Save(); //保存水印到图像

return originalimage;

}

 

下面是使用这函数,在上传图片时给图片加水印的例子:

     这里只给出了后台页面的程序。在aspx页面中只要加一个 Panel和Button。ID分别是Panel1和Button1。即刻。这个例子通过修改Biuld_file函数中n的值可实现多文件同时上传。

using System;

using System.Collections;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.IO;

using System.Web;

using System.Web.SessionState;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.HtmlControls;

 

namespace GraphicsCopyRight

{

     /// 

     /// WebForm1 的摘要说明。

     /// 

     public class WebForm1 : System.Web.UI.Page

     {

         protected System.Web.UI.WebControls.Panel Panel1;

         protected System.Web.UI.WebControls.Button Button1;

     

         private void Page_Load(object sender, System.EventArgs e)

         {

              // 在此处放置用户代码以初始化页面

              Biuld_file(sender,e);

         }

         public void Biuld_file(object sender,System.EventArgs e)

         {

                   int n=1;

             

                   if(n>0)

                   {

                       for(int i=0;i

                       {

                                 HtmlInputFile hif =new HtmlInputFile();//初始化上传控件

                            Panel1.Controls.Add(hif);

              

                       }

                   }

         }

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

         override protected void OnInit(EventArgs e)

         {

              //

              // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。

              //

              InitializeComponent();

              base.OnInit(e);

         }

         

         /// 

         /// 设计器支持所需的方法 - 不要使用代码编辑器修改

         /// 此方法的内容。

         /// 

         private void InitializeComponent()

         {    

              this.Button1.Click += new System.EventHandler(this.Button1_Click);

              this.Load += new System.EventHandler(this.Page_Load);

 

         }

         #endregion

 

         private void Button1_Click(object sender, System.EventArgs e)

         {

              string fileinfor="";

              string path=Request.PhysicalApplicationPath;

              int numFile=Request.Files.Count;

              for(int i=0;i

              {

                   if (Request.Files.Get(i).ContentLength>0)

                   {

                       fileinfor=Request.Files.Get(i).FileName;

                       string filetype=Request.Files.Get(i).ContentType;

                       string filename=System.IO.Path.GetFileName(fileinfor);

                       fileinfor=System.IO.Path.Combine(System.IO.Path.Combine(path,"upload"),filename);

                       Stream fs = Request.Files.Get(i).InputStream;

                       System.Drawing.Image img=System.Drawing.Image.FromStream(fs,false);

                       System.Drawing.Image _img=CopyRight(img);

                       try

                       {

                            _img.Save(fileinfor);

                            Response.Write(filename+"上传成功!");

                       }

                       catch

                       {

                            Response.Write("上传失败!");

                       }

                   }

 

         }

 

     }

         System.Drawing.Image CopyRight(System.Drawing.Image originalimage){

              System.Drawing.Image o=originalimage;

              int height=o.Height;

              int width=o.Width;

              int FontSize=60;

              int Point_x=0;

              int Point_y=0;

              Graphics o_layer=Graphics.FromImage(o);

              SizeF sizeofstring=new SizeF(width,height);

              while(true)

              {

                   sizeofstring=o_layer.MeasureString("CopyRight 风雨岩",new Font("Georgia",FontSize,FontStyle.Bold));

                   if (sizeofstring.Width

                       break;

                   }

                   FontSize--;

              }

              if (width>sizeofstring.Width)

                   Point_x=width-(int)sizeofstring.Width-10;

              if (height>sizeofstring.Height)

                   Point_y=height-(int)sizeofstring.Height-10;

              o_layer.DrawString("CopyRight 风雨岩",new Font("新宋体",FontSize,FontStyle.Bold),new SolidBrush(Color.FromArgb(40,180,180,180)),new PointF(Point_x,Point_y));

              o_layer.Save();

              return o;

         }

     }

}

  评论这张
 
阅读(1076)| 评论(1)

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018