<%@ 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> <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>
- using System;
- using System.Data;
- using System.Configuration;
- using System.Web;
- using System.Web.Security;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Web.UI.WebControls.WebParts;
- using System.Web.UI.HtmlControls;
- using System.Drawing;
- public partial class _Default : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- }
- protected void Button1_Click(object sender, EventArgs e)
- {
- string filename = FileUpload1.FileName;
- string nowpath = Server.MapPath(".") + "\\";
- filename = nowpath + filename;
- //保存原图
- FileUpload1.SaveAs(filename);
- System.Drawing.Image image, newimage, syimage;
- System.Drawing.Image.GetThumbnailImageAbort callb = null;
- image = System.Drawing.Image.FromFile(filename);
- syimage = System.Drawing.Image.FromFile(Server.MapPath(".") + "\\" + "缩略图.gif");//要目录下放一个"缩略图.gif"文件,可以从网上下载:http://www.baidu.com/img/baidu.gif
- //保存缩略图
- newimage = image.GetThumbnailImage(100, 100, callb, new IntPtr());
- newimage.Save(filename + ".缩略图.png");
- newimage.Dispose();
- //处理原图片
- Graphics g = Graphics.FromImage(image);
- Font f = new Font("隶书", 16);
- Brush b = new SolidBrush(ColorTranslator.FromHtml("#FF0000"));
- string addText = "文字水印内容";
- g.DrawString(addText, f, b, 10, 10);
- g.DrawImageUnscaled(syimage, 50, 50);
- //g.DrawImage(newimage,50,50,100,100);
- g.Dispose();
- //生成水印图
- image.Save(filename + ".水印.png");
- image.Dispose();
- syimage.Dispose();
- Image1.ImageUrl = FileUpload1.FileName;
- Image2.ImageUrl = FileUpload1.FileName + ".缩略图.png";
- Image3.ImageUrl = FileUpload1.FileName + ".水印.png";
- }
- }