使用FileUpload控件上传图片并自动生成缩略图、带文字和图片的水印图

翻译|其它|编辑:郝浩|2007-09-04 10:46:03.000|阅读 1529 次

概述:

# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>

<% @ Page Language="C#" AutoEventWireup="true" CodeFile="upfile.aspx.cs" Inherits="upfile_upfile" %>

 2
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 4
 5 <html xmlns="http://www.w3.org/1999/xhtml" >
 6 <head runat="server">
 7   <title>无标题页</title>
 8 </head>
 9 <body>
10   <form id="form1" runat="server">
11   <div>
12     <asp:FileUpload ID="FileUpload1" runat="server" />
13     <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="上传" /><br />
14     <asp:Label ID="Label1" runat="server"></asp:Label></div>
15   </form>
16 </body>
17 </html>

 

 

  1 using System;
  2 using System.Data;
  3 using System.Configuration;
  4 using System.Collections;
  5 using System.Web;
  6 using System.Web.Security;
  7 using System.Web.UI;
  8 using System.Web.UI.WebControls;
  9 using System.Web.UI.WebControls.WebParts;
 10 using System.Web.UI.HtmlControls;
 11 using System.IO;
 12
 13 public partial class upfile_upfile : System.Web.UI.Page
 14 {
 15     protected void Page_Load(object sender, EventArgs e)
 16      { }
 17
 18     protected void Button1_Click(object sender, EventArgs e)
 19      {
 20         if (FileUpload1.HasFile)
 21          {
 22             string fileContentType = FileUpload1.PostedFile.ContentType;
 23             if (fileContentType == "image/bmp" || fileContentType == "image/gif" || fileContentType == "image/pjpeg")
 24              {
 25                 string name = FileUpload1.PostedFile.FileName; // 
客户端文件路径
 26
 27                 FileInfo file = new FileInfo(name);
 28                 string fileName = file.Name; // 文件名称
 29                 string fileName_s = "s_" + file.Name; // 缩略图文件名称
 30                 string fileName_sy = "sy_" + file.Name; // 水印图文件名称(文字)
 31                 string fileName_syp = "syp_" + file.Name; // 水印图文件名称(图片)
 32                 string webFilePath = Server.MapPath("file/" + fileName); // 服务器端文件路径
 33                 string webFilePath_s = Server.MapPath("file/" + fileName_s);  // 服务器端缩略图路径
 34                 string webFilePath_sy = Server.MapPath("file/" + fileName_sy); // 服务器端带水印图路径(文字)
 35                 string webFilePath_syp = Server.MapPath("file/" + fileName_syp); // 服务器端带水印图路径(图片)
 36                 string webFilePath_sypf = Server.MapPath("file/shuiyin.jpg"); // 服务器端水印图路径(图片)
 37
 38                 if (!File.Exists(webFilePath))
 39                  {
 40                     try
 41                      {
 42                         FileUpload1.SaveAs(webFilePath); // 使用 SaveAs 方法保存文件
 43                         AddShuiYinWord(webFilePath, webFilePath_sy);
 44                         AddShuiYinPic(webFilePath, webFilePath_syp, webFilePath_sypf);
 45                         MakeThumbnail(webFilePath, webFilePath_s, 130, 130, "Cut"); // 生成缩略图方法
 46                         Label1.Text = "提示:文件“" + fileName + "”成功上传,并生成“" + fileName_s + "”缩略图,文件类型为:" +
 47                     FileUpload1.PostedFile.ContentType + ",文件大小为:" + FileUpload1.PostedFile.ContentLength + "B";
 48                     }
 49                     catch (Exception ex)
 50                      {
 51                         Label1.Text = "提示:文件上传失败,失败原因:" + ex.Message;
 52                     }
 53                 }
 54                 else
 55                  {
 56                     Label1.Text = "提示:文件已经存在,请重命名后上传";
 57                 }
 58             }
 59             else
 60              {
 61                 Label1.Text = "提示:文件类型不符";
 62             }
 63         }
 64     }
 65     /**//**/
 66     /**//// <summary>
 67     /// 生成缩略图
 68     /// </summary>
 69     /// <param name="originalImagePath">源图路径(物理路径)</param>
 70     /// <param name="thumbnailPath">缩略图路径(物理路径)</param>
 71     /// <param name="width">缩略图宽度</param>
 72     /// <param name="height">缩略图高度</param>
 73     /// <param name="mode">生成缩略图的方式</param>   
 74     public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode)
 75      {
 76         System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath);
 77
 78         int towidth = width;
 79         int toheight = height;
 80
 81         int x = 0;
 82         int y = 0;
 83         int ow = originalImage.Width;
 84         int oh = originalImage.Height;
 85
 86         switch (mode)
 87          {
 88             case "HW"://
指定高宽缩放(可能变形)
 89                 break;
 90             case "W"://指定宽,高按比例
 91                 toheight = originalImage.Height * width / originalImage.Width;
 92                 break;
 93             case "H"://指定高,宽按比例
 94                 towidth = originalImage.Width * height / originalImage.Height;
 95                 break;
 96             case "Cut"://指定高宽裁减(不变形)
 97                 if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
 98                  {
 99                     oh = originalImage.Height;
100                     ow = originalImage.Height * towidth / toheight;
101                     y = 0;
102                     x = (originalImage.Width - ow) / 2;
103                 }
104                 else
105                  {
106                     ow = originalImage.Width;
107                     oh = originalImage.Width * height / towidth;
108                     x = 0;
109                     y = (originalImage.Height - oh) / 2;
110                 }
111                 break;
112             default:
113                 break;
114         }
115
116         //新建一个bmp图片
117         System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);
118
119         //新建一个画板
120         System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
121
122         //设置高质量插值法
123         g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
124
125         //设置高质量,低速度呈现平滑程度
126         g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
127
128         //清空画布并以透明背景色填充
129         g.Clear(System.Drawing.Color.Transparent);
130
131         //在指定位置并且按指定大小绘制原图片的指定部分
132         g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, towidth, toheight),
133             new System.Drawing.Rectangle(x, y, ow, oh),
134             System.Drawing.GraphicsUnit.Pixel);
135
136         try
137          {
138             //jpg格式保存缩略图
139             bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
140         }
141         catch (System.Exception e)
142          {
143             throw e;
144         }
145         finally
146          {
147             originalImage.Dispose();
148             bitmap.Dispose();
149             g.Dispose();
150         }
151     }
152
153     /**//**/
154     /**//// <summary>
155     /// 在图片上增加文字水印
156     /// </summary>
157     /// <param name="Path">原服务器图片路径</param>
158     /// <param name="Path_sy">生成的带文字水印的图片路径</param>
159     protected void AddShuiYinWord(string Path, string Path_sy)
160      {
161         string addText = "
测试水印";
162         System.Drawing.Image image = System.Drawing.Image.FromFile(Path);
163         System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image);
164         g.DrawImage(image, 0, 0, image.Width, image.Height);
165         System.Drawing.Font f = new System.Drawing.Font("Verdana", 16);
166         System.Drawing.Brush b = new System.Drawing.SolidBrush(System.Drawing.Color.Blue);
167
168         g.DrawString(addText, f, b, 15, 15);
169         g.Dispose();
170
171         image.Save(Path_sy);
172         image.Dispose();
173     }
174
175     /**//**/
176     /**//// <summary>
177     /// 在图片上生成图片水印
178     /// </summary>
179     /// <param name="Path">原服务器图片路径</param>
180     /// <param name="Path_syp">生成的带图片水印的图片路径</param>
181     /// <param name="Path_sypf">水印图片路径</param>
182     protected void AddShuiYinPic(string Path, string Path_syp, string Path_sypf)
183      {
184         System.Drawing.Image image = System.Drawing.Image.FromFile(Path);
185         System.Drawing.Image copyImage = System.Drawing.Image.FromFile(Path_sypf);
186         System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image);
187         g.DrawImage(copyImage, new System.Drawing.Rectangle(image.Width - copyImage.Width, image.Height - copyImage.Height, copyImage.Width, copyImage.Height), 0, 0, copyImage.Width, copyImage.Height, System.Drawing.GraphicsUnit.Pixel);
188         g.Dispose();
189
190         image.Save(Path_syp);
191         image.Dispose();
192     }
193 }

 


标签:

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@evget.com

文章转载自:csdn

为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP