#region 根据原图片生成等比缩略图
/// <summary>
/// 根据源图片生成缩略图
/// </summary>
/// <param name="imgPath_old">源图(大图)物理路径</param>
/// <param name="imgPath_new">缩略图物理路径(生成的缩略图将保存到该物理位置)</param>
/// <param name="width">缩略图宽度</param>
/// <param name="height">缩略图高度</param>
/// <param name="mode">缩略图缩放模式(取值"HW":指定高宽缩放,可能变形;取值"W":按指定宽度,高度按比例缩放;取值"H":按指定高度,宽度按比例缩放;取值"Cut":按指定高度和宽度裁剪,不变形);取值"DB":等比缩放,以值较大的作为标准进行等比缩放</param>
/// <param name="type">即将生成缩略图的文件的扩展名(仅限:JPG、GIF、PNG、BMP)</param>
public static void MakeThumbnail(string imgPath_old, string imgPath_new, int width, int height, string mode, string imageType, int xx, int yy)
{
System.Drawing.Image img = System.Drawing.Image.FromFile(imgPath_old);
int towidth = width; int toheight = height;
int x = ; int y = ; int ow = img.Width;
int oh = img.Height; switch (mode)
{
case "HW": //指定高宽压缩
if ((double)img.Width / (double)img.Height > (double)width / (double)height)//判断图形是什么形状
{
towidth = width;
toheight = img.Height * width / img.Width;
}
else if ((double)img.Width / (double)img.Height == (double)width / (double)height)
{
towidth = width;
toheight = height;
}
else
{
toheight = height;
towidth = img.Width * height / img.Height;
}
break;
case "W": //指定宽,高按比例
toheight = img.Height * width / img.Width;
break;
case "H": //指定高,宽按比例
towidth = img.Width * height / img.Height;
break;
case "Cut": //指定高宽裁减(不变形)
if ((double)img.Width / (double)img.Height > (double)towidth / (double)toheight)
{
oh = img.Height;
ow = img.Height * towidth / toheight;
y = yy; x = (img.Width - ow) / ;
}
else
{
ow = img.Width;
oh = img.Width * height / towidth;
x = xx; y = (img.Height - oh) / ;
} break;
case "DB": // 按值较大的进行等比缩放(不变形)
if ((double)img.Width / (double)towidth < (double)img.Height / (double)toheight)
{
toheight = height;
towidth = img.Width * height / img.Height;
}
else
{
towidth = width;
toheight = img.Height * width / img.Width;
} break;
default:
break;
}
//新建一个bmp图片
System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);
//新建一个画板
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
//设置高质量插值法
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
//设置高质量,低速度呈现平滑程度
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//清空画布并以透明背景色填充
g.Clear(System.Drawing.Color.Transparent);
//在指定位置并且按指定大小绘制原图片的指定部分
g.DrawImage(img, new System.Drawing.Rectangle(, , towidth, toheight),
new System.Drawing.Rectangle(x, y, ow, oh),
System.Drawing.GraphicsUnit.Pixel); try
{
//以jpg格式保存缩略图
switch (imageType.ToLower())
{
case "gif":
img.Save(imgPath_new, ImageFormat.Jpeg);//生成缩略图
break;
case "jpg":
bitmap.Save(imgPath_new, System.Drawing.Imaging.ImageFormat.Jpeg);
break;
case "bmp":
bitmap.Save(imgPath_new, System.Drawing.Imaging.ImageFormat.Bmp);
break;
case "png":
bitmap.Save(imgPath_new, System.Drawing.Imaging.ImageFormat.Png);
break;
default:
bitmap.Save(imgPath_new, System.Drawing.Imaging.ImageFormat.Jpeg);
break;
}
////保存缩略图
// bitmap.Save(imgPath_new);
}
catch (System.Exception e)
{
throw e;
}
finally
{
img.Dispose();
bitmap.Dispose(); g.Dispose();
}
}
#endregion

.NET 等宽、等高、等比例、固定宽高生成缩略图 类的更多相关文章

  1. 偏前端 - 不是固定宽高,页面随设备同比 放大/缩小 展示。不妨看看rem单位,你就有眉目上手做了!!!

    为什么要使用rem 之前有些适配做法,是通过js动态计算viewport的缩放值(initial-scale). 例如以屏幕320像素为基准,设置1,那屏幕375像素就是375/320=1.18以此类 ...

  2. JS-自制提速小工具:开发页面时需要按比例计算宽高值的快速计算器

    <!DOCTYPE html><html> <head> <meta charset="UTF-8"> <meta name= ...

  3. div 固定宽高 水平垂直居中方法

    div固定宽高,水平垂直居中,根据所用单位不同,分成两种情况,分别是"px"和"%"情况. 例:将三层div做出三个边框,要求水平垂直居中.效果如图 情况一(单 ...

  4. 固定宽高的DIV绝对居中示例

    看了一些代码,然后自己试验了一番,分享如下示例: 实现点: 如果元素的宽高固定,那么,css指定样式为top:50%;left:50%; 而margin-top和 margin-left 指定为负数, ...

  5. Selenium基础之--01(将浏览器最大化,设置浏览器固定宽、高,操控浏览器前进、后退)

    1,将浏览器最大化 我们知道调用启动的浏览器不是全屏的,这样不会影响脚本的执行,但是有时候会影响我们"观看"脚本的执行. coding=utf-8 from selenium im ...

  6. 如何创建width与height比例固定的元素

    面试题,刚在github上看到的,说说这里面的知识点吧~~ padding-bottom的值,其百分比是根据元素自身的width来算的. padding,在标准盒模型中,width+padding+b ...

  7. CSS百分比padding实现比例固定图片自适应布局

    一.CSS百分比padding都是相对宽度计算的 在默认的水平文档流方向下,CSS margin和padding属性的垂直方向的百分比值都是相对于宽度计算的,这个和top, bottom等属性的百分比 ...

  8. 我的前端组件 ---- 16:9固定宽高比例的div

    目标:遇到一个需求,让图片在页面中,不管宽度如何变化.宽高保持16:9的比例. 实现: 方法一:这也是比较经典的一个方法,利用padding-bottom来实现. <!DOCTYPE html& ...

  9. CSS左侧固定宽 右侧自适应(兼容所有浏览器)

    左侧固定宽,右侧自适应屏幕宽: 左右两列,等高布局: 左右两列要求有最小高度,例如:200px;(当内容超出200时,会自动以等高的方式增高) 要求不用JS或CSS行为实现: 仔细分析试题要求,要达到 ...

随机推荐

  1. MAC OS环境下搭建基于Python语言的Selenium2自动化测试环境

    #1安装Python Mac OS上自带python2.7,在此介绍安装python3.x版本 去官网下载Python for MAC版本 https://www.python.org 安装文件为pk ...

  2. Redis 字符串与哈希

    /*** * 字符串 redis里的字符串 ***/ //设置key的值 redis 127.0.0.1:6379> set key 'my name is imay' //设置值的过期时间 ( ...

  3. Appium + Python App自动化(2)第一个脚本

    [1]打开你的夜神模拟器(或者连接你的手机) [2]打开桌面的Appium [3]下载你要测的App的apk文件,放到桌面 [4]拖动你的apk安装包到夜神模拟器里,然后模拟器会提示你安装.安装.原来 ...

  4. Tree的两种存储形式

    1.xml存储 2.链式结构存储 List<Node> nodes = new List<Node>() { , Name = "中国" }, , Name ...

  5. 【java多线程】用户线程和守护线程的区别

    java中线程分为两种类型:用户线程和守护线程.通过Thread.setDaemon(false)设置为用户线程:通过Thread.setDaemon(true)设置为守护线程.如果不设置次属性,默认 ...

  6. linux各个文件作用

    linux下的文件结构,看看每个文件夹都是干吗用的/bin 二进制可执行命令 /dev 设备特殊文件 /etc 系统管理和配置文件 /etc/rc.d 启动的配置文件和脚本 /home 用户主目录的基 ...

  7. 多文件的Makefile

    Linux下编写一般采用gcc编译工具,但gcc无法满足大量的文件同时编译,这是就用到Makefile,首先先介绍一下gcc GCC编译的四个步骤 1.预处理,生成预编译文件(.文件): Gcc –E ...

  8. oracle之 12.1.0.1.0 C 在 linux 7 上安装报错处理

    环境说明:-- os[root@host-172-16-3-132 ~]# cat /etc/redhat-release CentOS Linux release 7.2.1511 (Core) - ...

  9. Web 漏洞分析与防御之 CSRF(二)

    原文地址:Web 漏洞分析与防御之 CSRF(二) 博客地址:http://www.extlight.com 一.全称 跨站请求伪造(Cross-site Request Forgery) 二.原理 ...

  10. RK3288 查看时钟树

    主控端可以通过指令查看时钟树,enable_cnt为1,表示时钟已使能. # cat d/clk/clk_summary cat d/clk/clk_summary clock enable_cnt ...