asp.net 上传图片并生成高清缩略图
pic.aspx文件:
<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>
pic.aspx.cs文件:
using System.Drawing; //导入命名空间 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; //获取MIME类型 if (fileContentType == "image/bmp" || fileContentType == "image/gif" || fileContentType == "image/pjpeg" || fileContentType == "image/jpeg" || fileContentType == "image/png") { //定义上传路径(在当前目录下的uploadfile文件下) string uploadpath = this.Server.MapPath("uploadfile"); //取得文件名 string tmpfilename = FileUploadImage.PostedFile.FileName; //文件名 string filename = tmpfilename.Substring(tmpfilename.LastIndexOf("\\") + 1); //求取后缀名 string suffix = filename.Substring(filename.LastIndexOf(".")); //重命名:DateTime Random ro = new Random(); filename = string.Format("{0}{1}{2}", DateTime.Now.ToString("yyyyMMddHHmmssff"), ro.Next(1000, 9999), suffix); //重命名:GUID(全球唯一标识符)推荐!!! //filename = string.Format("{0}{1}", Guid.NewGuid().ToString("N"), suffix); //原文件的保存路径 string fileSavePath = uploadpath + "\\" + filename; //保存原图片 FileUploadImage.SaveAs(fileSavePath); //调用生成缩略图程序,生成缩略图及生成写字的图片 this.toImage(FileUploadImage.PostedFile.InputStream, uploadpath, filename); //显示图片 //分别为原图片/写字的图片(多一个w)/缩略图(多一个x) this.imgshow.ImageUrl = "~/uploadfile/" + filename; this.imgpri.ImageUrl = "~/uploadfile/" + filename.Replace(suffix, "w" + suffix); this.imgSmall.ImageUrl = "~/uploadfile/" + filename.Replace(suffix, "x" + suffix); //显示图像控件 this.imgshow.Visible = true; this.imgpri.Visible = true; this.imgSmall.Visible = true; } else { Response.Write("不支持该图片格式"); } } } /**/ /// <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.Red); //开始绘制,根据上述两种设定,添加绘制的上左位置 g.DrawString("yokin", 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); }
删除图片
using System.IO; File.Delete(path);