/// <summary>
/// 根据指定尺寸得到按比例缩放的尺寸,返回true表示以更改尺寸
/// </summary>
/// <param name="picWidth">图片宽度</param>
/// <param name="picHeight">图片高度</param>
/// <param name="specifiedWidth">指定宽度</param>
/// /// <param name="specifiedHeight">指定高度</param>
/// <returns>返回true表示以更改尺寸</returns>
private bool GetPicZoomSize(ref int picWidth, ref int picHeight, int specifiedWidth, int specifiedHeight)
{
int sW = 0, sH = 0;
Boolean isZoomSize = false;
//按比例缩放
Size tem_size = new Size(picWidth, picHeight);
if (tem_size.Width > specifiedWidth || tem_size.Height > specifiedHeight) //将**改成c#中的或者操作符号
{
if ((tem_size.Width * specifiedHeight) > (tem_size.Height * specifiedWidth))
{
sW = specifiedWidth;
sH = (specifiedWidth * tem_size.Height) / tem_size.Width;
}
else
{
sH = specifiedHeight;
sW = (tem_size.Width * specifiedHeight) / tem_size.Height;
}
isZoomSize = true;
}
else
{
sW = tem_size.Width;
sH = tem_size.Height;
}
picHeight = sH;
picWidth = sW;
return isZoomSize;
}
/// <summary>
/// 无损压缩图片
/// </summary>
/// <param name="sFile">原图片</param>
/// <param name="dFile">压缩后保存位置</param>
/// <param name="dHeight">高度</param>
/// <param name="dWidth">宽度</param>
/// <param name="flag">压缩质量 1-100</param>
/// <returns></returns> public bool GetPicThumbnail(string sFile, string dFile, int dHeight, int dWidth, int flag)
{
System.Drawing.Image iSource = System.Drawing.Image.FromFile(sFile);
ImageFormat tFormat = iSource.RawFormat;
int sW = iSource.Width, sH = iSource.Height; GetPicZoomSize(ref sW, ref sH, dWidth, dHeight); Bitmap ob = new Bitmap(dWidth, dHeight);
Graphics g = Graphics.FromImage(ob);
g.Clear(Color.WhiteSmoke);
g.CompositingQuality = CompositingQuality.HighQuality;
g.SmoothingMode = SmoothingMode.HighQuality;
g.InterpolationMode = 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是压缩后的新路径
}
else
{
ob.Save(dFile, tFormat);
}
return true;
}
catch
{
return false;
}
finally
{
iSource.Dispose();
ob.Dispose(); }
}

缩放图片也会改变图片大小

C#无损压缩图片的更多相关文章

  1. Grunt中批量无损压缩图片插件--Grunt-contrib-imagemin

    Photoshop 切出的图片,无论是 PNG 还是 JPEG/JPG 格式,都含有许多相关信息,又或多余的颜色值,这些信息和颜色值,对网页前端并没有用处,反而增加图片大小,所以 Google Pag ...

  2. gulp 无损压缩图片

    在做项目中,美工有时候会给一些比较大的图片,在做网站的时候,图片太大会影响加载速度.因此,我们需要无损压缩图片. 在尝试过几个压缩图片的方法,发现gulp中的gulp-tinypng-nokey插件是 ...

  3. Grunt中批量无损压缩图片插件--grunt-sprite

    这是什么 这是一个帮助前端开发工程师将css代码中的切片合并成雪碧图的工具,它的主要功能是: 使用二叉树排列算法,对css文件进行处理,收集切片序列,生成雪碧图 在原css代码中为切片添加backgr ...

  4. .net 图片无损压缩

    命名空间: using System.Drawing.Imaging; using System.Drawing; using System.Drawing.Drawing2D; #region Ge ...

  5. C#/.net 通过js调用系统相机进行拍照,图片无损压缩后进行二维码识别

    这两天撸了一个需求,通过 JS  调用手机后置相机,进行拍照扫码.前台实现调用手机相机,然后截取图片并上传到后台的功能.后台接收传过来的图片后,通过调用开源二维码识别库 ZXing 进行二维码数据解析 ...

  6. c#无损高质量压缩图片

    这几天在做同城交友网www.niyuewo.com时遇到的一个问题,如何将会员的头像压缩,在网上搜索整理如下:在此也感谢医药精(www.yiyaojing.com)站长的帮忙 /// <summ ...

  7. c# 无损高质量压缩图片代码

    /// <summary> /// 无损压缩图片 /// </summary> /// <param name="sFile">原图片</ ...

  8. 性能优化——Android图片压缩与优化的几种方式

    图片优化压缩方式大概可以分为以下几类:更换图片格式,质量压缩,采样率压缩,缩放压缩,调用jpeg压缩等1.设置图片格式Android目前常用的图片格式有png,jpeg和webp,png:无损压缩图片 ...

  9. 使用JQuery插件Jcrop进行图片截取

    Jcrop插件本身并不含有图片截取功能,它仅仅是在前端层面构建一套截取动画效果并产生4个坐标点,插件使用者将这4个坐标点传回至服务器接口上进行截取操作.其优点是具有较高的通用性.浏览器兼容性(IE6+ ...

随机推荐

  1. codeforces626D . Jerry's Protest (概率)

    Andrew and Jerry are playing a game with Harry as the scorekeeper. The game consists of three rounds ...

  2. zoj3545Rescue the Rabbit (AC自动机+状压dp+滚动数组)

    Time Limit: 10 Seconds      Memory Limit: 65536 KB Dr. X is a biologist, who likes rabbits very much ...

  3. Git命令回退代码并同步到远程仓库

    git log 找到要回退的commit版本号并复制 git reset --hard [commitid] 本地库版本回退 git push -f origin [branchName] 同步到远端 ...

  4. 如何在windows上升级Powershell到5.1版本?

    前言 此篇我们说的是Powershell5.1低版本到5.1的升级,对于Powershell6(及以上版本)可以跨平台独立安装,在windows上可与之前的版本并存. 首先要整清楚Powershell ...

  5. Kubernets二进制安装(16)之安装部署traefik(ingress)

    K8S的DNS实现了服务在集群"内"被自动发现,如何使得服务在Kuberneters集群"外"被使用和访问呢,有二种方法 1)使用NodePort型的Servi ...

  6. 网络安全-WEB基础,burpsuite,WEB漏洞

    1. web基础 HTTP: GET POST REQUEST RESPONSE... JDK robots.txt 网页源代码/注释 目录扫描--御剑,dirmap 端口信息--nmap 备份文件- ...

  7. 鸟哥的linux私房菜——第十二章学习(Shell Scripts)

    第十二章  Shell Scripts 1.0).什么是shell scripts? script 是"脚本.剧本"的意思.整句话是说, shell script 是针对 shel ...

  8. GitHub Classroom

    GitHub Classroom GitHub Education https://classroom.github.com/classrooms https://classroom.github.c ...

  9. Chrome V8 引擎源码剖析

    Chrome V8 引擎源码剖析 V8 https://github.com/v8/v8 array & sort https://github.com/v8/v8/search?l=Java ...

  10. JavaScript Semicolon Insertion

    JavaScript Semicolon Insertion https://blog.izs.me/2010/12/an-open-letter-to-javascript-leaders-rega ...