C# 图片生成缩略图
C# 图片生成缩略图方法:
/// <summary>
/// 生成缩略图
/// </summary>
/// <param name="fileName">原图路径</param>
/// <param name="newFile">缩略图路径</param>
/// <param name="maxHeight">最大高度</param>
/// <param name="maxWidth">最大宽度</param>
public string ThumbnailImage(string fileName, string newFile, int maxHeight, int maxWidth)
{
System.Drawing.Image img = System.Drawing.Image.FromFile(fileName);
System.Drawing.Imaging.ImageFormat thisFormat = img.RawFormat;
//缩略图尺寸
double w = 0.0; //图片的宽
double h = 0.0; //图片的高
double sw = Convert.ToDouble(img.Width);
double sh = Convert.ToDouble(img.Height);
double mw = Convert.ToDouble(maxWidth);
double mh = Convert.ToDouble(maxHeight);
if (sw < mw && sh < mh)
{
w = sw;
h = sh;
}
else if ((sw / sh) > (mw / mh))
{
w = maxWidth;
h = (w * sh) / sw;
}
else
{
h = maxHeight;
w = (h * sw) / sh;
}
Size newSize = new Size(Convert.ToInt32(w), Convert.ToInt32(h));
Bitmap outBmp = new Bitmap(newSize.Width, newSize.Height);
Graphics g = Graphics.FromImage(outBmp);
//设置画布的描绘质量
g.CompositingQuality = CompositingQuality.HighQuality;
g.SmoothingMode = SmoothingMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(img, new Rectangle(, , newSize.Width, newSize.Height), , , img.Width, img.Height, GraphicsUnit.Pixel);
g.Dispose();
//以下代码为保存图片时,设置压缩质量
EncoderParameters encoderParams = new EncoderParameters();
long[] quality = new long[];
quality[] = ;
EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
encoderParams.Param[] = encoderParam;
//获得包含有关内置图像编码解码器的信息的ImageCodecInfo 对象.
ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();
ImageCodecInfo jpegICI = null;
for (int x = ; x < arrayICI.Length;x++)
{
if (arrayICI[x].FormatDescription.Equals("JPEG"))
{
jpegICI = arrayICI[x];
//设置JPEG编码
break;
}
}
if (jpegICI != null)
{
Bitmap newbit = new Bitmap(outBmp);
newbit.Save(newFile, jpegICI, encoderParams);
}
else
{
outBmp.Save(newFile,
thisFormat);
}
img.Dispose();
outBmp.Dispose();
return newFile;
}
C# 图片生成缩略图的更多相关文章
- phpcms v9图片生成缩略图变成黑色解决方法
今天客户反映,上传的图片生成缩略图有的图片变成黑色,出现问题就百度了一下,有不少网友也遇到这样的问题,但是官方论坛也没有给出解决办法,那还得靠自己解决了,于是就研究phpcms v9 图片压缩代码.打 ...
- java 图片生成缩略图后,转化成流
功能:图片生成缩略图后,转化成流 public class ImageUtils { /** * * @param in1 * 文件流 * @param uploadFileName * 文件名称 * ...
- nginx实现本地图片生成缩略图
nginx可以实现图片的缩略图效果,很多网站为了前端静态资源相应的性能会给大图自动生成一个小图,比如我们经常会在网上看到bd_64x64.png这种格式,淘宝上的小图经常会看到xxx.jpg_100x ...
- json序列化.xml序列化.图片转base64.base64转图片.生成缩略图.IEnumerable<TResult> Select<TSource, TResult>做数据转换的五种方式
JSON序列化 /// <summary> /// JSON序列化 /// </summary> public static class SPDBJsonConvert { ...
- 毫秒级的时间处理上G的图片(生成缩略图)
测试环境: 测试图片(30M): 测试计时方法: Stopwatch sw1 = new Stopwatch(); sw1.Start(); //TODO...... sw1.Stop(); stri ...
- JAVA实现根据图片生成缩略图、裁剪、压缩图片
依赖(用来复制文件,可以根据自己的来) <dependency> <groupId>commons-io</groupId> <artifactId>c ...
- dedecms无法下载远程jpeg图片 织梦不能提取文章内容中的jpeg图片生成缩略图
文件:/dede/inc/inc_archives_functions.php 代码: preg_match_all("/(src)=[\"|'| ]{0,}([^>]*\. ...
- C# 生成缩略图 去除图片旋转角度
图片生成缩略图会有旋转角度 /// <summary> /// 测试JRE图片压缩后图片会旋转问题 /// </summary> public void Uploadimg1( ...
- java图片处理(加水印、生成缩略图)等之Thumbnailator库
Thumbnailator 是一个为Java界面更流畅的缩略图生成库.从API提供现有的图像文件和图像对象的缩略图中简化了缩略过程,两三行代码就能够从现有图片生成缩略图,且允许微调缩略图生成,同时保持 ...
随机推荐
- chrome 关闭安全模式
chrome.exe --disable-web-security --user-data-dir
- centOS下升级python版本,详细步骤
1.可利用linux自带下载工具wget下载,如下所示:( 笔者安装的是最小centos系统,所以使用编译命令前,必须安装wget服务,读者如果安装的是界面centos系统,或者使用过编译工具则可跳 ...
- jQuery源码分析--Event模块(2)
接下来就是触发事件了.事件触发后的处理函数的分发主要靠两个函数,一个jQuery.event.dispatch,一个是jQuery.event.handlers.这个dispatch会调用handle ...
- Python3.6(windows系统)通过pip安装bs4
Python3.6(windows系统)通过pip安装bs4 cmd安装命令: pip install beautifulsoup4 执行结果:
- CodeForces 450B Jzzhu and Sequences(矩阵快速幂)题解
思路: 之前那篇完全没想清楚,给删了,下午一上班突然想明白了. 讲一下这道题的大概思路,应该就明白矩阵快速幂是怎么回事了. 我们首先可以推导出 学过矩阵的都应该看得懂,我们把它简写成T*A(n-1)= ...
- ccf 行车路线
问题描述 小明和小芳出去乡村玩,小明负责开车,小芳来导航. 小芳将可能的道路分为大道和小道.大道比较好走,每走1公里小明会增加1的疲劳度.小道不好走,如果连续走小道,小明的疲劳值会快速增加,连续走s公 ...
- [SpringBoot] - 配置文件的多种形式及JSR303数据校验
Springboot配置文件: application.yml application.properties(自带) yml的格式写起来稍微舒服一点 在application.properties ...
- The P4 Language Specification v1.0.2 Introduction部分
Introduction P4 is a declarative language for expressing how packets are processed by the pipeline o ...
- [转]Python的getattr(),setattr(),delattr(),hasattr()
getattr()函数是Python自省的核心函数,具体使用大体如下: 获取对象引用getattrGetattr用于返回一个对象属性,或者方法 class A: def __init__(self): ...
- 代理模式:利用JDK原生动态实现AOP
代理模式:利用JDK原生动态实现AOP http://www.cnblogs.com/qiuyong/p/6412870.html 1.概述 含义:控制对对象的访问. 作用:详细控制某个(某类)某对象 ...