图片的等比缩放
2016-04-24 23:41
375 查看
HttpPostedFile file = context.Request.Files[0]; using (Stream stream = file.InputStream) { Image img = new Bitmap(stream); //3. 等比缩放图片 按照缩放大的比例来 200 * 100 int thumbW = 200, thumbH = 100; int w = 0, h = 0; if (img.Height * 1.0 / thumbH >= img.Width * 1.0 / thumbW) { h = thumbH; w = (int)(thumbH * 1.0 / img.Height * img.Width); } else { w = thumbW; h = (int)(thumbW * 1.0 / img.Width * img.Height); } using (Bitmap newImg = new Bitmap(w, h)) { using (Graphics graphics = Graphics.FromImage(newImg)) { graphics.DrawImage(img, new Rectangle(0, 0, w, h), new Rectangle(0, 0, img.Width, img.Height), GraphicsUnit.Pixel); //1. 直接裁剪图片 //graphics.DrawImage(img, 0, 0, img.Width, img.Height); //2. 指定 缩放 大小 100*100 //graphics.DrawImage(img, new Rectangle(0, 0, 100, 100), new Rectangle(0, 0, img.Width, img.Height), GraphicsUnit.Pixel); newImg.Save(context.Server.MapPath("/b.png")); context.Response.Write("Width: " + img.Width + " height: " + img.Height); } } }
相关文章推荐
- 77_JNI和NDK
- 76_Java中的引用类型
- iOS开发系列--C语言之构造类型
- USACO TRAINING - PROB Broken Necklace
- 消灭敌人
- 只是想删个文件而已~
- Delphi 10.1说明
- 20145208 实验三 Java面向对象程序设计
- 20145230 《Java程序设计》第8周学习总结
- 数据结构之栈和队列
- Python的三种代码续行书写方法
- iOS 计算日期间隔天数 —— HERO博客
- 236. Lowest Common Ancestor of a Binary Tree
- 分布式架构高可用架构篇_07_MySQL主从复制的配置(CentOS-6.7+MySQL-5.6)
- iOS查看日志利器 —— socat
- 分享到微信好友或朋友圈
- iOS开发 ☞ UIWebView
- 20145129 《Java程序设计》第8周学习总结
- 为什么要敏捷开发?
- 通过“D语言解析wav音频文件”来看C++向D代码的迁移