ASP.NET(C#)生成缩略图

 1if (FileUpload1.PostedFile.ContentType.ToLower().IndexOf("image"< 0)
 2        {
 3            FunctionUtility.JavaScriptHelper.Alert("上传图片格式不正确!");
 4            return;
 5        }

 6
 7        //生成原图
 8        Byte[] oFileByte = new Byte[FileUpload1.PostedFile.ContentLength];
 9        Stream oStream = FileUpload1.PostedFile.InputStream;
10        System.Drawing.Image oImage = System.Drawing.Image.FromStream(oStream);
11
12        //原图宽度和高度
13        int oWidth = oImage.Width;
14        int oHeight = oImage.Height;
15
16        //设置缩略图的初始宽度和高度
17        int tWidth = 200;
18        int tHeight = 200;
19
20        //按比例计算出缩略图的宽度和高度
21        if (oWidth >= oHeight)
22        {
23            tHeight = (int)Math.Floor(Convert.ToDouble(oHeight) * (Convert.ToDouble(tWidth) / Convert.ToDouble(oWidth)));
24        }

25        else
26        {
27            tWidth = (int)Math.Floor(Convert.ToDouble(oWidth) * (Convert.ToDouble(tHeight) / Convert.ToDouble(oHeight)));
28        }

29
30        //生成缩略图
31        Bitmap tImage = new Bitmap(tWidth, tHeight);
32        Graphics g = Graphics.FromImage(tImage);
33
34        //指定高质量插值法
35        g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
36        //指定高质量低速度呈现
37        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
38        //清空画布并以透明背景色填充
39        g.Clear(Color.Transparent);
40
41        g.DrawImage(oImage, new Rectangle(00, tWidth, tHeight), new Rectangle(00, oWidth, oHeight), GraphicsUnit.Pixel);
42
43        //设置文件名称
44        string setFileName = FunctionUtility.FileHelper.GetDateTimeFileName()+".jpg";
45        //保存原图的物理路径
46        string oFullName = Server.MapPath("/DesignImages/Images/YT/" + setFileName);
47        //保存缩略图物理路径
48        string tFullName = Server.MapPath("/DesignImages/Images/SLT/" + setFileName);
49
50        //以JPG格式保存图片并释放占用的资源
51        try
52        {
53            oImage.Save(oFullName, System.Drawing.Imaging.ImageFormat.Jpeg);
54            tImage.Save(tFullName, System.Drawing.Imaging.ImageFormat.Jpeg);
55
56            Image1.Visible = true;
57            Image2.Visible = true;
58            Image1.ImageUrl = "/DesignImages/Images/YT/" + setFileName;
59            Image2.ImageUrl = "/DesignImages/Images/SLT/" + setFileName;
60
61            FunctionUtility.JavaScriptHelper.Alert("缩略图生成成功!");
62        }

63        catch (Exception oe)
64        {
65            throw oe;
66        }

67        finally
68        {
69            oImage.Dispose();
70            g.Dispose();
71            tImage.Dispose();
72        }

Tag标签: ASP.NET, 缩略图
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值