1、 / / 按比例缩小图片,自动计算宽度/ / 源图文件名(包括路径)/ 缩小后保存为文件名( 包括路径)/ 缩小至高度public void SmallPicWidth(string strOldPic, string strNewPic, int intHeight)System.Drawing.Bitmap objPic, objNewPic;tryobjPic = new System.Drawing.Bitmap(strOldPic);int intWidth = (intHeight / objPic.Height) * objPic.Width;objNewPic = new Sy
2、stem.Drawing.Bitmap(objPic, intWidth, intHeight);objNewPic.Save(strNewPic);catch (Exception exp) throw exp; finallyobjPic = null;objNewPic = null;/*/ / 缩小图片/ / 源图文件名(包括路径)/ 缩小后保存为文件名( 包括路径)/ 缩小至宽度/ 缩小至高度public void SmallPic(string strOldPic, string strNewPic, int intWidth, int intHeight)System.Drawi
3、ng.Bitmap objPic, objNewPic;tryobjPic = new System.Drawing.Bitmap(strOldPic);objNewPic = new System.Drawing.Bitmap(objPic, intWidth, intHeight);objNewPic.Save(strNewPic);catch (Exception exp) throw exp; finallyobjPic = null;objNewPic = null; 图片上传(文件按日期命名+真正意义上类型判断+缩略图)作者:小角色 来源:博客园 发布时间:2008-04-22 1
4、0:15 阅读:349 次 原文链接 收藏 ImageThumbnail.cs(生成缩略图文件)using System;using System.IO;using System.Drawing;using System.Drawing.Imaging;public class ImageThumbnailpublic Image ResourceImage;private int ImageWidth;private int ImageHeight;public string ErrorMessage;public ImageThumbnail(string ImageFileName)Re
5、sourceImage = Image.FromFile(ImageFileName);ErrorMessage = “;public bool ThumbnailCallback()return false;/ 方法 1,按大小public bool ReducedImage(int Width, int Height, string targetFilePath)tryImage ReducedImage;Image.GetThumbnailImageAbort callb = new Image.GetThumbnailImageAbort(ThumbnailCallback);Redu
6、cedImage = ResourceImage.GetThumbnailImage(Width, Height, callb, IntPtr.Zero);ReducedImage.Save(targetFilePath, ImageFormat.Jpeg);ReducedImage.Dispose();return true;catch (Exception e)ErrorMessage = e.Message;return false;/ 方法 2,按百分比 缩小 60% Percent 为 0.6 targetFilePath 为目标路径public bool ReducedImage(
7、double Percent, string targetFilePath)tryImage ReducedImage;Image.GetThumbnailImageAbort callb = new Image.GetThumbnailImageAbort(ThumbnailCallback);ImageWidth = Convert.ToInt32(ResourceImage.Width * Percent);ImageHeight = (ResourceImage.Height) * ImageWidth / ResourceImage.Width;/等比例缩放ReducedImage
8、= ResourceImage.GetThumbnailImage(ImageWidth, ImageHeight, callb, IntPtr.Zero);ReducedImage.Save(targetFilePath, ImageFormat.Jpeg);ReducedImage.Dispose();return true;catch (Exception e)ErrorMessage = e.Message;return false;前台文件:就一个 FileUpload1 和一个 Button,就不用贴代码了吧!using System;using System.Data;using
9、 System.Configuration;using System.Collections;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;public partial class _Default : System.Web.UI.Pageprotected void Page_Load(object se
10、nder, EventArgs e)protected void btn_Upload_Click(object sender, EventArgs e)try/判断是否已经选取文件if (FileUpload1.HasFile)if (IsAllowedExtension(FileUpload1)/取得文件的扩展名,并转换成小写string fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();string path1 = Server.MapPath(“/Bigimages/“);string
11、 path2 = Server.MapPath(“/Smallimages/“);string fileName = DateRndName() + fileExtension;FileUpload1.PostedFile.SaveAs(path1 + fileName);/生成缩略图并存进文件夹ImageThumbnail img = new ImageThumbnail(FileUpload1.PostedFile.FileName);img.ReducedImage(200,200,path2 + fileName);/200,200 表是长和宽都为 200/img.ReducedIma
12、ge(0.4, path2 + fileName);/0.4 表示缩小 40%Response.Write(“alert(“上传成功“);“);elseResponse.Write(“alert(“您只能上传 jpg 或者 gif 图片“);“);elseResponse.Write(“alert(“你还没有选择文件“);“);catch (Exception error)Response.Write(error.ToString();/真正判断文件类型的关键函数public static bool IsAllowedExtension(FileUpload hifile)System.IO.
13、FileStream fs = new System.IO.FileStream(hifile.PostedFile.FileName, System.IO.FileMode.Open, System.IO.FileAccess.Read);System.IO.BinaryReader r = new System.IO.BinaryReader(fs);string fileclass = “;byte buffer;trybuffer = r.ReadByte();fileclass = buffer.ToString();buffer = r.ReadByte();fileclass +
14、= buffer.ToString();catchr.Close();fs.Close();if (fileclass = “255216“ | fileclass = “7173“)/说明 255216 是 jpg;7173是 gif;6677 是 BMP,13780 是 PNG;7790 是 exe,8297 是 rarreturn true;elsereturn false;#region DateRndName()日期时间+3 位数随机string DateRndName()Random ra = new Random();DateTime d = DateTime.Now;strin
15、g s = null, y, m, dd, h, mm, ss;y = d.Year.ToString();m = d.Month.ToString();if (m.Length 2) m = “0“ + m;dd = d.Day.ToString();if (dd.Length 2) dd = “0“ + dd;h = d.Hour.ToString();if (h.Length 2) h = “0“ + h;mm = d.Minute.ToString();if (mm.Length 2) mm = “0“ + mm;ss = d.Second.ToString();if (ss.Length 2) ss = “0“ + ss;s += y + m + dd + h + mm + ss;s += ra.Next(100, 999).ToString();return s;#endregion