zoukankan
html css js c++ java
asp.net上传图片并同时生成缩略图
using System; using System.Data; using 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; using System.Drawing; public partial class slt_Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void bt_upload_Click(object sender, EventArgs e) { //检查上传文件的格式是否有效 if (this.UploadFile.PostedFile.ContentType.ToLower().IndexOf("image") < 0) { Response.Write("上传图片格式无效!"); return; } //生成原图 Byte[] oFileByte = new byte[this.UploadFile.PostedFile.ContentLength]; System.IO.Stream oStream = this.UploadFile.PostedFile.InputStream; System.Drawing.Image oImage = System.Drawing.Image.FromStream(oStream); int oWidth = oImage.Width; //原图宽度 int oHeight = oImage.Height; //原图高度 int tWidth = 100; //设置缩略图初始宽度 int tHeight = 100; //设置缩略图初始高度 //按比例计算出缩略图的宽度和高度 if (oWidth >= oHeight) { tHeight = (int)Math.Floor(Convert.ToDouble(oHeight) * (Convert.ToDouble(tWidth) / Convert.ToDouble(oWidth))); } else { tWidth = (int)Math.Floor(Convert.ToDouble(oWidth) * (Convert.ToDouble(tHeight) / Convert.ToDouble(oHeight))); } //生成缩略原图 Bitmap tImage = new Bitmap(tWidth, tHeight); Graphics g = Graphics.FromImage(tImage); g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //设置高质量插值法 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;//设置高质量,低速度呈现平滑程度 g.Clear(Color.Transparent); //清空画布并以透明背景色填充 g.DrawImage(oImage, new Rectangle(0, 0, tWidth, tHeight), new Rectangle(0, 0, oWidth, oHeight), GraphicsUnit.Pixel); string oFullName = Server.MapPath(".") + "/image/" + "o" + DateTime.Now.ToShortDateString().Replace("-", "") + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + ".jpg"; //保存原图的物理路径 string tFullName = Server.MapPath(".") + "/image/" + "t" + DateTime.Now.ToShortDateString().Replace("-", "") + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + ".jpg"; //保存缩略图的物理路径 try { //以JPG格式保存图片 oImage.Save(oFullName, System.Drawing.Imaging.ImageFormat.Jpeg); tImage.Save(tFullName, System.Drawing.Imaging.ImageFormat.Jpeg); } catch (Exception ex) { throw ex; } finally { //释放资源 oImage.Dispose(); g.Dispose(); tImage.Dispose(); } } }
查看全文
相关阅读:
浏览器之本地缓存存储 localStorage 和 sessionStorage的区别以及用法
webpack 命令 Module build failed (from ./node_modules/babel-loader/lib/index.js) 错误问题解决方案
webpack 4 x使用详细
JS动态判断设备类型为PC或者移动端,然后根据设备加载相应的代码
自制弹出框所踩的坑
ant深入浅出(一)ant+xdoclet 生成hibernate配置文件以及实体映射文件
收费系统
自学考试 (二)数据结构图
ORM框架Hibernate (四)MyEclipse Hibernate Tool 逆向生成实体类
自学考试 (一)如何利用MindManager工具复习
原文地址:https://www.cnblogs.com/javawebsoa/p/2458099.html
最新文章
SQL SERVER技术内幕之3 联接查询
打开打印机属性窗口
SFTPHelper
NPOI
CSS设计指南之CSS三种机制:继承、层叠和特指
Java实现RSA加密
Java驼峰和下划线互相转化
Python3基础笔记_迭代器
分布式事务实现机制_两段提交
Datagrip破解激活
热门文章
Python3基础笔记_字典
Python3基础笔记_元组
Python3基础笔记_列表
Python基础笔记_Number类型
Python3基础笔记_字符串类型
sdfsdsf
JS控制输入框和文本框字数
列表栏按规律动态插入广告或者其他数据
for 循环遍历数据,根据不同的条件判断动态渲染页面!
webpack 4 脚手架搭建
Copyright © 2011-2022 走看看