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. postgresql 窗口函数排序实例

    经常遇到一种应用场景,将部分行的内容进行汇总.比较.排序. 比如数据表名称test.test2 select num,province from test.test2 得到结果: ;"黑龙江 ...

  2. 开发指南专题五:JEECG微云高速开发平台代码生成器

    开发指南专题五:JEECG微云高速开发平台代码生成器 1.1. Maven开发环境搭建 在搭建jeecg的maven开发环境之前,须要先配置好本机的maven环境,并在eclipse中安装好m2ecl ...

  3. 开发第一个flutter程序 hello world

    上一篇咱们配置了flutter的基本环境之后,那么本篇就来讲一下如何开发第一个小应用hello world 双击打开 android studio 选择plugins 找到 fultter 并且下载安 ...

  4. Xbox One手柄 + Xbox Wireless Adapter PC无线适配器驱动安装、配对全流程

    以下步骤在Windows 7系统中操作.XBox One手柄+无线适配器并非仅只能在Windows 10中使用. 一点感想:微软的XBoxOne手柄实在是好东西,但产品使用说明与文档实在太垃圾,翻遍官 ...

  5. Android——Android和SVN::::SVN+delete项目

    SVN使用笔记(比较详细) http://www.cnblogs.com/merray/p/4182380.html 删除项目 http://jingyan.baidu.com/article/c74 ...

  6. chrome调试手机webview中页面

    http://blog.csdn.net/freshlover/article/details/42528643 注: 1. 可以调试真机上页面(USB连接)和虚拟机上页面 2. 手机系统需要4.4+ ...

  7. 【css】css 中文字体 unicode 对照表

    css 中文字体可以用 unicode 格式来表示,比如“宋体”可以用 \5B8B\4F53 来表示.具体参考下表: 中文名 英文名 unicode 宋体 SimSun \5B8B\4F53 黑体 S ...

  8. opencv 无法使用 dll 动态链接库 UnsatisfiedLinkError java.library.path Can't find dependent libraries

    System.loadLibrary(Core.NATIVE_LIBRARY_NAME) 使用如上方法加载本地 dll文件. 一般会出现两种错误: 1. UnsatisfiedLinkError ja ...

  9. 第四百节,Django+Xadmin打造上线标准的在线教育平台—生产环境部署CentOS6.5安装python3.5.1

    第四百节,Django+Xadmin打造上线标准的在线教育平台—生产环境部署CentOS6.5安装python3.5.1 1.检查系统是否安装了python [root@192 ~]# rpm -qa ...

  10. WAS生成的文件:javacore.***.txt 、heapdump.***.phd、core.***.dmp、Snap.***.trc

    WAS生成的常见文件有哪些? 原文链接:http://blog.csdn.net/pqh20085101092/article/details/39370389 javacore.***.txt : ...