C# 实现图片压缩
代码:
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 }
}
C# 实现图片压缩的更多相关文章
- Golang 编写的图片压缩程序,质量、尺寸压缩,批量、单张压缩
目录: 前序 效果图 简介 全部代码 前序: 接触 golang 不久,一直是边学边做,边总结,深深感到这门语言的魅力,等下要跟大家分享是最近项目 服务端 用到的图片压缩程序,我单独分离了出来,做成了 ...
- 三款不错的图片压缩上传插件(webuploader+localResizeIMG4+LUploader)
涉及到网页图片的交互,少不了图片的压缩上传,相关的插件有很多,相信大家都有用过,这里我就推荐三款,至于好处就仁者见仁喽: 1.名气最高的WebUploader,由Baidu FEX 团队开发,以H5为 ...
- 前端构建工具之gulp(一)「图片压缩」
前端构建工具之gulp(一)「图片压缩」 已经很久没有写过博客了,现下终于事情少了,开始写博吧 今天网站要做一些优化:图片压缩,资源合并等 以前一直使用百度的FIS工具,但是FIS还没有提供图片压缩的 ...
- gulp图片压缩
gulp图片压缩 网页性能优化,通常要处理图片,尤其图片量大的时候,更需要工具来批量处理,这里使用gulp,做个简单总结 image-resize压缩尺寸 var gulp = require('gu ...
- Android 图片压缩、照片选择、裁剪,上传、一整套图片解决方案
1.Android一整套图片解决方案 http://mp.weixin.qq.com/s?__biz=MzAxMTI4MTkwNQ==&mid=2650820998&idx=1& ...
- Java中图片压缩处理
原文http://cuisuqiang.iteye.com/blog/2045855 整理文档,搜刮出一个Java做图片压缩的代码,稍微整理精简一下做下分享. 首先,要压缩的图片格式不能说动态图片,你 ...
- android 图片压缩
引用:http://104zz.iteye.com/blog/1694762 第一:我们先看下质量压缩方法: private Bitmap compressImage(Bitmap image) { ...
- HTML5 CANVAS 实现图片压缩和裁切
原文地址:http://leonshi.com/2015/10/31/html5-canvas-image-compress-crop/?utm_source=tuicool&utm_medi ...
- C# 图片压缩
/// <summary> /// 图片压缩方法 /// </summary> /// <param name="sF ...
- Html5+asp.net mvc 图片压缩上传
在做图片上传时,大图片如果没有压缩直接上传时间会非常长,因为有的图片太大,传到服务器上再压缩太慢了,而且损耗流量. 思路是将图片抽样显示在canvas上,然后用通过canvas.toDataURL方法 ...
随机推荐
- Django的mode的分组查询和聚合查询和F查询和Q查询
1.聚合查询 # 聚合函数aggregate,求指定字段的最大值,最小值,平均值,和的值,方法如下 from django.db.models import Avg from django.db.mo ...
- 基于快速排序的数组划分:2组 3组 K组(sort color)大小写排序 · Partition Array
2组: [抄题]: 给出一个整数数组 nums 和一个整数 k.划分数组(即移动数组 nums 中的元素),使得: 所有小于k的元素移到左边 所有大于等于k的元素移到右边 返回数组划分的位置,即数组中 ...
- php在线支付流程
1.企业与银行的两种接入方式: (1).企业直接与银行对接. 优点:直接与银行进行财务结算,资金安全,适合资金流较大企业. 缺点:开发和维护工作量较大,分别与每家银行签订合同,每年需交 ...
- FormValidator表单验证
所需的库文件 红色框内是所需要的JavaScript库文件. <%@ page language="java" contentType="text/html; ch ...
- db2 托管事务未设置方法有问题
https://q.cnblogs.com/q/63862
- 学习bn算法
好处: 1.归一化后有什么好处呢?原因在于神经网络学习过程本质就是为了学习数据分布,一旦训练数据与测试数据的分布不同,那么网络的泛化能力也大大降低: 2.另外一方面,一旦每批训练数据的分布各不相同(b ...
- python函数、装饰器、迭代器、生成器
目录: 函数补充进阶 函数对象 函数的嵌套 名称空间与作用域 闭包函数 函数之装饰器 函数之迭代器 函数之生成器 内置函数 一.函数补充进阶 1.函数对象: 函数是第一类对象,即函数可以当作数据传递 ...
- 2018.09.19 atcoder AtCoDeer and Rock-Paper(贪心)
传送门 sb贪心啊. 显然能选帕子就选帕子. 首先假设第一个人全出石头. 考虑把一些石头修改成帕子. 这样贡献只增不减,加起来就是答案. 代码: #include<bits/stdc++.h&g ...
- 2018.09.11 loj#10216.五指山(exgcd)
传送门 就是一个exgcd的板子. 但注意算距离差的时候是在一个环上面算. 还有,答案要开long long233... 注意这两点之后就是exgcd板子了. 代码: #include<bits ...
- 2018.07.31cogs2964. 数列操作η(线段树)
传送门 线段树基本操作. 给出一个排列b,有一个初始值都为0的数组a,维护区间加1,区间统计区间∑(ai/bi)" role="presentation" style=& ...