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

这是一个ASP.NET页面,用于接收用户上传的图片,然后根据预设条件检查图片类型是否正确。如果文件类型符合要求,程序会在服务器上保存图片,并创建带有文字和图片水印的缩略图。同时,它会显示一条消息,告知用户文件上传及处理的结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1<%@ 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>



1using 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;
11using System.IO;
12
13public 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}
194

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值