/// <summary>
/// 将 Stream 转成 byte[]
/// </summary>
/// <param name="stream"></param>
/// <returns></returns>
public static byte[] StreamToBytes(Stream stream)
{
byte[] bytes = new byte[stream.Length];
stream.Read(bytes, , bytes.Length);
// 设置当前流的位置为流的开始
stream.Seek(, SeekOrigin.Begin);
return bytes;
} /// <summary>
/// 将 byte[] 转成 Stream
/// </summary>
/// <param name="bytes"></param>
/// <returns></returns>
public static Stream BytesToStream(byte[] bytes)
{
Stream stream = new MemoryStream(bytes);
return stream;
} /// <summary>
/// 字节流转换成图片
/// </summary>
/// <param name="byt">要转换的字节流</param>
/// <returns>转换得到的Image对象</returns>
public static Image BytToImg(byte[] byt)
{
try
{
MemoryStream ms = new MemoryStream(byt);
Image img = Image.FromStream(ms);
return img;
}
catch (Exception ex)
{
LogHelper.WriteError("StreamHelper.BytToImg 异常", ex);
return null;
}
} /// <summary>
/// 图片转换成字节流
/// </summary>
/// <param name="img"></param>
/// <returns></returns>
public static byte[] ImageToByteArray(Image img)
{
ImageConverter imgconv = new ImageConverter();
byte[] b = (byte[])imgconv.ConvertTo(img, typeof(byte[]));
return b;
} /// <summary>
/// 把图片Url转化成Image对象
/// </summary>
/// <param name="imageUrl"></param>
/// <returns></returns>
public static Image Url2Img(string imageUrl)
{
try
{
if (string.IsNullOrEmpty(imageUrl))
{
return null;
} WebRequest webreq = WebRequest.Create(imageUrl);
WebResponse webres = webreq.GetResponse();
Stream stream = webres.GetResponseStream();
Image image;
image = Image.FromStream(stream);
stream.Close(); return image;
}
catch (Exception ex)
{
LogHelper.WriteError("StreamHelper.Url2Img 异常", ex);
} return null;
} /// <summary>
/// 把本地图片路径转成Image对象
/// </summary>
/// <param name="imagePath"></param>
/// <returns></returns>
public static Image ImagePath2Img(string imagePath)
{
try
{
if (string.IsNullOrEmpty(imagePath))
{
return null;
} byte[] bytes = Image2ByteWithPath(imagePath);
Image image = BytToImg(bytes);
return image;
}
catch (Exception ex)
{
LogHelper.WriteError("StreamHelper.ImagePath2Img 异常", ex);
return null;
}
}

sream bytes[] img相互转换的更多相关文章

  1. 彻底弄懂python编码

    在编写python程序的过程中,中英文混用经常会出现编码问题.围绕此问题,本文首先介绍编码的含义及常用编码,随后列举几个python经常遇到的编码异常及解决方法,接着列举笔者在实践中遇到的异常出现的情 ...

  2. python3中bytes、hex和字符串相互转换

    1.字符串转bytes a = 'abcd' a1 = bytes(a,encoding('utf-8')) 2.bytes转字符串 a = b'abcd' a1 = bytes.decode(a , ...

  3. 内置函数 -- bytes -- 字节码与字符串相互转换

    说明: 1. 返回值为一个新的不可修改字节数组,每个数字元素都必须在0 - 255范围内,是bytearray函数的具有相同的行为,差别仅仅是返回的字节数组不可修改. 2. 当3个参数都不传的时候,返 ...

  4. python之bytes和string相互转换

    来源:https://www.cnblogs.com/skiler/p/6687337.html 1.bytes主要是给计算机看的,string主要是给人看的 2.中间有个桥梁就是编码规则,现在大趋势 ...

  5. [转] python关于ctypes使用char指针与bytes相互转换的问题

    最近研究人脸识别,需要用python调用so动态库,涉及到c/c++中的指针字符串转Python的bytes对象的问题. 按照ctypes的文档,直观方式是先创建对应的类型数组,再将指针取地址一一赋值 ...

  6. Java File 与 Bytes相互转换

    public static byte[] fileToBytes(String filePath) { byte[] buffer = null; File file = new File(fileP ...

  7. IRandomAccessStream, IBuffer, Stream, byte[] 之间相互转换

    /* * 用于实现 IRandomAccessStream, IBuffer, Stream, byte[] 之间相互转换的帮助类 */ using System;using System.IO;us ...

  8. (一)一个工作任务引起的乱战——c#中结构体与byte[]间相互转换

    一个工作任务涉及到c#与c++系统间的udp通信,处理了蛮长时间没有完成任务,但是期间接触到不少小知识点.本人是初接触c#,c++语言没有接触过.可能写的东西都很小儿科,暂且记录下来当工作日记把. 先 ...

  9. 【JAVA编码】 JAVA字符编码系列二:Unicode,ISO-8859,GBK,UTF-8编码及相互转换

    http://blog.csdn.net/qinysong/article/details/1179489 这两天抽时间又总结/整理了一下各种编码的实际编码方式,和在Java应用中的使用情况,在这里记 ...

随机推荐

  1. SQL注入式攻击

    百度百科:http://baike.baidu.com/link?url=GQbJ2amTzTahZA7XJSBDLYYkN3waQ9JCoJ0l--tCWlvKQibe0YaH4hpmgEnLyn0 ...

  2. sed实例一则

    1.背景: test.txt文件里有这些语句 li^E1026^D20150802B07QH800^B698.^C20150801B08CDP00^B514.^C20150803D00A8L00^B2 ...

  3. ios中怎么获得当前版本号

    NSString *version = [NSBundle mainBundle].infoDictionary[(__bridge NSString *)kCFBundleVersionKey];

  4. APP_Store - 怎样为iOS8应用制作预览视频

    关于iOS 8应用预览视频的话题,从设计.技术规范,到录屏.编辑工具,介绍的都比较详尽:建议收藏,在接下来用的到的时候作以参考.下面进入译文. 最近一两个月里,苹果的世界里出现了很多新东西,比如屏幕更 ...

  5. [转]STUN和TURN技术浅析

    [转]STUN和TURN技术浅析 http://www.h3c.com.cn/MiniSite/Technology_Circle/Net_Reptile/The_Five/Home/Catalog/ ...

  6. [转]UDP/TCP穿越NAT的P2P通信方法研究(UDP/TCP打洞 Hole Punching)

     [转]UDP/TCP穿越NAT的P2P通信方法研究(UDP/TCP打洞 Hole Punching) http://www.360doc.com/content/12/0428/17/6187784 ...

  7. c/c++常用代码---doc,ppt,xls文件格式转PDF格式[转]

    [转]doc,ppt,xls文件格式转PDF格式 http://blog.csdn.net/lee353086/article/details/7920355 确实好用. 需要注意的是#import文 ...

  8. PID

    http://blog.gkong.com/liaochangchu_117560.ashx

  9. 【转载】set_input_delay和set_output_delay的选项-max和-min的讨论

    转自:http://www.cnblogs.com/freshair_cnblog/archive/2012/09/12/2681060.html 一.存在背景分析 文档的说法是,set_input_ ...

  10. spring注解注入失败一个原因

    所有的注解看起来都没有任何问题,最后是由于web-xml配置问题. 由于缺少监听器org.springframework.web.context.ContextLoaderListener, 导致无法 ...