您的位置:首页 > 游戏 > 游戏 > 工程造价询价网站_电子版简历免费的_百度热搜广告设计公司_深圳市住房和建设局

工程造价询价网站_电子版简历免费的_百度热搜广告设计公司_深圳市住房和建设局

2024/11/16 2:31:38 来源:https://blog.csdn.net/zl2121016/article/details/142999775  浏览:    关键词:工程造价询价网站_电子版简历免费的_百度热搜广告设计公司_深圳市住房和建设局
工程造价询价网站_电子版简历免费的_百度热搜广告设计公司_深圳市住房和建设局

1、使用System.Drawing库来压缩图片

using System.Drawing;
using System.Drawing.Imaging;
using System.IO;/// <summary>/// 压缩图片到目标大小,并覆盖原始图片/// </summary>/// <param name="imagePath">图片路径</param>/// <param name="targetSizeKb">目标大小(单位:KB)</param>public static void CompressImage(string imagePath, int targetSizeKb){using (var originalImage = Image.FromFile(imagePath)){// 尝试不同的质量因子进行压缩for (int quality = 90; quality > 10; quality -= 5){using (var ms = new MemoryStream()){// 设置压缩参数var encoderParameters = new EncoderParameters(1);encoderParameters.Param[0] = new EncoderParameter(Encoder.Quality, quality);var codec = GetEncoder(ImageFormat.Jpeg);// 保存图片到内存流中originalImage.Save(ms, codec, encoderParameters);// 如果达到目标大小,则保存并覆盖原图if (ms.Length <= targetSizeKb * 1024){File.WriteAllBytes(imagePath, ms.ToArray());Console.WriteLine($"压缩成功:{imagePath} (新大小: {ms.Length / 1024} KB)");return;}}}Console.WriteLine($"无法将图片压缩到 {targetSizeKb} KB 以下:{imagePath}");}}/// <summary>/// 获取指定格式的图片编码器/// </summary>/// <param name="format">图片格式</param>/// <returns>图片编码器</returns>static ImageCodecInfo GetEncoder(ImageFormat format){return ImageCodecInfo.GetImageDecoders().FirstOrDefault(codec => codec.FormatID == format.Guid);}

2、使用SixLabors.ImageSharp压缩图片

using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Formats.Jpeg;
using SixLabors.ImageSharp.Processing;
using System.IO;public static void CompressImage(string imagePath, int targetSizeKb){using (var image = Image.Load(imagePath)){int quality = 75;for (; quality >= 10; quality -= 5){using (var ms = new MemoryStream()){var encoder = new JpegEncoder { Quality = quality };image.Save(ms, encoder);if (ms.Length <= targetSizeKb * 1024){File.WriteAllBytes(imagePath, ms.ToArray());Console.WriteLine($"压缩成功:{imagePath} (新大小: {ms.Length / 1024} KB)");return;}}}Console.WriteLine($"无法将图片压缩到 {targetSizeKb} KB 以下:{imagePath}");}

3、

 public static bool CompressImage(Stream stream, string dFile, int flag = 90, int size = 300, bool sfsc = true){using (Image iSource = Image.FromStream(stream)){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);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-100EncoderParameter 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(stream, dFile, flag, size, false);}}else{ob.Save(dFile, tFormat);}return true;}catch{return false;}finally{iSource.Dispose();ob.Dispose();}}}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com