<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
&nbsp;
<table>
<tr>
<td style="width: 100px">
上传图片:</td>
<td style="width: 100px">
<asp:FileUpload ID="FileUpload1" runat="server" /></td>
<td style="width: 100px">
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="上传" /></td>
</tr>
<tr>
<td style="width: 100px" valign="top">
原图:</td>
<td style="width: 100px">
<asp:Image ID="Image1" runat="server" /></td>
<td style="width: 100px">
</td>
</tr>
<tr>
<td style="width: 100px" valign="top">
缩略图:</td>
<td style="width: 100px">
<asp:Image ID="Image2" runat="server" /></td>
<td style="width: 100px">
</td>
</tr>
<tr>
<td style="width: 100px" valign="top">
加水印:</td>
<td style="width: 100px">
<asp:Image ID="Image3" runat="server" /></td>
<td style="width: 100px">
</td>
</tr>
</table>
</div>
</form>
</body>
</html>

 

  1. using System;  
  2. using System.Data;  
  3. using System.Configuration;  
  4. using System.Web;  
  5. using System.Web.Security;  
  6. using System.Web.UI;  
  7. using System.Web.UI.WebControls;  
  8. using System.Web.UI.WebControls.WebParts;  
  9. using System.Web.UI.HtmlControls;  
  10. using System.Drawing;  
  11.   
  12. public partial class _Default : System.Web.UI.Page  
  13. {  
  14.     protected void Page_Load(object sender, EventArgs e)  
  15.     {  
  16.   
  17.     }  
  18.     protected void Button1_Click(object sender, EventArgs e)  
  19.     {  
  20.         string filename = FileUpload1.FileName;  
  21.         string nowpath = Server.MapPath(".") + "\\";  
  22.         filename = nowpath + filename;  
  23.   
  24.         //保存原图  
  25.         FileUpload1.SaveAs(filename);  
  26.   
  27.         System.Drawing.Image image, newimage, syimage;  
  28.         System.Drawing.Image.GetThumbnailImageAbort callb = null;  
  29.         image = System.Drawing.Image.FromFile(filename);  
  30.         syimage = System.Drawing.Image.FromFile(Server.MapPath(".") + "\\" + "缩略图.gif");//要目录下放一个"缩略图.gif"文件,可以从网上下载:http://www.baidu.com/img/baidu.gif  
  31.   
  32.         //保存缩略图  
  33.         newimage = image.GetThumbnailImage(100, 100, callb, new IntPtr());  
  34.         newimage.Save(filename + ".缩略图.png");  
  35.         newimage.Dispose();  
  36.   
  37.         //处理原图片  
  38.         Graphics g = Graphics.FromImage(image);  
  39.         Font f = new Font("隶书", 16);  
  40.         Brush b = new SolidBrush(ColorTranslator.FromHtml("#FF0000"));  
  41.         string addText = "文字水印内容";  
  42.         g.DrawString(addText, f, b, 10, 10);  
  43.         g.DrawImageUnscaled(syimage, 50, 50);  
  44.         //g.DrawImage(newimage,50,50,100,100);  
  45.         g.Dispose();  
  46.   
  47.         //生成水印图  
  48.         image.Save(filename + ".水印.png");  
  49.   
  50.         image.Dispose();  
  51.         syimage.Dispose();  
  52.   
  53.         Image1.ImageUrl = FileUpload1.FileName;  
  54.         Image2.ImageUrl = FileUpload1.FileName + ".缩略图.png";  
  55.         Image3.ImageUrl = FileUpload1.FileName + ".水印.png";  
  56.   
  57.     }  
  58. }  

 

posted on 2008-10-22 10:25  晃晃悠悠  阅读(269)  评论(0)    收藏  举报