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种方式生成缩略图的更多相关文章

  1. 使用timeit模块 测试两种方式生成列表的所用的时间

    from timeit import Timer def test(): li=[] for i in range(10000): li.append(i) def test2(): li=[i fo ...

  2. python基础===用9种方式生成新的对象

    class Point: def __init__(self, x, y): self.x = x self.y = y point1 = Point(1, 2) point2 = eval(&quo ...

  3. web项目生成web.xml的两种方式

    做了很多的项目,今天着手写个小demo发现做web项目的时候还需要从别的地方去拷贝,那么如果没有地方可以拷贝,要怎么办呢?下边介绍三种方式生成web.xml文件. 一.maven项目情况:(STS版) ...

  4. json序列化.xml序列化.图片转base64.base64转图片.生成缩略图.IEnumerable<TResult> Select<TSource, TResult>做数据转换的五种方式

     JSON序列化 /// <summary> /// JSON序列化 /// </summary> public static class SPDBJsonConvert { ...

  5. 两种方式实现java生成Excel

    Web应用中难免会遇到需要将数据导出并生成excel文件的需求.同样,对于本博客中的总结,也是建立在为了完成这样的一个需求,才开始去了解其实现形式,并且顺利完成需求的开发,先将实现过程总结于此.本博文 ...

  6. 生成freeswitch事件的几种方式

    本文描述了生成freeswitch事件的几种方式,这里记录下,也方便我以后查阅. 操作系统:debian8.5_x64 freeswitch 版本 : 1.6.8 在freeswitch代码中加入事件 ...

  7. php 生成word的三种方式

    原文地址 http://www.jb51.net/article/97253.htm 最近工作遇到关于生成word的问题 现在总结一下生成word的三种方法. btw:好像只要是标题带PHP的貌似点击 ...

  8. Android 生成LayoutInflater的三种方式

    通俗的说,inflate就相当于将一个xml中定义的布局找出来. 因为在一个Activity里如果直接用findViewById()的话,对应的是setConentView()的那个layout里的组 ...

  9. 使用NVelocity生成内容的几种方式

    使用NVelocity也有几个年头了,主要是在我的代码生成工具Database2Sharp上使用来生成相关代码的,不过NVelocity是一个非常不错的模板引擎,可以用来生成文件.页面等相关处理,非常 ...

随机推荐

  1. 【Java】MyBatis与Spring框架整合(二)

    本文讲解 Spring 注入映射器及事务功能的实现. 注入映射器实现 MyBatis 可以使用 SqlSession 的 getMapper ( Class<T> type ) 方法,根据 ...

  2. 对actuator的管理端点进行ip白名单限制(springBoot添加filter)

    在我们的SpringCloud应用中,我们会引入actuator来进行管理和监控我们的应用 常见的有:http://www.cnblogs.com/yangzhilong/p/8378152.html ...

  3. Tuxedo低版本客户端(Tuxedo 9)连接到高版本Tuxedo服务端(Tuxedo 12.1.3)的问题

    经过我实测,是没问题的.但是客户端的Tuxedo DLL必须全部是Tuxedo 9的DLL,不能混用.不然即使用Dependency Walker 分析DLL依赖完全正确,但是实际运行时结果也会出现奇 ...

  4. .NET 同步与异步 之 原子操作和自旋锁(Interlocked、SpinLock)(九)

    本随笔续接:.NET 同步与异步之锁(ReaderWriterLockSlim)(八) 之前的随笔已经说过.加锁虽然能很好的解决竞争条件,但也带来了负面影响:性能方面的负面影响.那有没有更好的解决方案 ...

  5. WineBottler for Mac(Mac 运行 exe 程序工具)安装

    1.软件简介    WineBottler 是 macOS 系统上一款模拟 Windows 环境的工具,让你能够在 Mac 上安装 Windows 软件,类似于知名的 Crossover,但 Wine ...

  6. 8.翻译系列: EF 6中配置领域类(EF 6 Code-First 系列)

    原文地址:http://www.entityframeworktutorial.net/code-first/configure-classes-in-code-first.aspx EF 6 Cod ...

  7. Java list对象列表排序 实例

    package com.test; public class Bean { private String name; private int priority; public String getNa ...

  8. C#-MVC开发微信应用(3)--文本消息和图文消息的应答

    最近咨询微信的人很多,感觉这块也是一块商机,也为了演示SNF快速开发平台的优势,就用SNF快速开发平台开发出一套微信应用程序.使用<SNF.CodeGenerator>代码生成工具可以节省 ...

  9. 脚本加密http://www.datsi.fi.upm.es/~frosal/sources/

    shc的官网下载地址: http://www.datsi.fi.upm.es/~frosal/sources/ 安装: 复制代码 代码如下: tar xzvf shc-.tgz cd shc- mkd ...

  10. Gradle依赖的统一管理,解决依赖冲突

    看见别人在用implementation rootProject.ext.dependencies["xxxx"]不知道是什么意思,上网查了一下,原来是为了解决或者说预防gradl ...