代码:

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# 实现图片压缩的更多相关文章

  1. Golang 编写的图片压缩程序,质量、尺寸压缩,批量、单张压缩

    目录: 前序 效果图 简介 全部代码 前序: 接触 golang 不久,一直是边学边做,边总结,深深感到这门语言的魅力,等下要跟大家分享是最近项目 服务端 用到的图片压缩程序,我单独分离了出来,做成了 ...

  2. 三款不错的图片压缩上传插件(webuploader+localResizeIMG4+LUploader)

    涉及到网页图片的交互,少不了图片的压缩上传,相关的插件有很多,相信大家都有用过,这里我就推荐三款,至于好处就仁者见仁喽: 1.名气最高的WebUploader,由Baidu FEX 团队开发,以H5为 ...

  3. 前端构建工具之gulp(一)「图片压缩」

    前端构建工具之gulp(一)「图片压缩」 已经很久没有写过博客了,现下终于事情少了,开始写博吧 今天网站要做一些优化:图片压缩,资源合并等 以前一直使用百度的FIS工具,但是FIS还没有提供图片压缩的 ...

  4. gulp图片压缩

    gulp图片压缩 网页性能优化,通常要处理图片,尤其图片量大的时候,更需要工具来批量处理,这里使用gulp,做个简单总结 image-resize压缩尺寸 var gulp = require('gu ...

  5. Android 图片压缩、照片选择、裁剪,上传、一整套图片解决方案

    1.Android一整套图片解决方案 http://mp.weixin.qq.com/s?__biz=MzAxMTI4MTkwNQ==&mid=2650820998&idx=1& ...

  6. Java中图片压缩处理

    原文http://cuisuqiang.iteye.com/blog/2045855 整理文档,搜刮出一个Java做图片压缩的代码,稍微整理精简一下做下分享. 首先,要压缩的图片格式不能说动态图片,你 ...

  7. android 图片压缩

    引用:http://104zz.iteye.com/blog/1694762 第一:我们先看下质量压缩方法: private Bitmap compressImage(Bitmap image) { ...

  8. HTML5 CANVAS 实现图片压缩和裁切

    原文地址:http://leonshi.com/2015/10/31/html5-canvas-image-compress-crop/?utm_source=tuicool&utm_medi ...

  9. C# 图片压缩

    /// <summary>        /// 图片压缩方法        /// </summary>        /// <param name="sF ...

  10. Html5+asp.net mvc 图片压缩上传

    在做图片上传时,大图片如果没有压缩直接上传时间会非常长,因为有的图片太大,传到服务器上再压缩太慢了,而且损耗流量. 思路是将图片抽样显示在canvas上,然后用通过canvas.toDataURL方法 ...

随机推荐

  1. 修改UITextView光标高度

    自定义UITextView文字字体时,经常出现光标与字体的高度不匹配,可以通过下面代码修改默认的光标高度, //创建子类重写UITextView方法 - (CGRect)caretRectForPos ...

  2. Ubuntu-18.04Python2与Python3自由切换

    一.配置ssh链接 安装openssh-server devops@devops-virtual-machine:~$ sudo apt-get install openssh-server 二.安装 ...

  3. 配置静态IP

    网卡配置静态IP地址编辑文件/etc/network/interfaces:sudo vi /etc/network/interfaces并用下面的行来替换有关eth0的行:# The primary ...

  4. Codeforces 703B. Mishka and trip 模拟

    B. Mishka and trip time limit per test:1 second memory limit per test:256 megabytes input:standard i ...

  5. Java数据结构和算法(一)线性结构

    Java数据结构和算法(一)线性结构 数据结构与算法目录(https://www.cnblogs.com/binarylei/p/10115867.html) 线性表 是一种逻辑结构,相同数据类型的 ...

  6. 数据帮助类DBhelper的定义

    数据库帮助类的使用DBhelperusing System;using System.Collections.Generic;using System.Text;using System.Data;u ...

  7. String [] args是干什么的

         我相信应该有不少人都疑惑,main后面的这个String [] args是干什么的呢?今天,巩固就为你们解密.      这是干什么的呢?先给大家一个简单定义(本人比较讨厌上来就举例子,因为 ...

  8. mac安全与隐私只有两个选项,少了一个任何来源

    很多软件安装后就会出现,程序已损坏,请移到废纸篓的提示. 解决方法:在终端里输入:sudo spctl --master-disable 然后回车,然后输入密码,即可在安全选项中看到重新出现的允许任何 ...

  9. 二进制搭建kubernetes多master集群【开篇、集群环境和功能介绍】

    本文主要说明kubernetes集群使用组建的版本和功能介绍.. 一.组件版本 Kubernetes 1.12.3 Docker 18.06.1-ce Etcd 3.3.10 Flanneld 0.1 ...

  10. 2018.09.22 牧场的安排(状压dp)

    描述 农民 John 购买了一处肥沃的矩形牧场,分成M*N(1 <= M <= 12; 1 <= N <= 12)个 格子.他想在那里的一些格子中种植美味的玉米.遗憾的是,有些 ...