//转自 瓦里奥
一. 二进制转换成图片
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);}