/给图片打水印

来源:互联网 发布:手机淘宝好评在哪里找 编辑:程序博客网 时间:2024/04/28 03:33

 前台html页:
<body MS_POSITIONING="GridLayout">
  <form id="Form1" method="post" runat="server">
   <table>
    <TR>
     <TD vAlign="middle" noWrap align="center" height="10">图片:</TD>
     <TD vAlign="middle" noWrap align="left" colSpan="2" height="10"><INPUT id="picFile" style="WIDTH: 216px; HEIGHT: 22px" type="file" size="16" name="logPath"
       runat="server">
      <asp:label id="lblErr" runat="server" CssClass="lb_msg"></asp:label></TD>
     <TD class="lb_msg" noWrap align="left" width="200" height="14">&nbsp;注意:上传文件名称不要用特殊符号和空格,例如"#;".</TD>
    </TR>
    <TR>
     <TD noWrap align="center" height="28">图片水印选择:</TD>
     <TD class="lb_msg" noWrap align="left" colSpan="3" height="28"><asp:dropdownlist id="dlType" runat="server" Width="100px">
       <asp:ListItem Value="font" Selected="True">文字水印</asp:ListItem>
       <asp:ListItem Value="img">图片水印</asp:ListItem>
      </asp:dropdownlist>
      <asp:Button id="Button1" runat="server" Text="Button"></asp:Button></TD>
    </TR>
   </table>
  </form>
</body>

 

后台cs代码:
#region 验证图片类型
  private string checkImageType(string str)
  {
   string strCheck;
   switch(str)
   {
    case "gif":
     strCheck="ok";
     break;
    case "jpg":
     strCheck="ok";
     break;
    case "bmp":
     strCheck="ok";
     break;
    case "png":
     strCheck="ok";
     break;
    case "GIF":
     strCheck="ok";
     break;
    case "JPG":
     strCheck="ok";
     break;
    case "BMP":
     strCheck="ok";
     break;
    case "PNG":
     strCheck="ok";
     break;
    default:
     strCheck="no";
     break;
   }
   return strCheck;
  }
  #endregion

//给图片打水印的方法(图片和文字两种)
  public void MakeSmallImg(System.Web.HttpPostedFile postFile, string saveImg, System.Double Width, System.Double Height,string strType)
  {
   //原始图片名称
   string originalFilename = postFile.FileName;
   //生成的高质量图片名称
   string strGoodFile = saveImg;
   //从文件取得图片对象
   System.Drawing.Image image = System.Drawing.Image.FromStream(postFile.InputStream, true);
   System.Double NewWidth, NewHeight;
   if (image.Width > image.Height)
   {
    NewWidth = Width;
    NewHeight = image.Height * (NewWidth / image.Width);
   }
   else
   {
    NewHeight = Height;
    NewWidth = (NewHeight / image.Height) * image.Width;
   }

   if (NewWidth > Width)
   {
    NewWidth = Width;
   }
   if (NewHeight > Height)
   {
    NewHeight = Height;
   }

   //取得图片大小
   System.Drawing.Size size = new Size((int)NewWidth, (int)NewHeight);
   //新建一个bmp图片
   System.Drawing.Image bitmap = new System.Drawing.Bitmap(size.Width, size.Height);
   //新建一个画板
   System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
   //设置高质量插值法
   g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
   //设置高质量,低速度呈现平滑程度
   g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
   //清空一下画布
   g.Clear(Color.White);
   //在指定位置画图
   g.DrawImage(image, new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height),
    new System.Drawing.Rectangle(0, 0, image.Width, image.Height),
    System.Drawing.GraphicsUnit.Pixel);
   if(strType=="font")
   {
    //文字水印
    System.Drawing.Graphics G=System.Drawing.Graphics.FromImage(bitmap);
    System.Drawing.Font f=new Font("华文彩云",12);
    System.Drawing.Brush b=new SolidBrush(Color.White);
    G.DrawString("www.fsxj.com",f,b,10,10);
    G.Dispose();
   }
   else
   {
    ///图片水印
    System.Drawing.Image copyImage = System.Drawing.Image.FromFile(System.Web.HttpContext.Current.Server.MapPath("Upload/loogo.JPG"));
    Graphics a = Graphics.FromImage(bitmap);//10,10
    a.DrawImage(copyImage, new Rectangle(bitmap.Width - copyImage.Width, bitmap.Height - copyImage.Height, copyImage.Width, copyImage.Height), 0, 0, copyImage.Width, copyImage.Height, GraphicsUnit.Pixel);
    copyImage.Dispose();
    a.Dispose();
    copyImage.Dispose();
   }

   //保存高清晰度的缩略图
   bitmap.Save(strGoodFile,System.Drawing.Imaging.ImageFormat.Jpeg);
   g.Dispose();
  }

//生成按钮
private void Button1_Click(object sender, System.EventArgs e)
{
   string strLogpic="";
   if(this.picFile.Value.ToString().ToString().Length>0)
   {
    string strFile= System.IO.Path.GetFileName(this.picFile.PostedFile.FileName).ToString();
    string str=strFile.Split('.')[strFile.Split(',').Length].ToString();
    if(this.checkImageType(str)=="ok")
    {
     strLogpic="Upload/" +  System.IO.Path.GetFileName(this.picFile.PostedFile.FileName).ToString();
    }
    else
    {
     this.Response.Write("<script language=javascript>window.alert('文件类型错误,请重新填写。');</script>"); 
     strLogpic="";
     return;
    }    
   }

   if(strLogpic.Length>0)
   {
    this.MakeSmallImg(this.picFile.PostedFile,this.Server.MapPath(strLogpic),200,210,this.dlType.SelectedValue.Trim().ToString());
    this.lblErr.Text="上传水印图片成功!";
   }
 }

 

原创粉丝点击