/*******    *    ***    *****     **        **
* * * * * * * *
***** * * * * * * *
* * * * * * * *
******* *** * ***** */ 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的更多相关文章

  1. c# Bitmap byte[] Stream 文件相互转换

    //byte[] 转图片 publicstatic Bitmap BytesToBitmap(byte[] Bytes) { MemoryStream stream = null; try { str ...

  2. C#图像处理:Stream 与 byte[] 相互转换,byte[]与string,Stream 与 File 相互转换等

    C# Stream 和 byte[] 之间的转换 一. 二进制转换成图片 MemoryStream ms = new MemoryStream(bytes); ms.Position = 0; Ima ...

  3. stream,file,filestream,memorystream简单的整理

    一.Stream 什么是stream?(https://www.cnblogs.com/JimmyZheng/archive/2012/03/17/2402814.html#no8) 定义:提供字节序 ...

  4. Stream、FileStream、MemoryStream的区别

    1.Stream:流,在msdn的定义:提供字节序列的一般性视图,Stream提供了读写流的方法是以字节的形式从流中读取内容.而我们经常会用到从字节流中读取文本或者写入文本,微软提供了StreamRe ...

  5. 【.Net】Byte,Stream,File的转换

    引言      文件的传输和读写通常都离不开Byte,Stream,File这个类,这里我简单封装一下,方便使用. 帮助类     public static class FileHelper { / ...

  6. C#中byte[] 与string相互转化问题

    using System; using System.IO; using System.Security.Cryptography; namespace ShareX.UploadersLib.Oth ...

  7. 关于byte[]与string、Image转换

    byte[]与string转换 参考网址:https://www.cnblogs.com/xskblog/p/6179689.html 1.使用System.Text.Encoding.Default ...

  8. 图片转为byte[]、String、图片之间的转换

    package com.horizon.action; import java.io.ByteArrayOutputStream; import java.io.File; import java.i ...

  9. 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序列化 - ...

随机推荐

  1. PPT嵌入字体的方法

    使用ppt的时候,很多时候会使用一些特殊字体,在其他计算机上无法正常显示.这个时候就需要导出PPT的时候进行字体嵌入. 1.1 常规方法 所谓常规方法,是指那些字体的许可协议允许随意分发,我们才能导出 ...

  2. 用WPF实现查找结果高亮显示

    概述 我们经常会遇到这样的需求:到数据库里查找一些关键字,把带这些关键字的记录返回显示在客户端上.但如果仅仅是单纯地把文本显示出来,那很不直观,用户不能很轻易地看到他们想找的内容,所以通常我们还要做到 ...

  3. silverlight中Combox绑定数据以及动态绑定默认选定项的用法

    在Sliverlight中,经常要用到下拉框Combox,然而Combox的数据绑定却是一件令初学者很头疼的事情.今天就来总结一下下拉框的使用方法: 下面写一个简单的例子吧.先写一个日期的Model, ...

  4. JavaScript正则表达式下——相关方法

    上篇博客JavaScript 正则表达式上——基本语法介绍了JavaScript正则表达式的语法,有了这些基本知识,可以看看正则表达式在JavaScript的应用了,在一切开始之前,看看RegExp实 ...

  5. 关于QCon2015感想与反思

    QCon2015专场有不少关于架构优化.专项领域调优专题,但能系统性描述产品测试方向只有<携程无线App自动化测试实践>.   (一). 携程的无线App自动化     <携程无线A ...

  6. CSS水平垂直居中的几种方法

    直接进入主题! 一.脱离文档流元素的居中 方法一:margin:auto法 CSS代码: div{ width: 400px; height: 400px; position: relative; b ...

  7. 解决URL中文乱码问题

    在做一个HTTPS连接时, 要客户端合成一段HTTPS地址 如果地址含中文的话程序会crash, 检查发现原来是中文没有转码的原因 在NSString库里面找到了下面两个方法 - (NSString ...

  8. Redis笔记,安装和常用命令

    转载于:http://www.itxuexiwang.com/a/shujukujishu/redis/2016/0216/96.html?1455870708 一.redis简单介绍 redis是N ...

  9. atitit.Servlet2.5 Servlet 3.0 新特性 jsp2.0 jsp2.1 jsp2.2新特性

    atitit.Servlet2.5 Servlet 3.0 新特性 jsp2.0 jsp2.1 jsp2.2新特性   1.1. Servlet和JSP规范版本对应关系:1 1.2. Servlet2 ...

  10. Nginx反向代理搭建配置

    1.反向代理方式是指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将服务器上得到的结果返回给internet 上请求连接的客户端,此时代理服务器对外就表现为一个 ...