C#5种方式生成缩略图
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.IO; namespace MyTest
{
class imgThumbnail
{
public enum mode
{
W,
H,
HW,
Cut,
MaxHW
}
public imgThumbnail()
{ }
/// <summary>
/// 生成缩略图
/// </summary>
/// <param name="originalImagePath">源图路径(物理路径)</param>
/// <param name="thumbnailPath">缩略图路径(物理路径)</param>
/// <param name="width">缩略图宽度</param>
/// <param name="height">缩略图高度</param>
/// <param name="mode">生成缩略图的方式</param>
public static Image MakeThumbnail(string originalImagePath, int width, int height, mode m)
{
if (!File.Exists(originalImagePath))
{
return null;
}
Image originalImage = Image.FromFile(originalImagePath); int towidth = width;
int toheight = height; int x = 0;
int y = 0;
int ow = originalImage.Width;
int oh = originalImage.Height; switch (m)
{
case mode.MaxHW:
if ((ow / width) > (ow / height))
{
towidth = width;
toheight = (Int32)Math.Ceiling(Convert.ToDecimal((oh * towidth) / ow));
if (toheight > height)
{
towidth = Convert.ToInt32(towidth * (Convert.ToDecimal(height) / toheight));
toheight = height;
}
}
else
{
toheight = height;
towidth = (Convert.ToInt32(Math.Ceiling(Convert.ToDecimal(ow * toheight)) / oh));
if (towidth > width)
{
toheight = Convert.ToInt32(toheight * Convert.ToDecimal(width) / towidth);
towidth = width;
}
}
break;
case mode.HW://指定高宽缩放(可能变形)
break;
case mode.W://指定宽,高按比例
toheight = originalImage.Height * width / originalImage.Width;
break;
case mode.H://指定高,宽按比例
towidth = originalImage.Width * height / originalImage.Height;
break;
case mode.Cut://指定高宽裁减(不变形)
if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
{
oh = originalImage.Height;
ow = originalImage.Height * towidth / toheight;
y = 0;
x = (originalImage.Width - ow) / 2;
}
else
{
ow = originalImage.Width;
oh = originalImage.Width * height / towidth;
x = 0;
y = (originalImage.Height - oh) / 2;
}
break;
default:
break;
} towidth = towidth > ow ? ow : towidth;
toheight = toheight > oh ? oh : toheight;
if (toheight == 0 || towidth == 0)
{
return null;
}
//新建一个bmp图片
Image bitmap = new System.Drawing.Bitmap(towidth, toheight); //新建一个画板
Graphics g = System.Drawing.Graphics.FromImage(bitmap); //设置高质量插值法
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //设置高质量,低速度呈现平滑程度
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; //清空画布并以透明背景色填充
g.Clear(Color.Transparent); //在指定位置并且按指定大小绘制原图片的指定部分
g.DrawImage(originalImage, new Rectangle(0, 0, towidth, toheight),
new Rectangle(x, y, ow, oh),
GraphicsUnit.Pixel); try
{
return bitmap;
//以jpg格式保存缩略图
//bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch (System.Exception e)
{
return null;
throw e;
}
finally
{
originalImage.Dispose();
//bitmap.Dispose();
g.Dispose();
}
}
}
}
C#5种方式生成缩略图的更多相关文章
- 使用timeit模块 测试两种方式生成列表的所用的时间
from timeit import Timer def test(): li=[] for i in range(10000): li.append(i) def test2(): li=[i fo ...
- python基础===用9种方式生成新的对象
class Point: def __init__(self, x, y): self.x = x self.y = y point1 = Point(1, 2) point2 = eval(&quo ...
- web项目生成web.xml的两种方式
做了很多的项目,今天着手写个小demo发现做web项目的时候还需要从别的地方去拷贝,那么如果没有地方可以拷贝,要怎么办呢?下边介绍三种方式生成web.xml文件. 一.maven项目情况:(STS版) ...
- json序列化.xml序列化.图片转base64.base64转图片.生成缩略图.IEnumerable<TResult> Select<TSource, TResult>做数据转换的五种方式
JSON序列化 /// <summary> /// JSON序列化 /// </summary> public static class SPDBJsonConvert { ...
- 两种方式实现java生成Excel
Web应用中难免会遇到需要将数据导出并生成excel文件的需求.同样,对于本博客中的总结,也是建立在为了完成这样的一个需求,才开始去了解其实现形式,并且顺利完成需求的开发,先将实现过程总结于此.本博文 ...
- 生成freeswitch事件的几种方式
本文描述了生成freeswitch事件的几种方式,这里记录下,也方便我以后查阅. 操作系统:debian8.5_x64 freeswitch 版本 : 1.6.8 在freeswitch代码中加入事件 ...
- php 生成word的三种方式
原文地址 http://www.jb51.net/article/97253.htm 最近工作遇到关于生成word的问题 现在总结一下生成word的三种方法. btw:好像只要是标题带PHP的貌似点击 ...
- Android 生成LayoutInflater的三种方式
通俗的说,inflate就相当于将一个xml中定义的布局找出来. 因为在一个Activity里如果直接用findViewById()的话,对应的是setConentView()的那个layout里的组 ...
- 使用NVelocity生成内容的几种方式
使用NVelocity也有几个年头了,主要是在我的代码生成工具Database2Sharp上使用来生成相关代码的,不过NVelocity是一个非常不错的模板引擎,可以用来生成文件.页面等相关处理,非常 ...
随机推荐
- 最新版本Zabbix3.4+Grafana5 整合部署
一.Grafana简介 Grafana是一个可视化面板(Dashboard),有着非常漂亮的图表和布局展示,功能齐全的度量仪表盘和图形编辑器,支持Graphite.elasticsearch.zabb ...
- GIS中栅格数据结构的显示与计算
在GIS中,根据数据存储的表现形式不同,通常将空间数据分为矢量数据和栅格数据.最简单的区别是:当栅格数据放大到一定程度时,就会呈现出我们熟悉的讨厌的"马赛克". 栅格数据存储 最简 ...
- 018-Go将磁盘目录实现简单的静态Web服务
package main import( "net/http" ) func main(){ http.Handle("/", http.FileServer( ...
- SpringCloud Stream生产者配置RabbitMq的动态路由键
在写这个文章前不得不吐槽目前国内一些blog的文章,尽是些复制粘贴的文章,提到点上但没任何的深入和例子.......... 经过测试下来总结一下RabbitMQ的Exchange的特性: 1.dire ...
- UICollectionView在初始化的时候移动到某个距离
#pragma mark -- 使用场景:选中非第一张图片用CollectionView进行浏览时,CollectionView滑动到对应的位置 #pragma mark -- 重点在于UICol ...
- js 引擎 和 html 渲染引擎
- 9.12 翻译系列:数据注解特性之ConcurrencyCheck【EF 6 Code-First系列】
原文链接:https://www.entityframeworktutorial.net/code-first/concurrencycheck-dataannotations-attribute-i ...
- android4.3 截屏功能的尝试与失败分析
1.背景 上一篇讲了在源码中捕获到了android手机的截屏函数(同时按下电源键与音量减,详情http://blog.csdn.net/buptgshengod/article/details/199 ...
- [k8s]通过openssl生成证书
证书认证原理: http://www.cnblogs.com/iiiiher/p/7873737.html [root@m1 ssl]# cat master_ssl.cnf [req] req_ex ...
- [APM] 2个实例+5个维度解读APM技术
为了加深EGO会员之间的相互了解,同时也为大家提供更多线上相互学习交流的机会,EGO正式启动会员群线上分享活动.本文是根据陈靖华分享主题“APM的价值”的内容整理而成. 第二期分享嘉宾:陈靖华,EGO ...