memorystream

When will MemoryStream.TryGetBuffer return a useful ArraySegment?

拟墨画扇 提交于 2019-12-23 07:16:06
问题 bool MemoryStream.TryGetBuffer(out ArraySegment<byte> buffer) is a new API in .NET 4.6 that can be used to get access to the valid bytes stored in the MemoryStream without copying them. This is very exciting! It returns a bool that is "true if the conversion was successful; otherwise, false" and populates the out param. When does it return true, indicating that the out ArraySegment<byte> buffer now contains valid information? This isn't documented today. I know that if it returns false, I can

write audio to memorystream from file

萝らか妹 提交于 2019-12-23 05:16:11
问题 I'm trying to send this file to the outputstream but cannot figure out why it spits out basically an empty mp3 file. As you can see I would get an exception closing the stream prematurely so I have commented out for now. Any pointers appreciated. using (FileStream mp3file = File.OpenRead(newFile)) { context.Response.AddHeader("content-transfer-encoding", "binary"); context.Response.ContentType = "audio/mpeg"; MemoryStream memStream = new MemoryStream(); byte[] bytes = new byte[mp3file.Length]

Read MemoryStream - load image \ byte and read it

为君一笑 提交于 2019-12-23 03:26:33
问题 ok i have image that i bind info in it and i want to read the info now from file (FileStream) its work but i want to do it not from file so i need to use MemoryStream here the example that work and how i do it now how i make it work with MemoryStream (with byte = My.Resources or PictureBox1.image) Using FS As New IO.FileStream(image, IO.FileMode.Open) FS.Seek(0, IO.SeekOrigin.End) While Not FS.ReadByte = Asc("|") FS.Position -= 2 End While Dim s As String = Nothing While Not FS.Position = FS

How to fill a MemoryStream with 0xFF bytes?

做~自己de王妃 提交于 2019-12-22 18:47:16
问题 I have a MemoryStream which is created from a File at runtime. Then the MemoryStream is edited and some bytes are removed. Now I have to maintain a Constant Filesize so I have to fill the MemoryStream with 0xFF bytes.. What is the Fastest way to Do this Operation? I know, that I always can loop through the MemoryStream sizes and add 0xFF's but I need to know a faster and more efficient way to do it! 回答1: If you have many bytes to write to the stream, it may be more efficient to write a array

Convert BitmapSource to MemoryStream

给你一囗甜甜゛ 提交于 2019-12-22 18:20:13
问题 How Do I convert BitmapSource to MemoryStream. Though I tried some code: private Stream StreamFromBitmapSource(BitmapSource writeBmp) { Stream bmp; using (bmp = new MemoryStream()) { BitmapEncoder enc = new BmpBitmapEncoder(); enc.Frames.Add(BitmapFrame.Create(writeBmp)); enc.Save(bmp); } return bmp; } It doesn't give any error but after putting debugging point it is showing some exceptions which are listed below. Capacity: 'printStream.Capacity' threw an exception of type 'System

Can a byte[] buffer for a MemoryStream have variable size?

a 夏天 提交于 2019-12-22 11:21:37
问题 I'm serializing an object to a byte[] using MemoryStream : byte[] serialized = new byte[1000]; using (MemoryStream stream = new MemoryStream(serialized)) using (TextWriter textWriter = new StreamWriter(stream)) serializer.Serialize(textWriter, stuffToSerialize); is there any way I can set 'serialized' to grow according to the size of the stuffToSerialize ? 回答1: The parameterless constructor new MemoryStream() uses one. Then serialise into it, and then when you need the byte[] call ToArray()

Opening an audio (wav) file from a MemoryStream to determine the duration

大憨熊 提交于 2019-12-22 08:31:53
问题 Is there a way, either within the Framework or by using P/Invoke to determine the duration of a wav file that's held in a MemoryStream? I've already had a look at Managed DirectX and another similar question, but everything seems to work with paths, rather than providing any way to pass in a stream. One of the links in the question I've referenced (A simple C# Wave editor....) makes it fairly clear that I could parse the MemoryStream to determine the duration of the wav file. Ideally I'd like

How to copy one file to many locations simultaneously

拟墨画扇 提交于 2019-12-22 07:27:07
问题 I want to find a way to copy one file to multiple locations simultaneously (with C#). means that i don't want the original file to be read only one time, and to "paste" the file to another locations (on local network). as far as my tests showed me, the File.Copy() will always read the source again. and as far as i understand, even while using memory, that memory piece gets locked. so basically, i want to mimic the "copy-paste" to the form of one "copy", and multiple "paste", without re

How to Merge two memory streams containing PDF file's data into one

纵饮孤独 提交于 2019-12-22 05:16:10
问题 I am trying to read two PDF files into two memory streams and then return a stream that will have both stream's data. But I don't seem to understand what's wrong with my code. Sample Code: string file1Path = "Sampl1.pdf"; string file2Path = "Sample2.pdf"; MemoryStream stream1 = new MemoryStream(File.ReadAllBytes(file1Path)); MemoryStream stream2 = new MemoryStream(File.ReadAllBytes(file2Path)); stream1.Position = 0; stream1.Copyto(stream2); return stream2; /*supposed to be containing data of

How do I “fork” a Stream in .NET?

你说的曾经没有我的故事 提交于 2019-12-22 04:33:35
问题 As discussed before, when a BinaryReader or BinaryWriter gets closed, its underlying Stream get closed as well (aargh). Consider this situation: a routine R is passed a MemoryStream, say M ; I would like to write some stuff to M and then pass it to another routine for more processing (not necessarily writing). For convenience, I'd like to wrap M in a BinaryWriter to do my writing. After writing, I'm done with the BinaryWriter but not with M . void R(MemoryStream M) { using (B = new