C#/JS 压缩到指定大小的图片 (内存不足问题修改)
//因为浏览器安全问题,无法获取上传图片地址,需要先存一遍然后再获取地址作参数上传
var des = CompressImage(@"C:\Users\PC\Pictures\测试\1652344981(1).jpg", @"C:\Users\PC\Pictures\测试\ok.jpg");
Console.WriteLine(des);
Console.ReadLine();
///
/// 无损压缩图片
///
/// 原图片地址
/// 压缩后保存图片地址
/// 压缩质量(数字越小压缩率越高)1-100
/// 压缩后图片的最大大小
/// 是否是第一次调用
///
bool CompressImage(string sFile, string dFile, int flag = 90, int size = 200, bool sfsc = true)
{
//如果是第一次调用,原始图像的大小小于要压缩的大小,则直接复制文件,并且返回true
FileInfo firstFileInfo = new FileInfo(sFile);
if (sfsc == true && firstFileInfo.Length < size * 1024)
{
firstFileInfo.CopyTo(dFile); return true;
}
Image iSource = Image.FromFile(sFile);
ImageFormat tFormat = iSource.RawFormat;
int dHeight = iSource.Height / 2;
int dWidth = iSource.Width / 2;
int sW = 0, sH = 0;
//按比例缩放
Size tem_size = new Size(iSource.Width, iSource.Height);
if (tem_size.Width > dHeight || tem_size.Width > dWidth)
{
if ((tem_size.Width * dHeight) > (tem_size.Width * dWidth))
{
sW = dWidth; sH = (dWidth * tem_size.Height) / tem_size.Width;
}
else
{
sH = dHeight;
sW = (tem_size.Width * dHeight) / tem_size.Height;
}
}
else
{
sW = tem_size.Width;
sH = tem_size.Height;
}
Bitmap ob = new Bitmap(dWidth, dHeight);
using (Graphics g = Graphics.FromImage(ob)) //原代码再服务器上经常爆内存不足
{
g.Clear(Color.WhiteSmoke);
g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.DrawImage(iSource, new Rectangle((dWidth - sW) / 2, (dHeight - sH) / 2, sW, sH), 0, 0, iSource.Width, iSource.Height, GraphicsUnit.Pixel);
g.Dispose();
}
//以下代码为保存图片时,设置压缩质量
EncoderParameters ep = new EncoderParameters();
long[] qy = new long[1];
qy[0] = flag; //设置压缩的比例1-100
EncoderParameter eParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qy);
ep.Param[0] = eParam;
try
{
ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();
ImageCodecInfo jpegICIinfo = null;
for (int x = 0; x < arrayICI.Length; x++)
{
if (arrayICI[x].FormatDescription.Equals("JPEG"))
{
jpegICIinfo = arrayICI[x]; break;
}
}
if (jpegICIinfo != null)
{
ob.Save(dFile, jpegICIinfo, ep);//dFile是压缩后的新路径
FileInfo fi = new FileInfo(dFile);
if (fi.Length > 1024 * size)
{
flag = flag - 10;
CompressImage(sFile, dFile, flag, size, false);
}
}
else
{
ob.Save(dFile, tFormat);
}
return true;
}
catch { return false; }
finally { iSource.Dispose(); ob.Dispose(); }
}
//JS版本
转至:https://blog.csdn.net/wrongyao/article/details/104807924
最新文章
- linq 小记
- HTML DOM 事件
- SQL Prompt激活
- 读书笔记:7个示例科普CPU Cache
- PLSQL Developer 常用设置及快捷键
- Spark Tungsten揭秘 Day3 内存分配和管理内幕
- form表单普通提交预览显示,读取显示tmp文件
- 关于this的使用
- JavaScript 客户端JavaScript之cookie和客户端持久性
- 【界面优化】使用viewpagerindicator添加下划线滑动动画
- H5的学习之旅-H5的实体(14)
- python各种类型日期转换大全
- 自动化测试基础篇--Selenium元素定位
- Artifact project04:war :Error during artifact deployment. See server log for details
- java 线程Thread 技术--volatile关键字
- docker 镜像存放路径的修改
- m3u8编码视频webgl、threejs渲染视频纹理demo
- 【13】享元模式(FlyWeight Pattern)
- 【转】Mybatis学习---MyBatis知识、原始Dao开发和mapper代理开发
- DNS ------ windows执行DNS过程
热门文章
- hexo-通过-metaweblog-api-同步各大博客网站
- MyBatis03:连接池及事务控制、xml动态SQL语句、多表操作
- 【Hive】概念、安装、数据类型、DDL、DML操作、查询操作、函数、压缩存储、分区分桶、实战Top-N、调优(fetch抓取)、执行计划
- [数学建模]主成分分析法PCA
- .Net执行SQL/存储过程之易用轻量工具
- buuctf_Dest0g3_crypto
- 神奇的DEBUG:因为异常导致MongoDB容器无法启动
- jQuery烟花效果
- ffmpeg第8篇:使用ffprobe采集文件信息
- dfs 返回值用bool相对void会快一点