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序列化 - ...
随机推荐
- Flash 与 php 使用 amfphp
创建 Flash 项目 使用 Flash Builder 创建一个项目. 创建 Flash 项目时,选择服务器技术为 PHP,并配置好服务器的 Web 根文件夹及根 URL 地址(这里设置根文件夹时, ...
- onCreateView中加载大位图
我的一个Fragment中,加载了一个1024*1024的图片,非常卡.解决办法 1. 将图片改为512*512 2. 异步加载. final SmartImageView mizige = (Sma ...
- as3 Loader 加载资源后内存泄露无法释放的问题。
本人用Loader加载外部一个swf.之后unloadAndStop,Flash概要分析发现,内存没有被释放. 网上搜了一大堆文章,要么就是加载bitmapdata之后,自己dispose,要么就是加 ...
- ejs模板中的四种表达式输出形式
在ejs模板中,通常会用下面四种方式在HTML中输出服务端的变量或表达式的值: 1. 直接在<%%>中写表达式或变量.这种情况通常只是用来进行表达式计算或给变量赋值,不会有任何输出,被称作 ...
- cmd 下通过NTML代理访问Maven 库
公司用web代理,NTLM验证的,这样在普通CMD下无法使用mvn命令访问网上的maven库,使用CNTLM工具解决. 下载CNTLM工具,安装,修改安装路径下的cntlm.ini,改成实际的ntlm ...
- Java学习之路:不走弯路,就是捷径
1.如何学习程序设计? JAVA是一种平台,也是一种程序设计语言,如何学好程序设计不仅仅适用于JAVA,对C++等其他程序设计语言也一样管用.有编程高手认为,JAVA也好C也好没什么分别,拿来就用.为 ...
- Mongodb 的基本使用
一.cmd连接mongodb 服务 进入mongodb的bin目录下:[D:\mongodb3.2.5\bin]$ mongo 127.0.0.1:27017 常用查询: show dbs 查看所有数 ...
- angularjs可交互的directive
angularjs可交互的directive http://jsfiddle.net/revolunet/s4gm6/ directive开发上手练手,以注释的方式说明 html <body n ...
- APP性能测试
方法一: 本地安装安卓模拟器,用LR选择模拟器录制方式录制 方法二: 手机真机需要root,可以在电脑上下载一键root工具(如卓大师),然后手机和电脑用数据线连接,然后root. 在手机上运行 Mo ...
- ThinkPHP5 隐藏接口里面的index.php
隐藏index.php 官方介绍是这样的:http://www.kancloud.cn/thinkphp/thinkphp5_quickstart/145250 可以去掉URL地址里面的入口文件ind ...