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类进行图片相关操作的更多相关文章

  1. 前端学习笔记(zepto或jquery)——对li标签的相关操作(一)

    对li标签的相关操作——点击li标签进行样式切换的两种方式 Demo演示: 1 2 3 4 // 详解: 第一种方式(以ul为基础): $("ul").bind("cli ...

  2. 前端学习笔记(zepto或jquery)——对li标签的相关操作(五)

    对li标签的相关操作——has与find的差异性 demo代码: <ul> <li><p>1</p></li> <li>2< ...

  3. 前端学习笔记(zepto或jquery)——对li标签的相关操作(四)

    对li标签的相关操作——五种方式给奇数项li标签设置样式 demo演示: 1 2 3 4 5 6 7 // 详解: 通常我们为多个li添加样式时常用的是使用filter,但我们在第三节中可以看到fil ...

  4. 前端学习笔记(zepto或jquery)——对li标签的相关操作(三)

    对li标签的相关操作——八种方式遍历li标签并获取其值 $("ul>li").forEach(function(item,index){ alert(index+" ...

  5. 前端学习笔记(zepto或jquery)——对li标签的相关操作(二)

    对li标签的相关操作——8种方式获取li标签的第一个元素的内容 1.alert($("ul>li").first().html());2.alert($('ul>li' ...

  6. JavaSE学习笔记(14)---File类和IO流(字节流和字符流)

    JavaSE学习笔记(14)---File类和IO流(字节流和字符流) File类 概述 java.io.File 类是文件和目录路径名的抽象表示,主要用于文件和目录的创建.查找和删除等操作. 构造方 ...

  7. JavaSE学习笔记(8)---常用类

    JavaSE学习笔记(8)---常用类 1.Object类 java.lang.Object类是Java语言中的根类,即所有类的父类.它中描述的所有方法子类都可以使用.在对象实例化的时候,最终找的父类 ...

  8. 转:学习笔记: Delphi之线程类TThread

    学习笔记: Delphi之线程类TThread - 5207 - 博客园http://www.cnblogs.com/5207/p/4426074.html 新的公司接手的第一份工作就是一个多线程计算 ...

  9. Objective-C学习笔记 利用协议实现回调函数

    来源:http://mobile.51cto.com/iphone-278354.htm Objective-C学习笔记 利用协议实现回调函数是本文要介绍的内容,主要是实现一个显示文字为测试的视图,然 ...

随机推荐

  1. PHP大批量插入数据库的3种方法和速度对比

    第一种,使用insert into 插入,最后显示为:23:25:05 01:32:05 也就是花了2个小时多! $params = array(‘value'=>'50′); set_time ...

  2. CSS创建一个遮罩层

    .layer{ width: 100%; position: absolute; left:; right:; top:; bottom:; -moz-opacity:; filter: alpha( ...

  3. java常见异常类图(分类了Error/RuntimeExecption、check Exception)

    版权:欧初权 http://www.cnblogs.com/langtianya/p/4435537.html

  4. @suppressWarnings解释

    J2SE 提供的最后一个批注是 @SuppressWarnings.该批注的作用是给编译器一条指令,告诉它对被批注的代码元素内部的某些警告保持静默. 一点背景:J2SE 5.0 为 Java 语言增加 ...

  5. [实战]MVC5+EF6+MySql企业网盘实战(28)——其他列表

    写在前面 本篇文章将实现,其他文件类型的列表. 系列文章 [EF]vs15+ef6+mysql code first方式 [实战]MVC5+EF6+MySql企业网盘实战(1) [实战]MVC5+EF ...

  6. Linux如何删除以分号开头的文件

    发现在创建文件时,有的时候会不小心创建以分号开头的文件. 如何删除呢? 执行  rm \;   即可删除 把以;号开头的文件名转义后再删除 创建文件:vi  index.php 或者vim  inde ...

  7. Sublime Text2 快捷键 (MAC版)

    工具是人的延伸,可以把人变得更聪明更强大,人类正是学会了使用工具,才创造出现在的文明.作为程序员,工具开发.使用是其能力的重要体现,业内的大牛都是造工具的好手.目前身边很多人都在用sublime te ...

  8. Android自定义遮罩层设计

    在做网页设计时,前端设计人员会经常用到基于JS开发的遮罩层,并且背景半透明.这样的效果怎么样在Android上实现呢?这个实现并不困难,先来上效果图: <ignore_js_op> 201 ...

  9. 关于visio 2007导入独立图库

    很多作网络拓扑或服务器系统拓扑时,我们都会找到相关的Visio图库来画,但很多时候我们不知如何才能够直接导入,下面是我自己的导入方式,供大家参考下! 打开07Visio,自动加载设置: 工具--> ...

  10. [Effective JavaScript 笔记]第24条:使用变量保存arguments对象

    迭代器(iterator)是一个可以顺序存取数据集合的对象.其一个典型的API是next方法.该方法获得序列中的下一个值. 迭代器示例 题目:希望编写一个便利的函数,它可以接收任意数量的参数,并为这些 ...