代码:

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 }
}

最新文章

  1. extern &ldquo;C&rdquo;原理,用法以及使用场景-2016.01.05
  2. (六)、nodejs中的express框架获取http参数
  3. Sublime Text 编辑器
  4. GSS1 spoj 1043 Can you answer these queries I 最大子段和
  5. velocity-字母序号 list
  6. Python学习笔记4(函数与模块)
  7. Qrcode生成二维码支持中文,带图片,带文字
  8. Picasso解决 TextView加载html图片异步显示
  9. Raspberry树莓派学习笔记2—配置RobotFramework自动化测试环境
  10. Android内核sysfs中switch类使用实例
  11. ITP项目:一期版本分享
  12. VM下设置CenOS为静态IP
  13. 转载 c++指针 指针入门
  14. WebRTC信令控制简介与STUN, TURN服务器搭建
  15. 如何让你的Ssh连接,更加安全?
  16. 「日常训练」ZgukistringZ(Codeforces Round #307 Div. 2 B)
  17. 【转载】R中有关数据挖掘的包
  18. ASP.NET Web API 2 返回 Json格式
  19. Python中logging模块的基本用法
  20. 应用索引技术优化SQL 语句(转)

热门文章

  1. 条款2:尽量以const, enum, inline替换#define
  2. C++重载、重写(覆盖)、隐藏
  3. 找峰值I II &#183; Find Peak Element I ii
  4. iOS 11 scroll滚动偏移,tableview偏移44,获取view的宽和高
  5. jQuery控制TR显示隐藏
  6. MySQL主从复制备份
  7. Java数据结构和算法(七)B+ 树
  8. sql删除重复记录
  9. Redis可以作为简单搜索引擎优化查询
  10. 使用WebUploader客户端批量上传图片,后台使用springMVC接收实例