BufferHelp byte[] Stream string FileStream Image Bitmap
/******* * *** ***** ** **
* * * * * * * *
***** * * * * * * *
* * * * * * * *
******* *** * ***** */ using System.Drawing;
using System.IO;
using System.Text; namespace Endv
{
//BufferHelp
public static class Buffer
{
/// <summary>
/// 拼接 byte
/// </summary>
/// <param name="buffer1"></param>
/// <param name="buffer2"></param>
/// <returns></returns>
public static byte[] Joint(byte[] buffer1, byte[] buffer2)
{
byte[] bufferWhole = new byte[buffer1.Length + buffer2.Length];
System.Buffer.BlockCopy(buffer1, , bufferWhole, , buffer1.Length);
System.Buffer.BlockCopy(buffer2, , bufferWhole, buffer1.Length, buffer2.Length);
return bufferWhole;
} public static byte[] Joint(byte[] buffer1, byte[] buffer2, byte[] buffer3)
{
return Joint(Joint(buffer1, buffer2), buffer3);
} public static byte[] Joint(byte[] buffer1, byte[] buffer2, byte[] buffer3, byte[] buffer4)
{
return Joint(Joint(buffer1, buffer2, buffer3), buffer4);
} public static byte[] Joint(byte[] buffer1, byte[] buffer2, byte[] buffer3, byte[] buffer4, byte[] buffer5, byte[] buffer6)
{
return Joint(Joint(buffer1, buffer2, buffer3, buffer4), buffer5, buffer6);
} // .... // 将 Stream 转成 byte[]
public static byte[] StreamToBytes(Stream stream)
{
byte[] bytes = new byte[stream.Length];
stream.Read(bytes, , bytes.Length);
// 设置当前流的位置为流的开始
stream.Seek(, SeekOrigin.Begin);
return bytes;
} // 将 byte[] 转成 Stream
public static Stream BytesToStream(byte[] bytes)
{
Stream stream = new MemoryStream(bytes);
return stream;
} // 将 byte[] 转成 string
public static string BytesToString(byte[] bytes)
{
UnicodeEncoding converter = new UnicodeEncoding();
string s = converter.GetString(bytes);
return s;
} // string 转成 byte[]
public static byte[] StringToBytes(string s)
{
UnicodeEncoding converter = new UnicodeEncoding();
byte[] bytes = converter.GetBytes(s);
return bytes;
} //Stream 和 文件之间的转换
//将 Stream 写入文件
public static void StreamToFile(Stream stream, string fileName)
{
// 把 Stream 转换成 byte[]
byte[] bytes = new byte[stream.Length];
stream.Read(bytes, , bytes.Length);
// 设置当前流的位置为流的开始
stream.Seek(, SeekOrigin.Begin);
// 把 byte[] 写入文件
FileStream fs = new FileStream(fileName, FileMode.Create);
BinaryWriter bw = new BinaryWriter(fs);
bw.Write(bytes);
bw.Close();
fs.Close();
}
//从文件读取 Stream
public static Stream FileToStream(string fileName)
{
// 打开文件
FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
// 读取文件的 byte[]
byte[] bytes = new byte[fileStream.Length];
fileStream.Read(bytes, , bytes.Length);
fileStream.Close();
// 把 byte[] 转换成 Stream
Stream stream = new MemoryStream(bytes);
return stream;
} //二进制转换成图片
public static Image bytesToImage(byte[] bytes)
{
MemoryStream ms = new MemoryStream(bytes);
Image img = Image.FromStream(ms);
return img;
} //图片 转换成 byte
public static byte[] ImageTobytes(Image img)
{
Bitmap BitReturn = new Bitmap(img);
byte[] bytes = null;
MemoryStream ms = new MemoryStream();
BitReturn.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
bytes = ms.GetBuffer();
return bytes;
} // .... } }
BufferHelp byte[] Stream string FileStream Image Bitmap的更多相关文章
- c# Bitmap byte[] Stream 文件相互转换
//byte[] 转图片 publicstatic Bitmap BytesToBitmap(byte[] Bytes) { MemoryStream stream = null; try { str ...
- C#图像处理:Stream 与 byte[] 相互转换,byte[]与string,Stream 与 File 相互转换等
C# Stream 和 byte[] 之间的转换 一. 二进制转换成图片 MemoryStream ms = new MemoryStream(bytes); ms.Position = 0; Ima ...
- stream,file,filestream,memorystream简单的整理
一.Stream 什么是stream?(https://www.cnblogs.com/JimmyZheng/archive/2012/03/17/2402814.html#no8) 定义:提供字节序 ...
- Stream、FileStream、MemoryStream的区别
1.Stream:流,在msdn的定义:提供字节序列的一般性视图,Stream提供了读写流的方法是以字节的形式从流中读取内容.而我们经常会用到从字节流中读取文本或者写入文本,微软提供了StreamRe ...
- 【.Net】Byte,Stream,File的转换
引言 文件的传输和读写通常都离不开Byte,Stream,File这个类,这里我简单封装一下,方便使用. 帮助类 public static class FileHelper { / ...
- C#中byte[] 与string相互转化问题
using System; using System.IO; using System.Security.Cryptography; namespace ShareX.UploadersLib.Oth ...
- 关于byte[]与string、Image转换
byte[]与string转换 参考网址:https://www.cnblogs.com/xskblog/p/6179689.html 1.使用System.Text.Encoding.Default ...
- 图片转为byte[]、String、图片之间的转换
package com.horizon.action; import java.io.ByteArrayOutputStream; import java.io.File; import java.i ...
- Serializable 序列化 The byte stream created is platform independent. So, the object serialized on one platform can be deserialized on a different platform.
Java 序列化接口Serializable详解 - 火星猿类 - 博客园 http://www.cnblogs.com/tomtiantao/p/6866083.html 深入理解JAVA序列化 - ...
随机推荐
- js操作Dom的一些方法简化
众所周知JQ的选择符很强大,一些看起来很难实现的功能只要在$符号中传入简单的字符串就可以获取到各种层级关系的DOM,而却不用考虑浏览器的兼容性.但有时候在做小项目的时候并不需要引入JQ,而又不想频繁繁 ...
- 关于MySQL的在线扩容
原文地址:http://bucketli.iteye.com/blog/1294032 主要简单总结下,mysql在线扩容和缩容一般涉及到的内容,主要包括三个方面,1.在线也就意味着需要把增量的数据重 ...
- 手机淘宝UWP
各位园主好! bug 走势: 哪天bug 足够少,哪天就可以发布了 :) 2015/10/23: 49 2015/10/26: 40 2015/10/27: 36 2015/10/28: 30 20 ...
- dojo/aspect源码解析
dojo/aspect模块是dojo框架中对于AOP的实现.关于AOP的详细解释请读者另行查看其它资料,这里简单复习一下AOP中的基本概念: 切面(Aspect):其实就是共有功能的实现.如日志切面. ...
- Java提高篇(二六)-----hashCode
在前面三篇博文中LZ讲解了(HashMap.HashSet.HashTable),在其中LZ不断地讲解他们的put和get方法,在这两个方法中计算key的hashCode应该是最重要也是最 ...
- 《OOC》笔记(1)——C语言const、static和extern的用法
<OOC>笔记(1)——C语言const.static和extern的用法 C语言中const关键字用法不少,我只喜欢两种用法.一是用于修饰函数形参,二是用于修饰全局变量和局部变量. 用c ...
- JavaBean和Map转换封装类
package com.ljq.util; import java.beans.BeanInfo; import java.beans.Introspector; import java.beans. ...
- 使用Vue.js时,对Chrome控制台的一点小心得
之前对Chrome控制台的console.log()输出没太放心上,其实仔细研究后,对工作效率有显著的提示.看下面的五段代码: console.log(''); console.log(typeof ...
- DDD~领域事件应用篇(订单处理变得更清晰)
回到目录 上一讲主要说了领域事件和领域总线,这并不是一个很容易理解的文章,所以本讲实例篇主要是为了补充上一讲的理论知识,本讲实例关注的是实际中的订单处理模块,我们知道,订单处理是电子商务的核心,往往在 ...
- 转载:Spring AOP (下)
昨天记录了Spring AOP学习的一部分(http://www.cnblogs.com/yanbincn/archive/2012/08/13/2635413.html),本来是想一口气梳理完的.但 ...