图片压缩


/****************************************************************************************
** 作者: Eddie Xu 
** 时间: 2018/11/29 10:34:53
** 版本: V1.0.0
** CLR: 4.0.30319.42000
** GUID: acdbc3e5-b7f9-4eb6-aacd-11c415b7fbef
** 机器名: XULQ
** 描述: 尚未编写描述
****************************************************************************************/

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Manjinba.Communication.Common.Utils
{
/// <summary>
/// 压缩图片
/// </summary>
public class CompressImageUtil
{
/// <summary>
/// 根据图片地址无损压缩图片
/// </summary>
/// <param name="sFile">原图片地址</param>
/// <param name="dFile">压缩后保存图片地址</param>
/// <param name="compressionRatio">压缩质量(数字越小压缩率越高)1-100</param>
/// <param name="isNarrowResolution">是否缩小分辨率</param>
/// <returns></returns>
public static bool ImageCompress(string sFile, string dFile, out int dHeight, out int dWidth, int compressionRatio = 90, bool isNarrowResolution = false)
{
Image iSource = Image.FromFile(sFile);
ImageFormat tFormat = iSource.RawFormat;
dHeight = 0;
dWidth = 0;
if (isNarrowResolution)
{
dHeight = iSource.Height / 2;
dWidth = iSource.Width / 2;
}
else
{
dHeight = iSource.Height;
dWidth = iSource.Width;
}
int sW, sH;
//等比例缩放
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);
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] = compressionRatio;//设置压缩的比例1-100
EncoderParameter eParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qy);
ep.Param[0] = eParam;

try
{
ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();
ImageCodecInfo jpegICIinfo = arrayICI.FirstOrDefault(t => t.FormatDescription.Equals("JPEG"));
if (jpegICIinfo != null)
{
ob.Save(dFile, jpegICIinfo, ep);//dFile是压缩后的新路径
}
else
{
ob.Save(dFile, tFormat);
}
return true;
}
catch
{
return false;
}
finally
{
iSource.Dispose();
ob.Dispose();
}
}
/// <summary>
/// 根据图片地址无损压缩图片
/// </summary>
/// <param name="sFile">原图片地址</param>
/// <param name="dFile">压缩后保存图片地址</param>
/// <param name="compressionRatio">压缩质量(数字越小压缩率越高)1-100</param>
/// <param name="isNarrowResolution">是否缩小分辨率</param>
/// <returns></returns>
public static bool ImageCompress(Stream sStream, out Stream dStream, out int dHeight, out int dWidth, int compressionRatio = 90, bool isNarrowResolution = false)
{
dStream = new MemoryStream();
Image iSource = Image.FromStream(sStream);
ImageFormat tFormat = iSource.RawFormat;
dHeight = 0;
dWidth = 0;
if (isNarrowResolution)
{
dHeight = iSource.Height / 2;
dWidth = iSource.Width / 2;
}
else
{
dHeight = iSource.Height;
dWidth = iSource.Width;
}
int sW, sH;
//等比例缩放
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);
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] = compressionRatio;//设置压缩的比例1-100
EncoderParameter eParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qy);
ep.Param[0] = eParam;

try
{
ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();
ImageCodecInfo jpegICIinfo = arrayICI.FirstOrDefault(t => t.FormatDescription.Equals("JPEG"));
if (jpegICIinfo != null)
{
ob.Save(dStream, jpegICIinfo, ep);//dStream是压缩后的新文件流
}
else
{
ob.Save(dStream, tFormat);
}
return true;
}
catch
{
return false;
}
finally
{
iSource.Dispose();
ob.Dispose();
}
}
}
}

posted @ 2019-03-16 09:01  Nine4酷  阅读(176)  评论(0编辑  收藏  举报