.net学习笔记----利用System.Drawing.Image类进行图片相关操作
C#中对图片的操作主要是通过System.Drawing.Image等类进行。
一、将图片转换为字节流

/// <summary>
/// 图片处理帮助类
/// </summary>
public static class PicProcessHelper
{
/// <summary>
/// 将图片转换为指定的字节流
/// </summary>
/// <param name="filePath">图片路径</param>
/// <returns>指定的字节流</returns>
public static byte[] ConvertToByte(String filePath)
{
var m = new System.IO.MemoryStream();
var bp = new System.Drawing.Bitmap(filePath);
bp.Save(m, System.Drawing.Imaging.ImageFormat.Jpeg); //将此图像以指定的格式保存到指定的流中。
byte[] imgByte = m.GetBuffer(); //从内存缓冲区中读取
return imgByte;
}
}

二、将字节流转换回图片

/// <summary>
/// 根据字节流返回Image类型
/// </summary>
/// <param name="streamByte"></param>
/// <returns></returns>
public static Image ReturnImage(byte[] streamByte)
{
System.IO.MemoryStream ms = new System.IO.MemoryStream(streamByte);
Image img = Image.FromStream(ms);
return img;
}

三、将Image对象转换为字节流

//将Image转换成流数据,并保存为byte[]
public static byte[] PhotoImageInsert(System.Drawing.Image imgPhoto)
{
MemoryStream mstream = new MemoryStream();
imgPhoto.Save(mstream, System.Drawing.Imaging.ImageFormat.Bmp);
byte[] byData = new Byte[mstream.Length];
mstream.Position = ;
mstream.Read(byData, , byData.Length); mstream.Close();
return byData;
}

四、保存图片
var oldFilename = @"E:\环境部署\图片集\熊猫1.jpg";
var oldImage = System.Drawing.Image.FromFile(oldFilename);
var newFilename = @"E:\我的新熊猫.jpg";
oldImage.Save(newFilename, ImageFormat.Jpeg);
五、生成缩略图

/// <summary>
/// 生成图片缩略文件
/// </summary>
/// <param name="originalImage">图片源文件</param>
/// <param name="width">缩略图宽度</param>
/// <param name="height">缩略图高度</param>
/// <param name="mode">生成缩略图的方式</param>
/// <returns>缩率处理后图片文件</returns>
public static Image MakeThumbnail(Image originalImage, int width, int height, ThumbnailModel mode)
{
int towidth = width;
int toheight = height; int x = ;
int y = ;
int ow = originalImage.Width;
int oh = originalImage.Height; switch (mode)
{
case ThumbnailModel.HighWidth: //指定高宽缩放(可能变形)
break;
case ThumbnailModel.Width: //指定宽,高按比例
toheight = originalImage.Height * width / originalImage.Width;
break;
case ThumbnailModel.Hight: //指定高,宽按比例
towidth = originalImage.Width * height / originalImage.Height;
break;
case ThumbnailModel.Default: //指定高,宽按比例
if (ow <= towidth && oh <= toheight)
{
x = -(towidth - ow) / ;
y = -(toheight - oh) / ;
ow = towidth;
oh = toheight;
}
else
{
if (ow > oh)//宽大于高
{
x = ;
y = -(ow - oh) / ;
oh = ow;
}
else//高大于宽
{
y = ;
x = -(oh - ow) / ;
ow = oh;
}
}
break;
case ThumbnailModel.Auto:
if (originalImage.Width / originalImage.Height >= width / height)
{
if (originalImage.Width > width)
{
towidth = width;
toheight = (originalImage.Height * width) / originalImage.Width;
}
else
{
towidth = originalImage.Width;
toheight = originalImage.Height;
}
}
else
{
if (originalImage.Height > height)
{
toheight = height;
towidth = (originalImage.Width * height) / originalImage.Height;
}
else
{
towidth = originalImage.Width;
toheight = originalImage.Height;
}
}
break;
case ThumbnailModel.Cut: //指定高宽裁减(不变形)
if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
{
oh = originalImage.Height;
ow = originalImage.Height * towidth / toheight;
y = ;
x = (originalImage.Width - ow) / ;
}
else
{
ow = originalImage.Width;
oh = originalImage.Width * height / towidth;
x = ;
y = (originalImage.Height - oh) / ;
}
break;
default: break;
} //新建一个bmp图片
System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight); //新建一个画板
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap); //设置高质量插值法
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //设置高质量,低速度呈现平滑程度
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; //清空画布并以透明背景色填充
g.Clear(System.Drawing.Color.White); //在指定位置并且按指定大小绘制原图片的指定部分
g.DrawImage(originalImage, new System.Drawing.Rectangle(, , towidth, toheight),
new System.Drawing.Rectangle(x, y, ow, oh),
System.Drawing.GraphicsUnit.Pixel); return bitmap;
}

其中缩略图模式定义如下:

/// <summary>
/// 缩率图处理模式
/// </summary>
public enum ThumbnailModel
{
/// <summary>
/// 指定高宽缩放(可能变形)
/// </summary>
HighWidth, /// <summary>
/// 指定宽,高按比例
/// </summary>
Width, /// <summary>
/// 默认 全图不变形
/// </summary>
Default, /// <summary>
/// 指定高,宽按比例
/// </summary>
Hight, /// <summary>
/// 指定高宽裁减(不变形)??指定裁剪区域
/// </summary>
Cut, /// <summary>
/// 自动 原始图片按比例缩放
/// </summary>
Auto
}

.net学习笔记----利用System.Drawing.Image类进行图片相关操作的更多相关文章
- 前端学习笔记(zepto或jquery)——对li标签的相关操作(一)
对li标签的相关操作——点击li标签进行样式切换的两种方式 Demo演示: 1 2 3 4 // 详解: 第一种方式(以ul为基础): $("ul").bind("cli ...
- 前端学习笔记(zepto或jquery)——对li标签的相关操作(五)
对li标签的相关操作——has与find的差异性 demo代码: <ul> <li><p>1</p></li> <li>2< ...
- 前端学习笔记(zepto或jquery)——对li标签的相关操作(四)
对li标签的相关操作——五种方式给奇数项li标签设置样式 demo演示: 1 2 3 4 5 6 7 // 详解: 通常我们为多个li添加样式时常用的是使用filter,但我们在第三节中可以看到fil ...
- 前端学习笔记(zepto或jquery)——对li标签的相关操作(三)
对li标签的相关操作——八种方式遍历li标签并获取其值 $("ul>li").forEach(function(item,index){ alert(index+" ...
- 前端学习笔记(zepto或jquery)——对li标签的相关操作(二)
对li标签的相关操作——8种方式获取li标签的第一个元素的内容 1.alert($("ul>li").first().html());2.alert($('ul>li' ...
- JavaSE学习笔记(14)---File类和IO流(字节流和字符流)
JavaSE学习笔记(14)---File类和IO流(字节流和字符流) File类 概述 java.io.File 类是文件和目录路径名的抽象表示,主要用于文件和目录的创建.查找和删除等操作. 构造方 ...
- JavaSE学习笔记(8)---常用类
JavaSE学习笔记(8)---常用类 1.Object类 java.lang.Object类是Java语言中的根类,即所有类的父类.它中描述的所有方法子类都可以使用.在对象实例化的时候,最终找的父类 ...
- 转:学习笔记: Delphi之线程类TThread
学习笔记: Delphi之线程类TThread - 5207 - 博客园http://www.cnblogs.com/5207/p/4426074.html 新的公司接手的第一份工作就是一个多线程计算 ...
- Objective-C学习笔记 利用协议实现回调函数
来源:http://mobile.51cto.com/iphone-278354.htm Objective-C学习笔记 利用协议实现回调函数是本文要介绍的内容,主要是实现一个显示文字为测试的视图,然 ...
随机推荐
- 如何使用MASM来编译、连接、调试汇编语言
先声明下,本人绝非大虾,也只是菜鸟一个,写此文的目的只是为了加深我对知识的理解罢了.好,进入正题.我是把masm解压后发在D盘中的一个叫masm的文件里,在masm文件里新建个记事本(记事本功能是很强 ...
- tomcat管理员配置
纸上得来终觉浅,绝知此事要躬行 博客园 首页 新闻 新随笔 联系 管理 随笔- 458 文章- 0 评论- 38 Tomcat的Manager显示403 Access Denied 管理to ...
- mysql 服务无法启动 服务没有报告任何错误
问题 解决方法 1.必须保证 mysql 下不存在 data 文件夹,如果存在 data 文件夹,则先删除 mysql 下的 data 文件夹,然后初始化 mysqld --initialize 服务 ...
- Lua函数之一
LUA函数之一 函数声明: function foo(arguments) statements end 1.函数调用 调用函数的时候,如果参数列表为空,必须使用()表明是函数调用,例如: os.da ...
- rsync+inotify 实现服务器之间目录文件实时同步(转)
软件简介: 1.rsync 与传统的 cp. tar 备份方式相比,rsync 具有安全性高.备份迅速.支持增量备份等优点,通过 rsync 可 以解决对实时性要求不高的数据备份需求,例如定期的备份文 ...
- QQ 腾讯QQ(简称“QQ”)是腾讯公司开发的一款基于Internet的即时通信(IM)软件
QQ 编辑 腾讯QQ(简称“QQ”)是腾讯公司开发的一款基于Internet的即时通信(IM)软件.腾讯QQ支持在线聊天.视频通话.点对点断点续传文件.共享文件.网络硬盘.自定义面板.QQ邮箱等多种功 ...
- Debian普通用户添加sudo权限
转自:http://chenpeng.info/html/964 刚安装好的Debian默认还没有sudo功能.1.安装sudo# apt-get install sudo2.修改 /etc/sudo ...
- 为自己的git添加alias,命令缩写
在多人协作开发时,一般用git来进行代码管理.git有一些命令如:git pull . git push等等,这些命令可以设置alias,也就是缩写.如:git pull 是 git pl, git ...
- 教你如何---构建良好的windows程序(初学者必看)
一使用菜单栏和工具栏 1.菜单栏和工具栏有什么作用和优点: 通过菜单栏把应用程序的功能进行分组,能够方便用户查找和使用,下图所示的菜单栏包含的每一项都是顶层菜单项,顶层菜单项下的选项称为”子菜单”或” ...
- Toast工具类,Android中不用再每次都写烦人的Toast了
package com.zhanggeng.contact.tools; /** * Toasttool can make you use Toast more easy ; * * @author ...