Stream,byte[],LZMA
//转自 瓦里奥
一. 二进制转换成图片
1
2
3
4
5
|
MemoryStream ms = new MemoryStream(bytes);
ms.Position = 0;
Image img = Image.FromStream(ms);
ms.Close();
this .pictureBox1.Image
|
二. C#中byte[]与string的转换代码
1.
1
2
3
|
System.Text.UnicodeEncoding converter = new System.Text.UnicodeEncoding();
byte [] inputBytes =converter.GetBytes(inputString);
string inputString = converter.GetString(inputBytes);
|
2.
1
2
3
|
string inputString = System.Convert.ToBase64String(inputBytes);
byte [] inputBytes = System.Convert.FromBase64String(inputString);
FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
|
三. C# Stream 和 byte[] 之间的转换
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
/// 将 Stream 转成 byte[]
public byte [] StreamToBytes(Stream stream)
{
byte [] bytes = new byte [stream.Length];
stream.Read(bytes, 0, bytes.Length);
// 设置当前流的位置为流的开始
stream.Seek(0, SeekOrigin.Begin);
return bytes;
}
/// 将 byte[] 转成 Stream
public Stream BytesToStream( byte [] bytes)
{
Stream stream = new MemoryStream(bytes);
return stream;
}
|
四. Stream 和 文件之间的转换
将 Stream 写入文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
public void StreamToFile(Stream stream, string fileName)
{
// 把 Stream 转换成 byte[]
byte [] bytes = new byte [stream.Length];
stream.Read(bytes, 0, bytes.Length);
// 设置当前流的位置为流的开始
stream.Seek(0, SeekOrigin.Begin);
// 把 byte[] 写入文件
FileStream fs = new FileStream(fileName, FileMode.Create);
BinaryWriter bw = new BinaryWriter(fs);
bw.Write(bytes);
bw.Close();
fs.Close();
}
|
五. 从文件读取 Stream
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
public 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, 0, bytes.Length);
fileStream.Close();
// 把 byte[] 转换成 Stream
Stream stream = new MemoryStream(bytes);
return stream;
}
|
六
1
2
3
4
5
6
|
//Bitmap 转化为 Byte[]
Bitmap BitReturn = new Bitmap();
byte [] bReturn = null ;
MemoryStream ms = new MemoryStream();
BitReturn.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
bReturn = ms.GetBuffer();
|
//from stackoverflow
private static void CompressFileLZMA(string inFile, string outFile) { SevenZip.Compression.LZMA.Encoder coder = new SevenZip.Compression.LZMA.Encoder(); FileStream input = new FileStream(inFile, FileMode.Open); FileStream output = new FileStream(outFile, FileMode.Create); // Write the encoder properties coder.WriteCoderProperties(output); // Write the decompressed file size. output.Write(BitConverter.GetBytes(input.Length), 0, 8); // Encode the file. coder.Code(input, output, input.Length, -1, null); output.Flush(); output.Close(); } private static void DecompressFileLZMA(string inFile, string outFile) { SevenZip.Compression.LZMA.Decoder coder = new SevenZip.Compression.LZMA.Decoder(); FileStream input = new FileStream(inFile, FileMode.Open); FileStream output = new FileStream(outFile, FileMode.Create); // Read the decoder properties byte[] properties = new byte[5]; input.Read(properties, 0, 5); // Read in the decompress file size. byte [] fileLengthBytes = new byte[8]; input.Read(fileLengthBytes, 0, 8); long fileLength = BitConverter.ToInt64(fileLengthBytes, 0); coder.SetDecoderProperties(properties); coder.Code(input, output, input.Length, fileLength, null); output.Flush(); output.Close(); }
public static void Decompress(Stream inStream, Stream outStream){ byte[] properties = new byte[5]; inStream.Read(properties, 0, 5); SevenZip.Compression.LZMA.Decoder decoder = new SevenZip.Compression.LZMA.Decoder(); decoder.SetDecoderProperties(properties); long outSize = 0; for (int i = 0; i < 8; i++) { int v = inStream.ReadByte(); outSize |= ((long)(byte)v) << (8 * i); } long compressedSize = inStream.Length - inStream.Position; decoder.Code(inStream, outStream, compressedSize, outSize, null);}public static string DecompressLzma(string inputstring){ if (!string.IsNullOrEmpty(inputstring)) { byte[] myInts = Array.ConvertAll(inputstring.Split(','), s => (byte)int.Parse(s)); var stream = new MemoryStream(myInts); var outputStream = new MemoryStream(); Decompress(stream, outputStream); using (var reader = new StreamReader(outputStream)) { outputStream.Position = 0; string output = reader.ReadToEnd(); return output; } } return "";}
public static string CompressLzma(string inputstring){ if (!string.IsNullOrEmpty(inputstring)) { var stream = new MemoryStream(Encoding.Unicode.GetBytes(inputstring ?? "")); var outputStream = new MemoryStream(); Compress(stream, outputStream); byte[] bytes = outputStream.ToArray(); } return "";}public static void Compress(MemoryStream inStream, MemoryStream outStream){ CoderPropID[] propIDs; object[] properties; PrepareEncoder(out propIDs, out properties); SevenZip.Compression.LZMA.Encoder encoder = new SevenZip.Compression.LZMA.Encoder(); encoder.SetCoderProperties(propIDs, properties); encoder.WriteCoderProperties(outStream); Int64 fileSize = inStream.Length; for (int i = 0; i < 8; i++) { outStream.WriteByte((Byte)(fileSize >> (8 * i))); } encoder.Code(inStream, outStream, -1, -1, null);}public static void PrepareEncoder(out CoderPropID[] propIDs, out object[] properties){ bool eos = true; Int32 dictionary = 1 << 16; Int32 posStateBits = 2; Int32 litContextBits = 3; // for normal files // UInt32 litContextBits = 0; // for 32-bit data Int32 litPosBits = 0; // UInt32 litPosBits = 2; // for 32-bit data Int32 algorithm = 2; Int32 numFastBytes = 32; string mf = "bt2"; propIDs = new CoderPropID[] { CoderPropID.DictionarySize, CoderPropID.PosStateBits, CoderPropID.LitContextBits, CoderPropID.LitPosBits, CoderPropID.Algorithm, CoderPropID.NumFastBytes, CoderPropID.MatchFinder, CoderPropID.EndMarker }; properties = new object[] { dictionary, posStateBits, litContextBits, litPosBits, algorithm, numFastBytes, mf, eos };}
public static string CompressLzma(string inputstring){ if (!string.IsNullOrEmpty(inputstring)) { var stream = new MemoryStream(Encoding.UTF8.GetBytes(inputstring ?? "")); var outputStream = new MemoryStream(); Compress(stream, outputStream); byte[] bytes = outputStream.ToArray(); var result = string.Join(",", Array.ConvertAll(bytes, v => signedInt((int)v))); return result; } return "";}public static void PrepareEncoder(out CoderPropID[] propIDs, out object[] properties){ bool eos = true; Int32 dictionary = 1 << 16; Int32 posStateBits = 2; Int32 litContextBits = 3; // for normal files // UInt32 litContextBits = 0; // for 32-bit data Int32 litPosBits = 0; // UInt32 litPosBits = 2; // for 32-bit data Int32 algorithm = 2; Int32 numFastBytes = 64; string mf = "bt4"; propIDs = new CoderPropID[] { CoderPropID.DictionarySize, CoderPropID.PosStateBits, CoderPropID.LitContextBits, CoderPropID.LitPosBits, CoderPropID.Algorithm, CoderPropID.NumFastBytes, CoderPropID.MatchFinder, CoderPropID.EndMarker }; properties = new object[] { dictionary, posStateBits, litContextBits, litPosBits, algorithm, numFastBytes, mf, eos };}private static int signedInt(int unsignedInt){ return unsignedInt >= 128 ? Math.Abs(128 - unsignedInt) - 128 : unsignedInt;}public static void Compress(MemoryStream inStream, MemoryStream outStream){ CoderPropID[] propIDs; object[] properties; PrepareEncoder(out propIDs, out properties); SevenZip.Compression.LZMA.Encoder encoder = new SevenZip.Compression.LZMA.Encoder(); encoder.SetCoderProperties(propIDs, properties); encoder.WriteCoderProperties(outStream); Int64 fileSize = inStream.Length; for (int i = 0; i < 8; i++) { outStream.WriteByte((Byte)(fileSize >> (8 * i))); } encoder.Code(inStream, outStream, -1, -1, null);}
Stream,byte[],LZMA相关推荐
- 【C#】数据类型(sbyte,byte,short,ushort,int,uint,long,ulong和char。、、、)...
C#的数据类型可以分为3类:数值类型,引用类型,指针类型.指针类型仅在不安全代码中使用. 值类型包括简单类型(如字符型,浮点型和整数型等),集合类型和结构型.引用类型包括类类型,接口类型,代表类型和数 ...
- 【C#】数据类型(sbyte,byte,short,ushort,int,uint,long,ulong和char。、、、)
C#的数据类型可以分为3类:数值类型,引用类型,指针类型.指针类型仅在不安全代码中使用. 值类型包括简单类型(如字符型,浮点型和整数型等),集合类型和结构型.引用类型包括类类型,接口类型,代表类型和数 ...
- iOS -- 十进制、十六进制字符串,byte,data等之间的转换
十进制->十六进制 Byte bytes[]={0xA6,0x27,0x0A}; NSString *strIdL = [NSStringstringWithFormat:@"%@&q ...
- C 数据类型(bit,byte,word;char,int,long;float,double)
文章目录 1.位.字节和字 2.整数类型 3.浮点类型 C语言提供 两大系列的 多种数据类型. 两大数据类型是 整数类型和 浮点数类型. 1.位.字节和字 位.字节和字是描述计算机数据单元或存储单元的 ...
- 计算器中,Byte,Word, Dword, Qword
HEX :十六进制 Hexadecimal DEC :十进制 Decimal OCT :八进制 Octal BIN :二进制 Binary 1.MC:清除存储器中的数值. Memory Clear 2 ...
- java 16进制字符串转base64_16进制字符串,byte[] ,base64三者之间的转换
packagecom.lgdz.qydevice.utils; importcn.hutool.core.codec.Base64; importcn.hutool.core.util.Charset ...
- java异或什么意思_java基础知识,数据类型,运算符(003)
一.java注释. 单行注释与取消快捷键:shift+ctrl+c或则ctrl+/. 多行注释:shift+ctrl+/,多行注释取消shift+ctrl+. 文档注释: 以"/**&quo ...
- 【Java10】lambda表达式(函数式编程),Stream流,File类,字节/字符流,乱码,缓冲/转换/序列化/打印流,Properties
文章目录 1.lambda表达式标准语法:()->{} 2.lambda表达式简略语法:可推导即可省略 3.lambda表达式原理:lambda效率比匿名内部类高 4.两个函数式接口:Consu ...
- c#实现linux中gzip压缩解压缩算法:byte[]字节数组,文件,字符串,数据流的压缩解压缩
全栈工程师开发手册 (作者:栾鹏) c#教程全解 c#实现gzip压缩解压缩byte[]字节数组,文件,字符串. 测试代码 static void Main() {//测试字符串String inpu ...
- UWPWP8.1 重新绘制图片 WriteableBitmap用法 图片转byte[]数组,byte[]数组转图片
---恢复内容开始--- WriteableBitmap 是UWP和WP8.1绘制图片的,重组图片的最重要方法.方法较为简单,方法多样性. 通过查看文档,WriteableBitmap的继承性是 ...
最新文章
- 发展是硬道理 高可用从双机热备走向容灾复制集群
- (资源)OpenStack IRC资源
- go函数详解:函数定义、形参、返回值定义规范、函数内存分析、不支持重载、支持可变参数、基本数据类型和数组默认都是值传递的、支持自定义数据类型、函数返回值命名
- 【渝粤教育】国家开放大学2018年秋季 1133t文献检索 参考试题
- Unity 3D中 Ulua-UGUI简单的Demo——热更新的具体流程、使用说明
- 音创点歌机_音创点歌系统_音创KTV点歌系统下载- 下载之家
- 信号与系统基础简单知识概括
- 解决禁用笔记本键盘,外接使用键盘方法
- Python中in和has_key的性能比较
- 关于移动端的touch事件(touchstart, touchmove, touchend,touchcancel)
- Wireshark:抓取微信网址
- Python图像识别-Opencv07 异或运算,图像加密
- LLVM-clang
- dz plugin.php,解决dz论坛提示“插件不存在或已关闭”
- Drools规则引擎之常用语法
- camel标记法是什么方法
- 彻底禁用Chrome的“请停用以开发者模式运行的扩展程序”提示
- python语言语块句的标记_个人学习记录
- 水果做键盘控制跑跑卡丁车
- 高德地图自定义图标的点标记Marker--初体验(二)