<%@ 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> 上传图片:<asp:FileUpload ID="uploadImage" runat="server" /> <br /> 生成图片宽度:<asp:TextBox ID="TextBox1" runat="server">500</asp:TextBox> <br /> 生成图片高度:<asp:TextBox ID="TextBox2" runat="server">500</asp:TextBox> <br /> 每行显示字符数:<asp:TextBox ID="TextBox4" runat="server">20</asp:TextBox> <br /> 水印文字:<asp:TextBox ID="TextBox3" runat="server" TextMode="MultiLine">在人生的道路上,如果你没有耐心去等待成功的到来,那么你只好用一生的时间去等待失败!</asp:TextBox> <br /> <asp:Button ID="Button1" runat="server" οnclick="Button1_Click" Text="提交" /> <br /> </div> </form> </body> </html> using System; using System.Collections; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { HttpPostedFile hpf = uploadImage.PostedFile; //取得文件名,不含路径 char[] splitChar = { '//' }; string[] FilenameArray = hpf.FileName.Split(splitChar); string Filename = FilenameArray[FilenameArray.Length - 1].ToLower(); //将用户输入的水印文字处理 //string sMessage = lineStr(TextBox3.Text.Trim().ToString(), 20); if (hpf.FileName.Length < 1) { Response.Write("请选择你要上传的图片文件"); return; } if (hpf.ContentType != "image/pjpeg" && hpf.ContentType != "image/gif")//5~1-a-s-p-x { Response.Write("只允许上传JPEG GIF文件"); return; } else { System.Text.StringBuilder sb = new System.Text.StringBuilder(); sb.Append(DateTime.Now.Year.ToString()); sb.Append(DateTime.Now.Month.ToString()); sb.Append(DateTime.Now.Day.ToString()); sb.Append(DateTime.Now.Hour.ToString()); sb.Append(DateTime.Now.Minute.ToString()); sb.Append(DateTime.Now.Second.ToString()); if (Filename.ToLower().EndsWith("gif")) sb.Append(".gif"); else if (Filename.ToLower().EndsWith("jpg")) sb.Append(".jpg"); else if (Filename.ToLower().EndsWith("jpeg")) sb.Append(".jpeg"); Filename = sb.ToString(); //保存图片到服务器上 try { hpf.SaveAs(Server.MapPath("~") + "/Image/" + Filename); } catch (Exception ee) { Response.Write("上传图片失败,原因:" + ee.Message); return; } //生成缩略图 //原始图片名称 string originalFilename = hpf.FileName; //生成高质量图片名称 string strFile = Server.MapPath("~") + "/Image/Small_" + Filename; //从文件获取图片对象 System.Drawing.Image image = System.Drawing.Image.FromStream(hpf.InputStream, true); Double Width = Double.Parse(TextBox1.Text.Trim()); Double Height = Double.Parse(TextBox2.Text.Trim()); System.Double newWidth, newHeight; if (image.Width > image.Height) { newWidth = Width; newHeight = image.Height * (newWidth / image.Width); } else { newHeight = Height; newWidth = image.Width * (newHeight / image.Height); } if (newWidth > Width) newWidth = Width; if (newHeight > Height) newHeight = Height; System.Drawing.Size size = new System.Drawing.Size((int)newWidth, (int)newHeight); //设置图片的宽度和高度 System.Drawing.Image bitmap = new System.Drawing.Bitmap(size.Width, size.Height); //新建bmp图片 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(System.Drawing.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); //文字水印 System.Drawing.Graphics testGrahpics = System.Drawing.Graphics.FromImage(bitmap); System.Drawing.Font font = new System.Drawing.Font("宋体", 10); System.Drawing.Brush brush = new System.Drawing.SolidBrush(System.Drawing.Color.Black); //分行 string sInput = TextBox3.Text.Trim().ToString(); //获取输入的水印文字 int coloum = Convert.ToInt32(TextBox4.Text); //获取每行的字符数 //利用循环,来依次输出 for (int i = 0, j = 0; i < sInput.Length; i += coloum, j++) { //若要修改水印文字在照片上的位置,可将20修改成你想要的任何值 if (j != sInput.Length / coloum) { string s = sInput.Substring(i, coloum); testGrahpics.DrawString(s, font, brush, 20, 20 * (i / coloum + 1)); } else { string s = sInput.Substring(i, sInput.Length % coloum); testGrahpics.DrawString(s, font, brush, 20, 20*(j+1)); } } testGrahpics.Dispose(); //保存缩略图c try { bitmap.Save(strFile, System.Drawing.Imaging.ImageFormat.Jpeg); } catch (Exception ex) { Response.Write("保存缩略图失败" + ex.Message); } //释放资源 g.Dispose(); bitmap.Dispose(); image.Dispose(); } } } using System; using System.Data; using System.Configuration; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; using System.Drawing; using System.Drawing.Imaging; using System.IO; /// <summary> ///DrawHelper 的摘要说明 /// </summary> public class DrawHelper { public DrawHelper() { } //生成缩略图 public void CreateThumbnailImage(string sFileSrcPath, string sFileDstPath, int iSizeLimit) { //检测源图片是否存在 if (File.Exists(sFileSrcPath)) { //获取源图片图像 System.Drawing.Image image = System.Drawing.Image.FromFile(sFileSrcPath); //定义一个大小结构 SizeF size = new SizeF(image.Width, image.Height); //计算出符合要求的大小 while (size.Width > iSizeLimit || size.Height > iSizeLimit)//51+aspx { size.Width /= 1.1F; size.Height /= 1.1F; } //创建缩略图图像 Bitmap bitmap = new Bitmap(Convert.ToInt16(size.Width), Convert.ToInt16(size.Height)); //创建缩略图绘画面 Graphics g = Graphics.FromImage(bitmap); //清除整个绘画面并以透明色填充 g.Clear(Color.Transparent); //定义源图像矩形区域 Rectangle Srcrect = new Rectangle(0, 0, image.Width, image.Height); //定义缩略图矩形区域 Rectangle Dstrect = new Rectangle(0, 0, bitmap.Width, bitmap.Height); //绘制缩略图 g.DrawImage(bitmap, Dstrect, Srcrect, GraphicsUnit.Pixel); //保存为Jpeg图片 bitmap.Save(sFileDstPath, ImageFormat.Jpeg); //释放对象 g.Dispose(); image.Dispose(); bitmap.Dispose(); } } }