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序列化 - ...
随机推荐
- objective-c(类别)
objective-c中的Categary(类别)使用相当广泛,其内涵类似于javascript中的prototype,可以扩展某一个类的方法. 下面给出一个基本的例子,参考oc程序设计一书: 实现一 ...
- 《CLR.via.C#第三版》第一部分读书笔记(一)
最近开始仔细研读<CLR.via.C#第三版>这本书.读pdf文档确实很累.建议有条件的朋友还是买书看吧. 我的笔记用来记录我对这本书的理解,简化下逻辑,对每个部分我觉得是要点的进行归纳总 ...
- Web前端技术研究:Css hack技术---令人沮丧的技术
我最近想好好整理下csshack技术,但是结果很沮丧,下面我将我最初写的笔记和大家分享下. 我在单位整理的研究笔记: 不同的浏览器对某些CSS代码解析会存在一定的差异,因此就会导致不同浏览器下给用户展 ...
- HBase Java简单示例
Hbase采用Java实现,原生客户端也是Java实现,其他语言需要通过thritf接口服务间接访问Hbase的数据. Hbase作为大数据存储数据库,其写能力非常强,加上Hbase本身就脱胎于Had ...
- [ZigBee] 11、ZigBee之睡眠定时器二
1.前言 上一节讲了Zigbee的睡眠定时器利用外部按键使系统从休眠态唤醒到工作态,其核心在于: 61 void SysPowerMode(uchar mode) 62 { 63 if(mode &g ...
- Android Fragment add/replace以及backstack
无论Fragment以何种方式加入,都不会影响backstack,backstack由addToBackStack函数决定,只有调用了这个函数,才会将Fragment加入返回栈.这个说法其实不太准确, ...
- 翻译-使用Spring调用SOAP Web Service
原文链接: http://spring.io/guides/gs/consuming-web-service/ 调用SOAP web service 本指南将指导你使用Spring调用一个基于SOAP ...
- HTML5触屏版多线程渲染模板技术分享
前言: 了解js编译原理的屌丝们都知道,js是单线程的,想当年各路神仙为了实现js的多线程,为了解决innerHTML输出大段HTML卡页面的顽疾,纷纷设计了诸如假冒的“多线程“实现,我自己也在写开源 ...
- WPF路线图白皮书: 2015及未来
介绍 当2006年微软首次推出Windows Presentation Foundation(WPF)时,它代表了应用程序的设计和开发又向前跨出了巨大的一步.它简化了GUI应用程序的开发,有助于UI和 ...
- Redis中5种数据结构的使用场景介绍
转载于:http://www.itxuexiwang.com/a/shujukujishu/redis/2016/0216/108.html?1455861435 一.redis 数据结构使用场景 原 ...