/*******    *    ***    *****     **        **
* * * * * * * *
***** * * * * * * *
* * * * * * * *
******* *** * ***** */ 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. 解决Win7 软件图标不显示--Win7图标异常,快捷方式不显示解决方法

    电脑症状:WIN7的系统,桌面上的图标显示的不正常,快捷方式显示的是未知程序.看不到程序默认图标,快捷方式图标不显示. 解决方法:删除程序图标缓存即可.   将下面的内容复制到记事本保存为“Repai ...

  2. Android按需添加Google Play服务

    以前无论使用何种Google Play服务,都是直接在gradle文件中引用一个库. compile 'com.google.android.gms:play-services:9.4.0' 这直接导 ...

  3. Web语义化

    在昨天和做SEO的同学聊了一会儿,当然我没有学会搜索引擎优化的技巧和知识,但在此之前一直对HTML5中header.footer.sidebar.article等标签嗤之以鼻,觉得这个和div没有什么 ...

  4. Java-接口练习1

    1.(1)编写一个接口ShapePara,要求: 接口中的方法: int getArea():获得图形的面积.int getCircumference():获得图形的周长 (2)编写一个圆类Circl ...

  5. JS基础知识总结

      js基础知识点总结 如何在一个网站或者一个页面,去书写你的js代码:1.js的分层(功能):jquery(tool) 组件(ui) 应用(app),mvc(backboneJs)2.js的规划() ...

  6. ajax图片上传及FastDFS入门案例.

    今天来开始写图片上传的功能, 现在的图片上传都讲求 上传完成后立刻回显且页面不刷新, 这里到底是怎么做的呢? 当然是借助于ajax了, 但是ajax又不能提交表单, 这里我们还要借助一个插件: jqu ...

  7. Atitit js版本es5 es6新特性

    Atitit js版本es5 es6新特性 Es5( es5 其实就是adobe action script的标准化)1 es6新特性1 Es5( es5 其实就是adobe action scrip ...

  8. SVG 基础

    <!doctype html><html lang="en"><head> <meta charset="UTF-8" ...

  9. vuejs与服务器通信

    vuejs与服务器通信 与服务器通信 Vue 实例的原始数据 $data 能直接用 JSON.stringify() 序列化.社区贡献了一个插件 vue-resource,提供一种容易的方式与 RES ...

  10. linux命令 - export - 设置环境变量

    linux命令 - export - 设置环境变量 功能说明:设置或显示环境变量. 语 法:export [-fnp][变量名称]=[变量设置值] 补充说明:在shell中执行程序时,shell会提供 ...