本文整理汇总了C#中System.IO.MemoryStream.Read方法的典型用法代码示例。如果您正苦于以下问题:C# System.IO.MemoryStream.Read方法的具体用法?C# System.IO.MemoryStream.Read怎么用?C# System.IO.MemoryStream.Read使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.IO.MemoryStream
的用法示例。
在下文中一共展示了System.IO.MemoryStream.Read方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CreateZipByteArray
/// <summary>
/// Creates a zip archive from a byte array
/// </summary>
/// <param name="buffer">The file in byte[] format</param>
/// <param name="fileName">The name of the file you want to add to the archive</param>
/// <returns></returns>
public static byte[] CreateZipByteArray(byte[] buffer, string fileName)
{
ICSharpCode.SharpZipLib.Checksums.Crc32 crc = new ICSharpCode.SharpZipLib.Checksums.Crc32();
using (System.IO.MemoryStream zipMemoryStream = new System.IO.MemoryStream())
{
ICSharpCode.SharpZipLib.Zip.ZipOutputStream zipOutputStream = new ICSharpCode.SharpZipLib.Zip.ZipOutputStream(zipMemoryStream);
zipOutputStream.SetLevel(6);
ICSharpCode.SharpZipLib.Zip.ZipEntry zipEntry = new ICSharpCode.SharpZipLib.Zip.ZipEntry(fileName);
zipEntry.DateTime = DateTime.Now;
zipEntry.Size = buffer.Length;
crc.Reset();
crc.Update(buffer);
zipEntry.Crc = crc.Value;
zipOutputStream.PutNextEntry(zipEntry);
zipOutputStream.Write(buffer, 0, buffer.Length);
zipOutputStream.Finish();
byte[] zipByteArray = new byte[zipMemoryStream.Length];
zipMemoryStream.Position = 0;
zipMemoryStream.Read(zipByteArray, 0, (int)zipMemoryStream.Length);
zipOutputStream.Close();
return zipByteArray;
}
}
示例2: TestExtractArchiveTarGzCreateContainer
public void TestExtractArchiveTarGzCreateContainer()
{
CloudFilesProvider provider = (CloudFilesProvider)Bootstrapper.CreateObjectStorageProvider();
string containerName = TestContainerPrefix + Path.GetRandomFileName();
string sourceFileName = "DarkKnightRises.jpg";
byte[] content = File.ReadAllBytes("DarkKnightRises.jpg");
using (MemoryStream outputStream = new MemoryStream())
{
using (GZipOutputStream gzoStream = new GZipOutputStream(outputStream))
{
gzoStream.IsStreamOwner = false;
gzoStream.SetLevel(9);
using (TarOutputStream tarOutputStream = new TarOutputStream(gzoStream))
{
tarOutputStream.IsStreamOwner = false;
TarEntry entry = TarEntry.CreateTarEntry(containerName + '/' + sourceFileName);
entry.Size = content.Length;
tarOutputStream.PutNextEntry(entry);
tarOutputStream.Write(content, 0, content.Length);
tarOutputStream.CloseEntry();
tarOutputStream.Close();
}
}
outputStream.Flush();
outputStream.Position = 0;
ExtractArchiveResponse response = provider.ExtractArchive(outputStream, "", ArchiveFormat.TarGz);
Assert.IsNotNull(response);
Assert.AreEqual(1, response.CreatedFiles);
Assert.IsNotNull(response.Errors);
Assert.AreEqual(0, response.Errors.Count);
}
using (MemoryStream downloadStream = new MemoryStream())
{
provider.GetObject(containerName, sourceFileName, downloadStream, verifyEtag: true);
Assert.AreEqual(content.Length, GetContainerObjectSize(provider, containerName, sourceFileName));
downloadStream.Position = 0;
byte[] actualData = new byte[downloadStream.Length];
downloadStream.Read(actualData, 0, actualData.Length);
Assert.AreEqual(content.Length, actualData.Length);
using (MD5 md5 = MD5.Create())
{
byte[] contentMd5 = md5.ComputeHash(content);
byte[] actualMd5 = md5.ComputeHash(actualData);
Assert.AreEqual(BitConverter.ToString(contentMd5), BitConverter.ToString(actualMd5));
}
}
/* Cleanup
*/
provider.DeleteContainer(containerName, deleteObjects: true);
}
示例3: Zip
/********************************************************
* CLASS METHODS
*********************************************************/
/// <summary>
///
/// </summary>
/// <param name="zipPath"></param>
/// <param name="filenamesAndData"></param>
/// <returns></returns>
public static bool Zip(string zipPath, System.Collections.Generic.Dictionary<string, string> filenamesAndData)
{
var success = true;
var buffer = new byte[4096];
try
{
using (var stream = new ZipOutputStream(System.IO.File.Create(zipPath)))
{
foreach (var filename in filenamesAndData.Keys)
{
var file = filenamesAndData[filename].GetBytes();
var entry = stream.PutNextEntry(filename);
using (var ms = new System.IO.MemoryStream(file))
{
int sourceBytes;
do
{
sourceBytes = ms.Read(buffer, 0, buffer.Length);
stream.Write(buffer, 0, sourceBytes);
}
while (sourceBytes > 0);
}
}
stream.Flush();
stream.Close();
}
}
catch (System.Exception err)
{
System.Console.WriteLine("Compression.ZipData(): " + err.Message);
success = false;
}
return success;
}
示例4: ImageToByteArr
/// <summary>
/// 将图片转换成字节流
/// </summary>
/// <param name="img"></param>
/// <returns></returns>
private byte[] ImageToByteArr(Image img)
{
using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
{
img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
ms.Position = 0;
byte[] imageBytes = new byte[ms.Length];
ms.Read(imageBytes, 0, imageBytes.Length);
return imageBytes;
}
}
示例5: SerializeColumnInfo
private static string SerializeColumnInfo(List<ColumnInfo> arr)
{
using (System.IO.MemoryStream stream = new System.IO.MemoryStream())
{
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf =
new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
bf.Serialize(stream, arr);
stream.Position = 0;
byte[] buffer = new byte[(int)stream.Length];
stream.Read(buffer, 0, buffer.Length);
return Convert.ToBase64String(buffer);
}
}
示例6: Save
public void Save(Beetle.BufferWriter writer)
{
writer.Write(Message.GetType().FullName);
byte[] data;
using (System.IO.Stream stream = new System.IO.MemoryStream())
{
ProtoBuf.Meta.RuntimeTypeModel.Default.Serialize(stream, Message);
data = new byte[stream.Length];
stream.Position = 0;
stream.Read(data, 0, data.Length);
}
writer.Write(data);
}
示例7: BitmapToBitMapImage
public System.Windows.Media.Imaging.BitmapImage BitmapToBitMapImage(Bitmap bitmap)
{
System.IO.MemoryStream stream = new System.IO.MemoryStream();
bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);
stream.Position = 0;
byte[] data = new byte[stream.Length];
stream.Read(data, 0, Convert.ToInt32(stream.Length));
BitmapImage bmapImage = new BitmapImage();
bmapImage.BeginInit();
bmapImage.StreamSource = stream;
bmapImage.EndInit();
return bmapImage;
}
示例8: GZip
public static byte[] GZip(this byte[] bytes)
{
System.IO.MemoryStream ms = new System.IO.MemoryStream();
GZipStream gs = new GZipStream(ms, CompressionMode.Compress, true);
gs.Write(bytes, 0, bytes.Length);
gs.Close();
gs.Dispose();
ms.Position = 0;
bytes = new byte[ms.Length];
ms.Read(bytes, 0, (int)ms.Length);
ms.Close();
ms.Dispose();
return bytes;
}
示例9: GenerateCheckSum
/// <summary>
/// Generates a check sum from the serializable object.
/// </summary>
/// <param name="any">Any object that can be serialized.</param>
/// <returns>A check sum.</returns>
public static UInt16 GenerateCheckSum(object any)
{
System.Xml.Serialization.XmlSerializer xser = new System.Xml.Serialization.XmlSerializer(any.GetType());
System.IO.MemoryStream ms = new System.IO.MemoryStream();
xser.Serialize(ms, any);
ms.Seek(0, System.IO.SeekOrigin.Begin);
byte[] streambytes = new byte[ms.Length];
ms.Read(streambytes, 0, streambytes.Length);
UInt16[] ckarray = OrcaLogic.Collections.ArrayConverter.ToUInt16(streambytes);
ms.Close();
ms = null;
return OrcaLogic.Math.CheckSum.Generate(ckarray, ckarray.Length);
}
示例10: GenerateCheckSum
/// <summary>
/// Generates a check sum from the serializable object.
/// </summary>
/// <param name="any">Any object that can be serialized.</param>
/// <returns>A check sum.</returns>
public static UInt16 GenerateCheckSum(object any)
{
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
System.IO.MemoryStream ms = new System.IO.MemoryStream();
bf.Serialize(ms, any);
ms.Seek(0,System.IO.SeekOrigin.Begin);
byte[] streambytes = new byte[ms.Length];
ms.Read(streambytes, 0, streambytes.Length);
UInt16[] ckarray = OrcaLogic.Collections.ArrayConverter.ToUInt16(streambytes);
ms.Close();
ms = null;
bf = null;
return OrcaLogic.Math.CheckSum.Generate(ckarray, ckarray.Length);
}
示例11: ProcessRequest
//***************************************************************************
// Public Methods
//
public void ProcessRequest(HttpContext context)
{
HttpRequest req = context.Request;
string
qsImgUrl = req.QueryString["src"],
qsAct = req.QueryString["a"];
bool buttonActive = (string.IsNullOrEmpty(qsAct) || qsAct == "1");
string srcUrl = WebUtil.MapPath(WebUtil.UrlDecode(qsImgUrl));
System.IO.FileInfo fiImg = new System.IO.FileInfo(srcUrl);
if (!fiImg.Exists)
{ context.Response.StatusCode = 404; }
else
{
byte[] imgData = null;
string contentType = string.Format("image/{0}", fiImg.Extension.TrimStart('.'));
using (System.IO.FileStream fs = fiImg.OpenRead())
{
if (buttonActive)
{
// If the button is active, just load the image directly.
imgData = new byte[fs.Length];
fs.Read(imgData, 0, imgData.Length);
}
else
{
// If the button is disabled, load the image into a Bitmap object, then run it through the grayscale filter.
using (System.Drawing.Bitmap img = (System.Drawing.Bitmap)System.Drawing.Bitmap.FromStream(fs))
using (System.Drawing.Bitmap imgGs = RainstormStudios.Drawing.BitmapFilter.GrayScale(img))
using(System.IO.MemoryStream imgGsStrm = new System.IO.MemoryStream())
{
imgGs.Save(imgGsStrm, img.RawFormat);
imgData = new byte[imgGsStrm.Length];
imgGsStrm.Read(imgData, 0, imgData.Length);
}
}
}
context.Response.StatusCode = 200;
context.Response.ContentType = contentType;
context.Response.OutputStream.Write(imgData, 0, imgData.Length);
}
}
示例12: SerializeObjectToBytes
/// <summary>
///
/// </summary>
/// <param name="objectNeedSerialized"></param>
/// <param name="formatType"></param>
/// <returns></returns>
public static byte[] SerializeObjectToBytes(object objectNeedSerialized,SeralizeFormatType formatType)
{
System.Runtime.Serialization.IFormatter oFormatter = null;
if (formatType == SeralizeFormatType.BinaryFormat)
oFormatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
else if (formatType == SeralizeFormatType.XmlFortmat)
oFormatter = new System.Runtime.Serialization.Formatters.Soap.SoapFormatter();
System.IO.MemoryStream oStream = new System.IO.MemoryStream();
oFormatter.Serialize(oStream, objectNeedSerialized);
byte[] oBuffer = new byte[oStream.Length];
oStream.Position = 0;
oStream.Read(oBuffer, 0, oBuffer.Length);
return oBuffer;
}
示例13: Index
//
// GET: /QR/
/// <summary>
///
/// </summary>
/// <param name="qrUrl"></param>
/// <returns></returns>
public ImageResult Index(string qrUrl)
{
using (var bitmap = EncodeUrl(qrUrl))
{
const string contentType = "image/jpg";
byte[] data;
using (var stream = new System.IO.MemoryStream())
{
bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
stream.Position = 0;
data = new byte[stream.Length];
stream.Read(data, 0, (int)stream.Length);
stream.Close();
}
return this.Image(data, contentType);
}
}
示例14: decriptUrl
public static string decriptUrl(string Valor)
{
string Chave = "#[email protected]!s#";
Valor = Valor.Replace("MAIS", "+");
TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
des.IV = new byte[8];
PasswordDeriveBytes pdb = new PasswordDeriveBytes(Chave, new byte[-1 + 1]);
des.Key = pdb.CryptDeriveKey("RC2", "MD5", 128, new byte[8]);
byte[] encryptedBytes = Convert.FromBase64String(Valor);
System.IO.MemoryStream MS = new System.IO.MemoryStream(Valor.Length);
CryptoStream decStreAM = new CryptoStream(MS, des.CreateDecryptor(), CryptoStreamMode.Write);
decStreAM.Write(encryptedBytes, 0, encryptedBytes.Length);
decStreAM.FlushFinalBlock();
byte[] plainBytes = new byte[Convert.ToInt32(MS.Length - 1) + 1];
MS.Position = 0;
MS.Read(plainBytes, 0, Convert.ToInt32(MS.Length));
decStreAM.Close();
return System.Text.Encoding.UTF8.GetString(plainBytes);
}
示例15: ExecuteResult
public override void ExecuteResult(ControllerContext context)
{
var response = context.HttpContext.Response;
response.Clear();
response.ContentType = MimiType;
response.AddHeader("content-disposition", String.Format("attachment;filename={0}.{1}", ReportName, FileExt));
byte[] buf = new byte[32768];
using (var spreadsheet = new System.IO.MemoryStream(RenderBytes))
{
while (true)
{
int read = spreadsheet.Read(buf, 0, buf.Length);
if (read <= 0)
break;
response.OutputStream.Write(buf, 0, read);
}
}
response.Flush();
}