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

上传人:平*** 文档编号:16348759 上传时间:2017-11-07 格式:DOCX 页数:6 大小:18.10KB
返回 下载 相关 举报
用FileUpload控件上传图片并自动生成缩略图、带文字和图片的水印图_第1页
第1页 / 共6页
用FileUpload控件上传图片并自动生成缩略图、带文字和图片的水印图_第2页
第2页 / 共6页
用FileUpload控件上传图片并自动生成缩略图、带文字和图片的水印图_第3页
第3页 / 共6页
用FileUpload控件上传图片并自动生成缩略图、带文字和图片的水印图_第4页
第4页 / 共6页
用FileUpload控件上传图片并自动生成缩略图、带文字和图片的水印图_第5页
第5页 / 共6页
点击查看更多>>
资源描述

《用FileUpload控件上传图片并自动生成缩略图、带文字和图片的水印图》由会员分享,可在线阅读,更多相关《用FileUpload控件上传图片并自动生成缩略图、带文字和图片的水印图(6页珍藏版)》请在金锄头文库上搜索。

1、234567 标题在这里8910 11 12 13 14 15 16171using System;2using System.Data;3using System.Configuration;4using System.Collections;5using System.Web;6using System.Web.Security;7using System.Web.UI;8using System.Web.UI.WebControls;9using System.Web.UI.WebControls.WebParts;10using System.Web.UI.HtmlControls;1

2、1using System.IO;1213public partial class upfile_upfile : System.Web.UI.Page1415 protected void Page_Load(object sender, EventArgs e)16 1718 protected void Button1_Click(object sender, EventArgs e) 19 20 if (FileUpload1.HasFile)21 22 string fileContentType = FileUpload1.PostedFile.ContentType;23 if

3、(fileContentType = image/bmp | fileContentType = image/gif | fileContentType = image/pjpeg)24 25 string name = FileUpload1.PostedFile.FileName; / 客户端文件路径2627 FileInfo file = new FileInfo(name);28 string fileName = file.Name; / 文件名称29 string fileName_s = s_ + file.Name; / 缩略图文件名称30 string fileName_sy

4、 = 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);/ 服务器端带水

5、印图路径(文字 )35 string webFilePath_syp = Server.MapPath(file/ + fileName_syp);/ 服务器端带水印图路径(图片)36 string webFilePath_sypf = Server.MapPath(file/shuiyin.jpg);/ 服务器端水印图路径(图片)3738 if (!File.Exists(webFilePath)39 40 try41 42 FileUpload1.SaveAs(webFilePath); / 使用 SaveAs 方法保存文件43 AddShuiYinWord(webFilePath, we

6、bFilePath_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.Co

7、ntentLength + B;48 49 catch (Exception ex)50 51 Label1.Text = 提示:文件上传失败,失败原因: + ex.Message;52 53 54 else55 56 Label1.Text = 提示:文件已经存在,请重命名后上传 ;57 58 59 else60 61 Label1.Text = 提示:文件类型不符 ;62 63 64 65 /*/*/66 /*/ 67 / 生成缩略图68 / 69 / 源图路径(物理路径) 70 / 缩略图路径(物理路径)71 / 缩略图宽度72 / 缩略图高度73 / 生成缩略图的方式 74 publi

8、c static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode)75 76 System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath); 7778 int towidth = width;79 int toheight = height;8081 int x = 0;82 int y = 0;83 int ow = origin

9、alImage.Width;84 int oh = originalImage.Height;8586 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

10、:/指定高宽裁减(不变形) 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 else105 106 ow = originalImage.Width;107 oh =

11、originalImage.Width * height / towidth;108 x = 0;109 y = (originalImage.Height - oh) / 2;110 111 break;112 default:113 break;114 115116 /新建一个 bmp 图片117 System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);118119 /新建一个画板120 System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);121122 /设置高质量插值法123 g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;124125 /设置高质量,低速度呈现平滑程度126 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;127128 /清空画布并以透明背景色填充129 g.Clear(System.Drawing.Color.Transparent);130131 /

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 办公文档 > 其它办公文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号