/// <summary>
/// 创建缩略图
/// </summary>
/// <param name="srcFileName"></param>
/// <param name="destFileName"></param>
private static void CreateThumbnailImage(string srcFileName, string destFileName)
{
using (var img = new Bitmap(srcFileName))
{
Size originalSize = new Size(img.Width, img.Height);
Size targetSize = new Size(, );
targetSize = CalculateNewImageSize(originalSize, targetSize);
using (
var bitmap = new Bitmap(targetSize.Width, targetSize.Height, PixelFormat.Format32bppArgb))
{
using (Graphics g = Graphics.FromImage(bitmap))
{
g.InterpolationMode = InterpolationMode.Low;
using (var wrapmode = new ImageAttributes())
{
wrapmode.SetWrapMode(WrapMode.TileFlipXY);
g.DrawImage(img, new Rectangle(, , targetSize.Width, targetSize.Height),
, ,
img.Width,
img.Height,
GraphicsUnit.Pixel,
wrapmode);
}
}
bitmap.Save(destFileName);
}
}
} /// <summary>
/// 计算缩略图的尺寸
/// </summary>
/// <param name="originalSize"></param>
/// <param name="targetSize"></param>
/// <returns></returns>
private static Size CalculateNewImageSize(Size originalSize, Size targetSize)
{
if (originalSize.Width <= targetSize.Width&&
originalSize.Height<=targetSize.Height)
{
//目标尺寸比原尺寸大,那么直接使用原尺寸
return originalSize;
} double targetRatio = GetRatio(targetSize);
double originalRatio = GetRatio(originalSize); var size = new Size(targetSize.Width, targetSize.Height); if (originalRatio < targetRatio)
{
size.Width = (originalSize.Width*targetSize.Height)/originalSize.Height;
}
else
{
size.Height = (originalSize.Height*targetSize.Width)/originalSize.Width;
} return size;
} private static double GetRatio(Size size)
{
return (double) size.Width/size.Height;
}

支持大文件的缩略图,800M的bmp图片缩放到300*300大概100kb左右

c# 创建缩略图的更多相关文章

  1. PHP 创建缩略图

    一.成比例缩小图像 <?php /* 创建缩略图 */ $file = __DIR__.'/button.png'; $scale = 0.5; // 比例 $image = ImageCrea ...

  2. php 使用GD库上传图片以及创建缩略图

    php 使用GD库上传图片以及创建缩略图   GD库是PHP进行图象操作一个很强大的库. 先在php.ini里增加一行引用:extension=php_gd2.dll 重启apache.做一个测试页 ...

  3. 使用GDI+轻松创建缩略图

    Gdi+ 还是相当好用的. 1> Image保存图像,需要一个CLSID的参数,它可以这样获得: int   GetEncoderClsid(const   WCHAR*   format,   ...

  4. Win+PHP+IECapt完整实现网页批量截图并创建缩略图

    最近在开发一个本地互联网应用的项目,为了增加用户体验,需要在搜索结果左侧显示如图一所示的某个网站的缩略图效果,在网上不停地百度谷歌了一上午后,发现大多数实现少量截图还是可以的,如果大批量的截图总会在中 ...

  5. JavaScript with Image:创建缩略图

    当图片很大,直接把图片从Server下载到浏览器上看是一种很不明智的做法,浪费了服务器的资源,网络带宽和客户端的资源.所以,通常Server和Client之间会传输缩略图,只有当Client请求某张图 ...

  6. python创建缩略图和选择轮廓效果

    # -*- encoding:utf-8 -*- ''' 改变颜色 --- 颜色反转''' from PIL import Image nest = Image.open("D:\\tk.j ...

  7. 使用 Amazon S3 触发器创建缩略图

    使用 Amazon S3 触发器创建缩略图 环境 centos (注意,必须是Linux环境) node12.x 安装教程 curl -sL https://rpm.nodesource.com/se ...

  8. scrapy下载图片到自己的目录,创建缩略图,存储入库

    环境和工具:python2.7,scrapy 实验网站:http://www.27270.com/tag/333.html  爬去所有兔女郎图片,下面的推荐需要过滤 逻辑:分析网站信息,下载图片和入库 ...

  9. JS创建缩略图

    <script language="javascript"> //显示缩略图 function DrawImage(ImgD,width_s,height_s){ /* ...

随机推荐

  1. 【leetcode刷题笔记】Permutations II

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  2. 【leetcode刷提笔记】Permutations

    Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...

  3. 【Flask】模板继承

    # 模版继承笔记: ### 为什么需要模版继承:模版继承可以把一些公用的代码单独抽取出来放到一个父模板中.以后子模板直接继承就可以使用了.这样可以重复性的代码,并且以后修改起来也比较方便. ### 模 ...

  4. 《机器学习实战》学习笔记第十二章 —— FP-growth算法

    主要内容: 一.  FP-growth算法简介 二.构建FP树 三.从一颗FP树中挖掘频繁项集 一.  FP-growth算法简介 1.上次提到可以用Apriori算法来提取频繁项集,但是Aprior ...

  5. castle windsor学习-----Registering components by conventions

    注册多个组件 1.one-by-one注册组件可能是一项非常重复的工作,可以通过Classes或Types注册一组组件(你可以指定一些特定的特征) 三个步骤 注册多个类型通常采取以下结构 contai ...

  6. cookie对比localStorage哪个适合作为网站皮肤存储

    cookie对比localStorage哪个适合作为网站皮肤存储 cookie cookie : 一般由服务器生成,可设置失效时间.如果在浏览器生成,默认是关闭浏览器之后失效 存储大小:4k 每次都会 ...

  7. mvc购物车项目(2)

    为了避免数据冗余,我们可以把共同的信息,抽出建立一个单独的表,把不是共有的信息,建立一张单独表. 订单表分为两个表 create table orders( id number primary key ...

  8. Oracle使用hs odbc连接mssql2008

    1.创建odbc 2.在 product\11.2.0\dbhome_1\hs\admin\  下拷贝initdg4odbc,把名字改为initcrmsql(init+所建odbc的名称) HS_FD ...

  9. ckeditor出现错误“从客户端(***)中检测到有潜在危险的 Request.Form值”的解决方法

    ckeditor出现错误“从客户端(***)中检测到有潜在危险的 Request.Form值”的解决方法 页面中使用ckeditor,提交文章时总是出错,“从客户端(TextBox1="&l ...

  10. Idea_学习_05_Intellij Idea自动添加注释的方法

    二.参考资料 1. Intellij Idea自动添加注释的方法