ASP.NET上传图片生成缩略图,对上传的文件进行图片格式判断,若不符合上传要求,则弹出消息提示框。符合图片格式要求的图片上传后,生成三个图片,一个是缩略图,第二个是带文字水印的缩略图,第三个是第一张缩略图一半大小的小缩略图。
源代码
1.前端代码 imageUpload.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="imageUpload.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> <center> <form id="form1" runat="server"> <div style="margin-top:200px;width:600px;"> <fieldset style="width:240"> <legend>上传图片生成缩略图</legend> <table style="width: 400px"> <tr> <td style="width: 100px"> <asp:Image id="imgshow" runat="server" Width="200px"></asp:Image></td> <td style="width: 100px"> <asp:Image id="imgpri" runat="server" Width="200px"></asp:Image></td> <td style="width: 100px"> <asp:Image id="imgSmall" runat="server" Width="100px"></asp:Image></td> </tr> <tr> <td colspan="2"> <asp:FileUpload ID="FileUploadImage" runat="server" Width="393px" /></td> <td style="width: 100px"> <asp:button id="btnUpload" runat="server" Text="上传图片" OnClick="btnUpload_Click" Font-Size="10pt"></asp:button></td> </tr> </table> </fieldset> </div> </form> </center> </body> </html>
2.后台C#代码 imageUpload.aspx.cs
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
public partial class _Default : System.Web.UI.Page
{
string filename = "";
protected void Page_Load(object sender, EventArgs e)
{
this.imgshow.Visible = false;
this.imgpri.Visible = false;
this.imgSmall.Visible = false;
}
protected void btnUpload_Click(object sender, EventArgs e)
{
if (FileUploadImage.PostedFile.FileName != "")
{
string fileContentType = FileUploadImage.PostedFile.ContentType;
//对文件格式进行判断
if(fileContentType == "image/bmp" || fileContentType == "image/gif" || fileContentType == "image/pjpeg"||fileContentType == "image/jpeg"||fileContentType ==
"image/png")
{
//定义上传路径(在当前目录下的upload文件下)
string uploadpath = this.Server.MapPath("upload");
//取得文件名
string tmpfilename = FileUploadImage.PostedFile.FileName;
//文件名
filename = tmpfilename.Substring(tmpfilename.LastIndexOf("\\") + 1);
//原文件的保存路径
string fileSavePath = uploadpath + "\\" + filename;
//保存原图片
FileUploadImage.SaveAs(fileSavePath);
//调用生成缩略图程序,生成缩略图及生成写字的图片
this.toImage(FileUploadImage.PostedFile.InputStream, uploadpath, filename);
//求取后缀名
string suffix = filename.Substring(filename.LastIndexOf("."));
//显示图片
//分别为原图片/写字的图片(多一个w)/缩略图(多一个x)
this.imgshow.ImageUrl = "~/demo/upload/" + filename;
this.imgpri.ImageUrl = "~/demo/upload/" + filename.Replace(suffix, "w" + suffix);
this.imgSmall.ImageUrl = "~/demo/upload/" + filename.Replace(suffix, "x" + suffix);
//显示图像控件
this.imgshow.Visible = true;
this.imgpri.Visible = true;
this.imgSmall.Visible = true;
}
else
{
ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('请上传bmp/gif/jpg/png图片格式文件!');</script>");
}
}
}
/**/
/// <summary>
/// 生成缩略图程序
/// </summary>
/// <param name="myStream">取到的流文件对象</param>
/// <param name="uploadPath">要保存的路径</param>
/// <param name="picName">上传的图片原文件名</param>
private void toImage(Stream myStream, string uploadPath, string picName)
{
//==============================生成缩略图====================================
//取得后缀名
string suffix = picName.Substring(picName.LastIndexOf("."));
//缩略图的保存路径
string fileXltPath = uploadPath + "\\" + picName.Replace(suffix, "x" + suffix);
//写字图的保存路径
string fileXztPath = uploadPath + "\\" + picName.Replace(suffix, "w" + suffix);
//创建一个图像对象取得上传图片对象
System.Drawing.Image myImage = System.Drawing.Image.FromStream(myStream, false);
//对绘制前的图片产生一个缩略图(原图片一半大小)
System.Drawing.Image thumbImage = myImage.GetThumbnailImage(myImage.Size.Width / 2, myImage.Size.Height / 2, null, System.IntPtr.Zero);
//保存缩略图
thumbImage.Save(fileXltPath, this.getImageFormat(suffix));
//关闭缩略图对象
thumbImage.Dispose();
//==============================绘制上传图片上的文字========================
//创建绘制对象
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(myImage);
g.DrawImage(myImage, 0, 0, myImage.Size.Width, myImage.Size.Height);
//选择字体及字体大小
System.Drawing.Font f = new Font("隶书", 40);
//定义字体颜色
System.Drawing.Brush b = new SolidBrush(System.Drawing.Color.HotPink);
//开始绘制,根据上述两种设定,添加绘制的上左位置
g.DrawString("weisico.com", f, b, 10, 10);
//关闭绘制对象
g.Dispose();
//保存绘制后上传图片
myImage.Save(fileXztPath, myImage.RawFormat);
//关闭图片对象
myImage.Dispose();
}
/// <summary>
/// 根据图片的后缀名,返回要保存的图片格式
/// </summary>
/// <param name="suffix">带.号的后缀名</param>
/// <returns>返回System.Drawing.Imaging.ImageForma对象</returns>
private System.Drawing.Imaging.ImageFormat getImageFormat(string suffix)
{
System.Drawing.Imaging.ImageFormat myFormat;
switch (suffix.ToLower())
{
case ".bmp":
myFormat = System.Drawing.Imaging.ImageFormat.Bmp;
break;
case ".emf":
myFormat = System.Drawing.Imaging.ImageFormat.Emf;
break;
case ".exif":
myFormat = System.Drawing.Imaging.ImageFormat.Exif;
break;
case ".gif":
myFormat = System.Drawing.Imaging.ImageFormat.Gif;
break;
case ".icon":
myFormat = System.Drawing.Imaging.ImageFormat.Icon;
break;
case ".jpeg":
case ".jpg":
myFormat = System.Drawing.Imaging.ImageFormat.Jpeg;
break;
case ".png":
myFormat = System.Drawing.Imaging.ImageFormat.Png;
break;
case ".tiff":
myFormat = System.Drawing.Imaging.ImageFormat.Tiff;
break;
case ".wmf":
myFormat = System.Drawing.Imaging.ImageFormat.Wmf;
break;
default:
myFormat = System.Drawing.Imaging.ImageFormat.MemoryBmp;
break;
}
return (myFormat);
}
}
说明:源代码中的文件是放在IIS根目录下的demo文件夹内,同时在demo文件夹内新建upload文件夹,存放上传的图片文件。若修改了存放文件位置,注意修改程序中的文件存放路径。