本文整理汇总了C#中System.IO.MemoryStream.Read方法的典型用法代码示例。如果您正苦于以下问题:C# MemoryStream.Read方法的具体用法?C# MemoryStream.Read怎么用?C# MemoryStream.Read使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.IO.MemoryStream
的用法示例。
在下文中一共展示了MemoryStream.Read方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ImageToBytes
/// <summary>
/// Convert Image to Byte[]
/// Image img = Image.FromFile("c:\\hcha.jpg");
/// </summary>
/// <param name="image"></param>
/// <returns></returns>
public static byte[] ImageToBytes(Image image)
{
ImageFormat format = image.RawFormat;
using (MemoryStream ms = new MemoryStream())
{
if (format.Equals(ImageFormat.Jpeg))
{
image.Save(ms, ImageFormat.Jpeg);
}
else if (format.Equals(ImageFormat.Png))
{
image.Save(ms, ImageFormat.Png);
}
else if (format.Equals(ImageFormat.Bmp))
{
image.Save(ms, ImageFormat.Bmp);
}
else if (format.Equals(ImageFormat.Gif))
{
image.Save(ms, ImageFormat.Gif);
}
else if (format.Equals(ImageFormat.Icon))
{
image.Save(ms, ImageFormat.Icon);
}
byte[] buffer = new byte[ms.Length];
//Image.Save()会改变MemoryStream的Position,需要重新Seek到Begin
ms.Seek(0, SeekOrigin.Begin);
ms.Read(buffer, 0, buffer.Length);
return buffer;
}
}
示例2: DescriptionMessage
public DescriptionMessage(byte[] bytes)
{
using (MemoryStream ms = new MemoryStream(bytes, false))
{
Name = TextHelper.DeStreamString(ms);
Location = TextHelper.DeStreamString(ms);
Description = TextHelper.DeStreamString(ms);
// Sensors Length
byte[] lengthBytes = new byte[sizeof(Int32)];
ms.Read(lengthBytes, 0, sizeof(Int32));
int length = BitConverter.ToInt32(lengthBytes, 0);
byte[] sensorsBytes = new byte[length];
ms.Read(sensorsBytes, 0, length);
Sensors = MessageHelper.DeserializeSensorDescriptionsData(sensorsBytes);
// Length
ms.Read(lengthBytes, 0, sizeof(Int32));
length = BitConverter.ToInt32(lengthBytes, 0);
PictureBytes = new byte[length];
ms.Read(PictureBytes, 0, length);
}
}
示例3: GetUncompressedNdfbinary
public byte[] GetUncompressedNdfbinary(byte[] data)
{
using (var ms = new MemoryStream(data))
{
var header = ReadHeader(ms);
if (header.IsCompressedBody)
{
using (var uncompStream = new MemoryStream())
{
ms.Seek(0, SeekOrigin.Begin);
var headBuffer = new byte[header.HeaderSize];
ms.Read(headBuffer, 0, headBuffer.Length);
uncompStream.Write(headBuffer, 0, headBuffer.Length);
ms.Seek((long)header.HeaderSize, SeekOrigin.Begin);
var buffer = new byte[4];
ms.Read(buffer, 0, buffer.Length);
uint compressedblocklen = BitConverter.ToUInt32(buffer, 0);
var contentBuffer = new byte[(ulong)(data.Length) - header.HeaderSize];
ms.Read(contentBuffer, 0, contentBuffer.Length);
//Compressor.Decomp(contentBuffer, uncompStream);
var da = Compressor.Decomp(contentBuffer);
uncompStream.Write(da, 0, da.Length);
data = uncompStream.ToArray();
}
}
}
return data;
}
示例4: ConvertFrom
public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
{
if (value is byte[])
{
var components = new List<CassandraType>();
using (var bytes = new MemoryStream((byte[])value))
{
byte[] byteCount = new byte[2];
while (true)
{
if (bytes.Read(byteCount, 0, 2) == 0)
break;
ushort count = BitConverter.ToUInt16(byteCount, 0);
byte[] buffer = new byte[count];
bytes.Read(buffer, 0, count);
components.Add((BytesType)buffer);
if (bytes.ReadByte() != 0)
break;
}
}
}
if (value is List<CassandraType>)
return (List<CassandraType>)value;
return null;
}
示例5: MimeEntry
/// <summary>
/// Default constructor.
/// </summary>
/// <param name="mimeEntry"></param>
/// <param name="mime"></param>
public MimeEntry(byte[] mimeEntry,MimeParser mime)
{
MemoryStream entryStrm = new MemoryStream(mimeEntry);
m_Headers = MimeParser.ParseHeaders(entryStrm);
m_ContentType = mime.ParseContentType(m_Headers);
m_Entries = new ArrayList();
// != multipart content
if(m_ContentType.ToLower().IndexOf("multipart") == -1){
m_CharSet = ParseCharSet(m_Headers);
m_ContentEncoding = ParseEncoding(m_Headers);
m_Data = new byte[entryStrm.Length - entryStrm.Position];
entryStrm.Read(m_Data,0,m_Data.Length);
}
// multipart content, get nested entries
else{
long s = (int)entryStrm.Position;
string boundaryID = MimeParser.ParseHeaderFiledSubField("Content-Type:","boundary",m_Headers);
m_Entries = mime.ParseEntries(entryStrm,(int)entryStrm.Position,boundaryID);
entryStrm.Position = s;
m_Data = new byte[entryStrm.Length - s];
entryStrm.Read(m_Data,0,m_Data.Length);
}
}
示例6: Main
static void Main(string[] args)
{
//CopySelfToDes();
MemoryStream ms = new MemoryStream();
ms.Seek(0, SeekOrigin.End);
byte[] bts1 = Encoding.Default.GetBytes("情人");
ms.Write(bts1, 0, bts1.Length);
byte[] bts2 = Encoding.Default.GetBytes("我是你的");
ms.Write(bts2, 0, bts2.Length);
int totalLen = bts1.Length + bts2.Length;
byte[] bts = new byte[totalLen - 2];
//ms.Seek(0, SeekOrigin.Begin);
ms.Seek( -4, SeekOrigin.End);//倒着读
int len = ms.Read(bts, 0,4);//你的
ms.Seek(0, SeekOrigin.End);//移到最后 追加
byte[] bts3 = Encoding.Default.GetBytes("淡淡");
ms.Write(bts3, 0, 4);
ms.Seek(0, SeekOrigin.Begin);
byte[] btsRead = new byte[totalLen+ bts3.Length];
ms.Read(btsRead, 0, totalLen + bts3.Length);
ms.Close();
string s = Encoding.Default.GetString(btsRead);
Console.WriteLine(s);
Console.Read();
}
示例7: TLLongSerialization
public void TLLongSerialization()
{
var buffer1 = BitConverter.GetBytes(25565L);
var buffer2 = BitConverter.GetBytes(99999L);
var pos = 0;
CollectionAssert.AreEquivalent(buffer1, new TLLong(buffer1, ref pos).ToBytes());
pos = 0;
CollectionAssert.AreEquivalent(buffer2, new TLLong(buffer2, ref pos).ToBytes());
using (var stream = new MemoryStream())
{
new TLLong(25565L).ToStream(stream);
new TLLong(99999L).ToStream(stream);
stream.Position = 0;
var actualBuffer1 = new byte[8];
var actualBuffer2 = new byte[8];
stream.Read(actualBuffer1, 0, 8);
stream.Read(actualBuffer2, 0, 8);
CollectionAssert.AreEquivalent(buffer1, actualBuffer1);
CollectionAssert.AreEquivalent(buffer2, actualBuffer2);
}
}
示例8: GetFileParts
private static Queue<byte[]> GetFileParts(byte[] file)
{
var fileParts = new Queue<byte[]>();
const int maxFilePart = 512 * 1024;
using (var stream = new MemoryStream(file))
{
while (stream.Position != stream.Length)
{
if ((stream.Length - stream.Position) > maxFilePart)
{
var temp = new byte[maxFilePart];
stream.Read(temp, 0, maxFilePart);
fileParts.Enqueue(temp);
}
else
{
var length = stream.Length - stream.Position;
var temp = new byte[length];
stream.Read(temp, 0, (int)(length));
fileParts.Enqueue(temp);
}
}
}
return fileParts;
}
示例9: Encode
public MemoryStream Encode(MemoryStream memoryStream)
{
memoryStream.Position = 0;
byte[] buffer320 = new byte[320];
byte[] buffer32 = new byte[32];
MemoryStream raw2armData = new MemoryStream();
raw2armData.Write(Encoding.UTF8.GetBytes("#!AMR\n"), 0, 6);
AMR_Encoder enc = new AMR_Encoder();
int start = 0;
int end = Convert.ToInt32(memoryStream.Length);
memoryStream.Position = start;
while (true)
{
if (end - start < 320)
{
memoryStream.Read(buffer320, 0, end - start);
for (int i = end - start; i < 320; i++)
{
buffer320[i] = 0;
}
}
else
{
memoryStream.Read(buffer320, 0, 320);
}
enc.Encode(buffer320, 0, buffer32, 0);
raw2armData.Write(buffer32, 0, buffer32.Length);
start += 320;
if (start >= end) break;
}
return raw2armData;
}
示例10: UploadFile
public override bool UploadFile()
{
FtpWebRequest reqFTP;
reqFTP = (FtpWebRequest)FtpWebRequest.Create(this.UrlString);
reqFTP.KeepAlive = true;
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
reqFTP.UseBinary = true;
reqFTP.ContentLength = this.FileData.Length;
int buffLength = 2048;
byte[] buff = new byte[buffLength];
MemoryStream ms = new MemoryStream(this.FileData);
try
{
int contenctLength;
using (Stream strm = reqFTP.GetRequestStream())
{
contenctLength = ms.Read(buff, 0, buffLength);
while (contenctLength > 0)
{
strm.Write(buff, 0, contenctLength);
contenctLength = ms.Read(buff, 0, buffLength);
}
}
return true;
}
catch (Exception ex)
{
throw new Exception("上传失败", ex.InnerException);
}
}
示例11: ReadWAVFile
/// <summary>
/// Extract WAV data from Memory Stream (assumes mono, 16-bit signed PCM for now)
/// </summary>
/// <param name="stream">MemoryStream</param>
/// <returns>Short array with all data</returns>
public static short[] ReadWAVFile(MemoryStream stream)
{
if (!stream.CanSeek) throw new Exception("Can't seek stream to read wav header");
stream.Seek(40, SeekOrigin.Begin);
Byte[] lengthData = new Byte[4];
int numBytesRead = stream.Read(lengthData, 0, 4);
if (numBytesRead != 4) throw new Exception("Stream ended while reading length value");
int lengthBytes = BitConverter.ToInt32(lengthData, 0);
System.Diagnostics.Debug.WriteLine("Bytes read: " + lengthBytes);
int dataStartByte = 44;
stream.Seek(dataStartByte, SeekOrigin.Begin);
Byte[] audioData = new Byte[lengthBytes];
numBytesRead = stream.Read(audioData, 0, lengthBytes);
if (numBytesRead != lengthBytes) throw new Exception("Could not read " + lengthBytes + " bytes, only read " + numBytesRead + " bytes.");
short[] shortData = new short[lengthBytes / 2];
for (int i = 0; i < lengthBytes; i += 2)
{
shortData[i / 2] = BitConverter.ToInt16(audioData, i);
}
return shortData;
}
示例12: CommandBuilder
private CommandBuilder(Connection connection, byte[] receivedBuffer)
{
m_ReceivedBuffer = receivedBuffer;
MemoryStream command = new MemoryStream(m_ReceivedBuffer);
byte[] header = new byte[Encoding.ASCII.GetByteCount(Constants.Protocol)];
command.Read(header, 0, header.Length);
if (!Encoding.ASCII.GetString(header).Equals(Constants.Protocol))
throw new InvalidDataException();
m_EncryptionMethod = (EncryptionMethod)command.ReadByte();
byte[] encryptedDataLength = new byte[2];
command.Read(encryptedDataLength, 0, encryptedDataLength.Length);
byte[] encryptedData = new byte[BitConverter.ToUInt16(encryptedDataLength, 0)];
if (encryptedData.Length > command.Position + encryptedData.Length)
throw new InvalidDataException();
command.Read(encryptedData, 0, encryptedData.Length);
switch (m_EncryptionMethod)
{
case EncryptionMethod.None:
m_CommandData = new MemoryStream(encryptedData);
break;
case EncryptionMethod.Rijndael:
m_CommandData = new MemoryStream(Core.RijndaelDecrypt(connection.ReceivingKey, encryptedData));
break;
case EncryptionMethod.RSA:
m_CommandData = new MemoryStream(Core.RSADecrypt(encryptedData));
break;
default:
throw new InvalidDataException();
}
}
示例13: DecryptDataAsBytes
private static byte[] DecryptDataAsBytes(byte[] encryptedData, string privateKeyPem, string publicKeyPem)
{
using (var encryptedDataStream = new MemoryStream(encryptedData))
using (var encryptedDataWithoutKeyPair = new MemoryStream())
{
var signatureLengthBytes = new byte[4];
var bytesRead = encryptedDataStream.Read(signatureLengthBytes, 0, 4);
if (bytesRead == -1)
throw new Exception("Unexpected end of encrypted data (expected encrypted key size)");
var signatureLength = BitConverter.ToInt32(signatureLengthBytes, 0);
var signature = new byte[signatureLength];
bytesRead = encryptedDataStream.Read(signature, 0, signatureLength);
if (bytesRead != signatureLength)
throw new Exception("Unexpected end of encrypted data (expected encrypted key)");
encryptedDataStream.CopyTo(encryptedDataWithoutKeyPair);
var decryptedData = EncryptedDataWithKeyPair.DecryptDataAsBytes(
encryptedDataWithoutKeyPair.ToArray(), privateKeyPem);
var signatureVerified = AsymmetricCryptoUtil.VerifySignature(decryptedData, signature, publicKeyPem);
if (!signatureVerified)
throw new Exception("Message could not be verified");
return decryptedData;
}
}
示例14: ReturnTumbNail
private static void ReturnTumbNail(HttpContext context, string thumbNailProductIDQuery)
{
int productID;
if (!string.IsNullOrWhiteSpace(thumbNailProductIDQuery) && int.TryParse(thumbNailProductIDQuery, out productID))
{
using (var db = new AvensiaWebshopEntities())
{
var imageQuery = db.Product.FirstOrDefault(p => p.ProductID == productID);
if (imageQuery == null)
{
Return404(context);
return;
}
context.Response.ContentType = "image/gif";
Stream strm = new MemoryStream(imageQuery.ThumbNailPhoto);
byte[] buffer = new byte[4096];
int byteSeq = strm.Read(buffer, 0, 4096);
while (byteSeq > 0)
{
context.Response.OutputStream.Write(buffer, 0, byteSeq);
byteSeq = strm.Read(buffer, 0, 4096);
}
}
}
}
示例15: ReturnImage
private static void ReturnImage(HttpContext context, string imageNameQuery)
{
if (!string.IsNullOrWhiteSpace(imageNameQuery))
{
using (var db = new AvensiaWebshopEntities())
{
var imageQuery = db.ProductPhoto.FirstOrDefault(i => i.LargePhotoFileName == imageNameQuery);
if (imageQuery == null)
{
Return404(context);
return;
}
context.Response.ContentType = "image/gif";
Stream strm = new MemoryStream(imageQuery.LargePhoto);
byte[] buffer = new byte[4096];
int byteSeq = strm.Read(buffer, 0, 4096);
while (byteSeq > 0)
{
context.Response.OutputStream.Write(buffer, 0, byteSeq);
byteSeq = strm.Read(buffer, 0, 4096);
}
}
}
}