活动介绍
file-type

C#实现图片上传及添加水印与缩略图功能

下载需积分: 15 | 477KB | 更新于2025-07-26 | 141 浏览量 | 17 下载量 举报 收藏
download 立即下载
在深入探讨C#中如何生成图片上传后的缩略图以及添加文字和图片水印的具体实现之前,首先我们来了解几个相关的核心概念。 **图片上传** 图片上传是指用户通过Web页面或者应用程序将图片文件发送到服务器的过程。在ASP.NET环境下,通常使用Html控件中的`<input type="file">`来让用户选择本地文件进行上传,然后通过服务器端的处理机制,如`HttpRequest.Files`集合,来接收上传的文件。 **生成缩略图** 生成缩略图是在服务器端通过编程方式对上传的图片进行处理,创建一个尺寸较小的新图片,以减少图片的存储空间和传输时间,同时也便于在页面上显示。在C#中,通常使用.NET Framework中的`System.Drawing`命名空间下的类来实现图片处理的功能。 **文字水印** 文字水印是指在图片上添加文字信息,通常用于版权声明或其他信息标识。在C#中,可以在创建缩略图的基础上,利用`Graphics`类来绘制文字,将其渲染到图片上。 **图片水印** 图片水印是指在原始图片上覆盖一张小图片,使其成为图片的一部分。这通常是为了品牌标识或其他视觉效果。在C#中实现图片水印,同样需要使用`Graphics`类来实现图片的叠加。 **技术实现** 下面,将介绍如何在C#中实现上述功能。 1. **读取上传图片** 首先,从`HttpRequest.Files`集合中获取上传的图片文件,并确定其格式和大小。示例代码如下: ```csharp HttpPostedFile postedFile = Request.Files["imageFile"]; string fileExtension = System.IO.Path.GetExtension(postedFile.FileName); ``` 2. **创建缩略图** 创建缩略图需要确定目标尺寸,然后使用`Bitmap`类来创建新的图片实例。示例代码如下: ```csharp public static Bitmap CreateThumbnail(HttpPostedFile postedFile, int width, int height) { // 获取上传图片的尺寸 var originalSize = new Size(postedFile.InputStream.Width, postedFile.InputStream.Height); var thumbnailSize = new Size(width, height); // 创建缩略图 Bitmap thumbnail = new Bitmap(thumbnailSize.Width, thumbnailSize.Height); // 设置绘图质量 Graphics graphics = Graphics.FromImage(thumbnail); graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; // 绘制缩略图 graphics.DrawImage(postedFile.InputStream, new Rectangle(0, 0, width, height), 0, 0, originalSize.Width, originalSize.Height, GraphicsUnit.Pixel); return thumbnail; } ``` 3. **添加文字水印** 在生成的缩略图上添加文字水印,需要指定文字内容、字体、颜色等。示例代码如下: ```csharp public static void AddTextWatermark(Bitmap thumbnail, string watermarkText, string fontName, int fontSize, Color textColor) { using (Graphics graphics = Graphics.FromImage(thumbnail)) { // 设置透明度 graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; // 创建文字格式 SolidBrush brush = new SolidBrush(textColor); Font drawFont = new Font(fontName, fontSize); StringFormat format = new StringFormat(); format.Alignment = StringAlignment.Center; format.LineAlignment = StringAlignment.Center; // 绘制文字 graphics.DrawString(watermarkText, drawFont, brush, new Rectangle(0, 0, thumbnail.Width, thumbnail.Height), format); } } ``` 4. **添加图片水印** 添加图片水印是通过叠加一张小图片到缩略图上来实现的。示例代码如下: ```csharp public static void AddImageWatermark(Bitmap thumbnail, Bitmap watermarkImage) { using (Graphics graphics = Graphics.FromImage(thumbnail)) { // 设置透明度 graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; // 设置水印图片位置 int x = (thumbnail.Width - watermarkImage.Width) / 2; int y = (thumbnail.Height - watermarkImage.Height) / 2; // 绘制水印图片 graphics.DrawImage(watermarkImage, x, y); } } ``` 综上所述,通过上述步骤可以实现图片上传后的缩略图生成以及文字和图片水印的添加。在实际应用中,还需要考虑异常处理、性能优化、安全检查等多方面的因素,以确保系统的健壮性和用户体验。 **项目结构说明** 在提供的文件名称列表中,`Default.aspx`和`Default.aspx.cs`是对应的ASP.NET页面和代码后端文件,`Web.config`是网站配置文件,而`.csproj`和`.csproj.user`文件则分别代表了C#项目文件和用户相关的配置文件。`51aspxImgUpload.sln`是解决方案文件,用来在Visual Studio中打开整个项目。`51aspx源码必读.txt`可能是对源码的说明文档,`最新Asp.Net源码下载.url`可能是提供一个链接到最新ASP.NET源码下载的快捷方式。 这些文件共同构成了一个典型的ASP.NET项目结构,该结构使得开发者能够管理、组织和构建使用ASP.NET技术开发的Web应用程序。

相关推荐