代码:

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. Py2exe——将python程序变成windows下可执行的exe

    一.安装Py2exe 二.定义一个目录,把你的Python文件放在下面,如为AddFileRandom.py文件 然后新建一个go.py文件,放于相同目录下,内容为: from distutils.c ...

  2. oracle pl/sql程序

    简单的pl/sql程序 declare begin dbms_output.put_line('hello world'); end; 什么是PL/SQL? pl/sql(Procedure lang ...

  3. 【深度好文】多线程之WaitHandle-->派生-》Mutex信号量构造

    bool flag = false; System.Threading.Mutex mutex = new System.Threading.Mutex(true, "Test", ...

  4. cout<<endl 本质探索

    C++中,有一种对象叫操控器(manipulators),专门用来操控stream的对象,在C++标准中,预定义好几种操控器,常见的有: flush 刷新output缓冲区,将内容写入输出设备 end ...

  5. ubuntu查找命令比较

    1. find find是最常见和最强大的查找命令,你可以用它找到任何你想找的文件.    find的使用格式如下:     $ find <指定目录> <指定条件> < ...

  6. 解决Axure发布分享预览的3个方法

    公司的同事制作的一个产品原型,要发给我,我当时正在客户这里,电脑上并没有Axure,客户又催得急,感到一阵无奈.这次回来之后,经过一番摸索,发现还是有办法的.这里给大家分享一下Axure发布分享预览的 ...

  7. KOBAS

    1. What is KOBAS 3.0? KOBAS (KEGG Orthology Based Annotation System) is a web server for gene/protei ...

  8. Perl的调试模式熟悉和应用

    perl -d file.pl perl -c file.pl DB<1> hList/search source lines:               Control script ...

  9. Pseudo-class和pseudo-element的差别

    相同点: Pseudo-class和pseudo-element的语法都是以selector或者selector.class开始的. 不同点: Pseudo-class的操作对象是文档树中已有的元素, ...

  10. 支持向量机(SVM)算法

    支持向量机(support vector machine)是一种分类算法,通过寻求结构化风险最小来提高学习机泛化能力,实现经验风险和置信范围的最小化,从而达到在统计样本量较少的情况下,亦能获得良好统计 ...