C# 实现图片压缩
2024-10-14 15:31:21
代码:
private static ImageCodecInfo GetImageCodecInfo(ImageFormat imageFormat)
{
ImageCodecInfo[] imageCodecInfoArr = ImageCodecInfo.GetImageDecoders();
foreach (ImageCodecInfo imageCodecInfo in imageCodecInfoArr)
{
if (imageCodecInfo.FormatID == imageFormat.Guid)
{
return imageCodecInfo;
}
}
return null;
}
代码:
MemoryStream ms = HttpUtil.HttpDownloadFile(url);
Bitmap bmp = new Bitmap(ms); EncoderParameters encoderParameters = new EncoderParameters();
EncoderParameter encoderParameter = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 75L);
encoderParameters.Param[] = encoderParameter; MemoryStream msCompress = new MemoryStream();
bmp.Save(msCompress, GetImageCodecInfo(ImageFormat.Jpeg), encoderParameters);
Bitmap bmpCompress = new Bitmap(msCompress);
bmpCompress.Save(path);
bmp.Save(path2); msCompress.Close();
ms.Close();
代码:
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 SunCreate.Common.ComLib
{
/// <summary>
/// 图片缩放
/// </summary>
public class ZoomImageUtil
{
#region 图片缩放
/// <summary>
/// 图片缩放
/// </summary>
/// <param name="bArr">图片字节流</param>
/// <param name="width">目标宽度,若为0,表示宽度按比例缩放</param>
/// <param name="height">目标长度,若为0,表示长度按比例缩放</param>
public static byte[] GetThumbnail(byte[] bArr, int width, int height)
{
if (bArr == null) return null;
MemoryStream ms = new MemoryStream(bArr);
Bitmap bmp = (Bitmap)Image.FromStream(ms);
ms.Close(); bmp = GetThumbnail(bmp, width, height); ImageCodecInfo imageCodecInfo = GetEncoder(ImageFormat.Jpeg);
EncoderParameters encoderParameters = new EncoderParameters();
EncoderParameter encoderParameter = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 75L);
encoderParameters.Param[] = encoderParameter; ms = new MemoryStream();
bmp.Save(ms, imageCodecInfo, encoderParameters);
byte[] result = ms.ToArray();
ms.Close();
bmp.Dispose(); return result;
}
#endregion #region 图片缩放
/// <summary>
/// 图片缩放
/// </summary>
/// <param name="bmp">图片</param>
/// <param name="width">目标宽度,若为0,表示宽度按比例缩放</param>
/// <param name="height">目标长度,若为0,表示长度按比例缩放</param>
private static Bitmap GetThumbnail(Bitmap bmp, int width, int height)
{
if (width == && height == )
{
width = bmp.Width;
height = bmp.Height;
}
else
{
if (width == )
{
width = height * bmp.Width / bmp.Height;
}
if (height == )
{
height = width * bmp.Height / bmp.Width;
}
} Image imgSource = bmp;
Bitmap outBmp = new Bitmap(width, height);
Graphics g = Graphics.FromImage(outBmp);
g.Clear(Color.Transparent);
// 设置画布的描绘质量
g.CompositingQuality = CompositingQuality.Default;
g.SmoothingMode = SmoothingMode.Default;
g.InterpolationMode = InterpolationMode.Default;
g.DrawImage(imgSource, new Rectangle(, , width, height + ), , , imgSource.Width, imgSource.Height, GraphicsUnit.Pixel);
g.Dispose();
imgSource.Dispose();
bmp.Dispose();
return outBmp;
}
#endregion #region 椭圆形缩放
/// <summary>
/// 椭圆形缩放
/// </summary>
/// <param name="bArr">图片字节流</param>
/// <param name="width">目标宽度,若为0,表示宽度按比例缩放</param>
/// <param name="height">目标长度,若为0,表示长度按比例缩放</param>
public static byte[] GetEllipseThumbnail(byte[] bArr, int width, int height)
{
if (bArr == null) return null;
MemoryStream ms = new MemoryStream(bArr);
Bitmap bmp = (Bitmap)Image.FromStream(ms); Bitmap newBmp = new Bitmap(width, height);
using (Graphics g = Graphics.FromImage(newBmp))
{
using (TextureBrush br = new TextureBrush(bmp))
{
br.ScaleTransform(width / (float)bmp.Width, height / (float)bmp.Height);
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
g.FillEllipse(br, new Rectangle(Point.Empty, new Size(width, height)));
}
}
MemoryStream newMs = new MemoryStream();
newBmp.Save(newMs, System.Drawing.Imaging.ImageFormat.Png);
byte[] result = newMs.ToArray(); bmp.Dispose();
newBmp.Dispose();
ms.Close();
newMs.Dispose(); return result;
}
#endregion #region GetEncoder
private static ImageCodecInfo GetEncoder(ImageFormat format)
{
ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();
foreach (ImageCodecInfo codec in codecs)
{
if (codec.FormatID == format.Guid)
{
return codec;
}
}
return null;
}
#endregion }
}
最新文章
- extern &ldquo;C&rdquo;原理,用法以及使用场景-2016.01.05
- (六)、nodejs中的express框架获取http参数
- Sublime Text 编辑器
- GSS1 spoj 1043 Can you answer these queries I 最大子段和
- velocity-字母序号 list
- Python学习笔记4(函数与模块)
- Qrcode生成二维码支持中文,带图片,带文字
- Picasso解决 TextView加载html图片异步显示
- Raspberry树莓派学习笔记2—配置RobotFramework自动化测试环境
- Android内核sysfs中switch类使用实例
- ITP项目:一期版本分享
- VM下设置CenOS为静态IP
- 转载 c++指针 指针入门
- WebRTC信令控制简介与STUN, TURN服务器搭建
- 如何让你的Ssh连接,更加安全?
- 「日常训练」ZgukistringZ(Codeforces Round #307 Div. 2 B)
- 【转载】R中有关数据挖掘的包
- ASP.NET Web API 2 返回 Json格式
- Python中logging模块的基本用法
- 应用索引技术优化SQL 语句(转)