相关文章推荐
坚强的手链  ·  pandas ...·  6 月前    · 
任性的机器猫  ·  A Postoperative ...·  1 年前    · 
酷酷的牙膏  ·  Android 12 Wifi ...·  1 年前    · 
/// /// private static Image Resizer(this Image bmp, int width) if (bmp.Width < width || width == 0) return bmp; Bitmap ob = new Bitmap(width, bmp.Height * width / bmp.Width); using (var g = Graphics.FromImage(ob)) g.Clear(Color.WhiteSmoke); g.DrawImage(bmp, new Rectangle(0, 0, ob.Width, ob.Height), 0, 0, bmp.Width, bmp.Height, GraphicsUnit.Pixel); return ob; /// /// path:路径;quality:质量; /// /// /// 路径 /// 质量 public static void Save(this Image bmp, string path, int quality) EncoderParameters eps = new EncoderParameters(); eps.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality); bmp.Save(path, ImageCodecInfo.GetImageEncoders().FirstOrDefault(x => x.FormatID == ImageFormat.Jpeg.Guid), eps); public void ConfigureServices(IServiceCollection services) if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) var pngquant = Configuration.GetSection("PngquantPathLinux").Value; PngquantConfig.Configure(new PngquantOptions { BinaryFolder = pngquant }); if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { var pngquant = Configuration.GetSection("PngquantPathWin").Value; PngquantConfig.Configure(new PngquantOptions { BinaryFolder = pngquant }); FileStream fs = new FileStream(filePath, FileMode.Open); byte[] byData = new byte[fs.Length]; //设置 压缩 选项 var options = new PngQuantOptions() QualityMinMax = (65, 80), //Minimum = 65, Maximum = 80. Default null Speed = 1, //Value between 1 and 11. default 3. IEBug = false, //Attempt to fix iebug. default false. Bit = 256 //bit-rate. default 256 ///Invoke the compressor Compressor pngQuant = new PngQuant(options); //Compress bytes byte[] compressed = await pngQuant.Compress(byData); MemoryStream ms = new MemoryStream(compressed); //把那个byte[] 数组传进去, 然后 using (FileStream fs = new FileStream(savePath, FileMode.Create, FileAccess.Write)) ms.WriteTo(fs);
背景: .net core 中默认已经取消可以直接访问 图片 ,因为这样不安全. 导致我们上传的 图片 无法直接通过url访问. 解决方案: 一: 通过修改项目配置,使可以直接通过url访问.(方法略,可以百度); 二: 图片 都通过接口返回,接口里面读取项目的 图片 ,然后返回流; 新建 .net core 2.0 项目(过程略) 通过nuget添加引用 System.Drawing.Common; .net core 开始的时候并没有System.Drawing,在2.0之后新增了 System.Dr