Questions tagged [memorystream]

4

votes
0

answer
37

Views

Why Stream.Copy is faster than Stream.Write to FileStream?

I have a question and I can't find a reason for it. I'm creating a custom archive file , I'm using Memory Streams to storing data and finally I use a file stream to write the data to disk. My Hard Disk is SSD , But the speed was too slow when I tried to write only 95MB to a file , It takes 12 second...
MikeTheCoder
1

votes
1

answer
352

Views

Cannot write to new MemoryStream - Stream closed

I have methods that compress one or more files to either a tar(.gz) or a zip. In these files I use bytes that I have already loaded in to memory, and I just need them to be compressed, and the compressed file to be written to a MemoryStream. At first, I was using memorystream references, to save on...
SimonC
1

votes
1

answer
2.1k

Views

Deserialize from MemoryStream issue

I need help to figure out how to deserialize from MemoryStream. var xmlStream = new MemoryStream(); e.Extract(xmlStream); if (xmlStream != null) { TextReader tr = new StreamReader(xmlStream); var contentItems = new ContentItems(); var serializer = new XmlSerializer(typeof(ContentItems)); contentIte...
1

votes
1

answer
1.4k

Views

how to save MemoryStream to JPEG in Windows 8 c#

I see solution for this problem: Saving as jpeg from memorystream in c# but it does not work in winRT. Is there a simple way to save MemoryStream as JPEG using FileSavePicker? I tried: private async void Save_Image(MemoryStream image) { // Launch file picker FileSavePicker picker = new FileSavePic...
ashish nirkhe
1

votes
1

answer
2.3k

Views

PdfStamper.CreateSignature and empty MemoryStream

Using iTextSharp 5.3.4.0, I'm having difficulty working with PdfStamper and MemoryStream. The MemoryStream is always empty. PdfReader pdfReader = new PdfReader(Server.MapPath(@"Document.pdf")); MemoryStream memoryStream = new MemoryStream(); PdfStamper pdfStamper = PdfStamper.CreateSignature(pdfRead...
brunopacola
1

votes
2

answer
2.7k

Views

Write to gzip file from MemoryStream using XmlWriter and GZipStream

I am tring to write generate a gzipped XML file from a MemoryStream. Here is what I have so far - XmlWriterSettings settings = new XmlWriterSettings(); settings.Indent = true; MemoryStream ms = new MemoryStream(); using (XmlWriter writer = XmlWriter.Create(ms, settings)) { // CREATE XML } Then I wan...
A Bogus
1

votes
2

answer
1k

Views

How to use Parallel.ForEach with image object?

Now my method is : public static byte[] ImageToByte(Image img) { byte[] byteArray = new byte[0]; using (MemoryStream stream = new MemoryStream()) { img.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp); stream.Close(); byteArray = stream.ToArray(); } return byteArray; } after read this. And i sti...
Polamin Singhasuwich
1

votes
2

answer
400

Views

MemoryStream Seek, Needed After Instance Creation With Buffer?

Do I need a Seek call in this code? // Assume bytes = byte[] of some bytes using (var memoryStream = new MemoryStream(bytes)) { memoryStream.Seek(0, SeekOrigin.Begin); return new BinaryFormatter().Deserialize(memoryStream); }
Haney
1

votes
3

answer
5.4k

Views

Memory stream out of memory exception when filesize greter than 4 gb

I am having a problem with memory stream. I am getting a exception of out of memory exception. How to handle this. var file = VC.ReadStream(filename, true); var memoryStream = new MemoryStream(); file.CopyTo(memoryStream); var fileContentBytes = memoryStream.ToArray(); memoryStream = null; LogUtil.L...
Prashant Kumar
1

votes
1

answer
74

Views

MemoryStream doesn't fill attachment

I am developing a method for C# where to send as an attachment a byte[]. The method below sends fine the email, but the attachment is always empty. public bool envio(MailMessage mail, SmtpClient cliente, byte[] origen) { bool res = true; System.IO.MemoryStream ms; System.IO.StreamWriter writer; ms =...
Dinalan
2

votes
2

answer
457

Views

Why do these two methods of loading images from SQL CE into a WPF Image produce different results?

In a ValueConverter, I was trying to convert a System.Data.Linq.Binary (SQL CE image) to a BitmapImage. This method works (image is show correctly on the form): public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { Binary binary = value as Binary; if (binary...
Mark Carpenter
2

votes
2

answer
2.3k

Views

Copy data from MemoryStream directly to BinaryWriter.BaseStream?

I have a bunch of data in a MemoryStream and I want to write all of it to a BinaryWriter. Lucky for me all streams now have a Stream.CopyTo(Stream) method, and I can get the target stream from the BinaryWriter.BaseStream property. public void WriteTo(BinaryWriter writer) { this.memoryStream.CopyTo(w...
2

votes
1

answer
3.3k

Views

How to create Source for WPF Media Element from byte array using c#?

I have converted a video to bytes array and stored it in MsSql Database. Now I fetch it from database and I get the byte array. I want to set this byte array as Source of media element and then show this video in WPF application. Please suggest a way where I can convert byte array or memory stream t...
Akash Soni
17

votes
1

answer
6.6k

Views

MemoryStream have one thread write to it and another read

This is how I write to a stream then read from it using 1 thread: System.IO.MemoryStream ms = new System.IO.MemoryStream(); // write to it ms.Write(new byte[] { 1, 2, 3, 4, 5, 6, 7 }, 0, 7); // go to the begining ms.Seek(0, System.IO.SeekOrigin.Begin); // now read from it byte[] myBuffer = new byte[...
Tono Nam
17

votes
2

answer
31.4k

Views

Stream was not readable error

I am getting the message "Stream was not readable" on the statement: using (StreamReader sr = new StreamReader(ms)) I have tried the tips posted here without success. Thanks for the help. This is my code: XmlSerializer xmlSerializer = new XmlSerializer(typeof(Conflict)); //Serialize Conflicts arra...
Arnold Krohn
7

votes
3

answer
9.3k

Views

How can I clone MemoryStream object?

I have a MemoryStream object which is passed by Stream type parameter (Stream is abstract class in C#). I want to clone him and to create another MemoryStream object a side with current position of the original and to create also a new XMLReader out of it, so I will be able to read its content....
JavaSa
2

votes
1

answer
769

Views

How do I store arbitrary binary data in a binary serialized class?

Using C#/.NET for my application, I've got a series of classes in my main data model that represent "binary" (as opposed to text) content. I've got a inheritance setup like this: Basically, the abstract class BinaryContent contains a MemoryStream that stores arbitrary binary data. That data is read...
Benny Jobigan
18

votes
1

answer
18k

Views

Getting PdfStamper to work with MemoryStreams (c#, itextsharp)

It came to me to rework old code which signs PDF files into new one, which signs MemoryStreams (byte arrays) that come and are sent by web services. Simple, right? Well, that was yesterday. Today I just can't get it to work. This is the old code, which uses FileStreams and it works: public static st...
ADSMarko
18

votes
5

answer
20.8k

Views

MemoryStream.Read doesn't copy bytes to buffer - c#

I don't really get it and it's driving me nuts. i've these 4 lines: Image img = Image.FromFile("F:\\Pulpit\\soa.bmp"); MemoryStream imageStream = new MemoryStream(); img.Save(imageStream, ImageFormat.Bmp); byte[] contentBuffer = new byte[imageStream.Length]; imageStream.Read(contentBuffer, 0, conten...
agnieszka
2

votes
3

answer
8.1k

Views

Equivalent of Java's “ByteBuffer.putType()” in C#

I am trying to format a byte array in C#, by porting a code from Java. In Java, the methods "buf.putInt(value);", buf.putShort, buf.putDouble, (and so forth) are used. However I don't know how to port this to C#. I have tried the MemoryStream class, but there is no method to put a specific type at t...
Lazlo
2

votes
2

answer
125

Views

How writing to MemoryStream has changed in Delphi 10.2?

I am porting my code from 10.1 to 10.2 and this gives me error: procedure TForm4.FormCreate(Sender: TObject); const CFourBytes: array[0..3] of Byte = (1, 2, 3, 4); var LStream: TMemoryStream; LBuffer: array of Byte; begin SetLength(LBuffer, 4); LStream := TMemoryStream.Create; LStream.Write(@CFourBy...
Wodzu
2

votes
3

answer
1.6k

Views

Deserialization throws an exception for this bit of C# code [closed]

Edit: Closing this because i've found the reason why it's erroring, but instead of removing this post .. i generate a newer post with a more refined question. Hi folks, i have some binary data i've read in. i wish to convert it to an System.Drawing.Image, so i create an instance of an Image object,...
1

votes
1

answer
90

Views

Is there a way to convert between byte[] to Stream and back without copying?

I need to convert a byte[] array into a stream (e.g. for uploading a file), then in another part of my code, convert a stream back into an array (e.g. for downloading a file). Is there a way to do these operations without making a copy of the byte arrays? This bit of code shows that MemoryStream cr...
noctonura
6

votes
3

answer
7.2k

Views

Include from “php://memory” stream

I'm writing a system for a browser application that will store some particular php scripts in a database and then pull them out and execute them when needed. At first I tried using exec() and piping to php the output of a script that got the scripts out of the database and printed them. This worked...
terminal_case
19

votes
1

answer
21.5k

Views

Get Imagesource from Memorystream in c# wpf

How can I get ImageSource from MemoryStream in WPF using c# ? or convert MemoryStream to ImageSource to display it as image in wpf ?
kartal
5

votes
1

answer
3.2k

Views

Trying to convert a ZipArchiveEntry into a byte[]

I am trying to take a list of ZipArchiveEntrys and convert them into byte arrays, but I am logistically running into a wall. So far I am trying to convert it into a MemoryStream to convert it into the byte[] like this: public static void ScanUpload(List scan) { foreach (var s in scan) {...
scapegoat17
1

votes
3

answer
5.7k

Views

Write one memorystream of UTF8-encoded data to the end of another C#

I'm trying to append the contents of one memory stream to the contents of another, knowing that both memorystream contain UTF8-encoded data, and get a UTF8 string back when I convert the combined memorystream back. But it's not working => the second memorystream is being appended as garbage (or at l...
user483679
1

votes
2

answer
6.4k

Views

how to convert created excel file using closed xml into bytes format

Hi I am using closedxML DLL for exporting to excel i have static method like this below public static void WriteToExcel(string fileName, List pages) { var wb = new XLWorkbook(); byte[] file; var ws = wb.Worksheets.Add("CPs"); WriteCostHeader(ws); ////write all the header columns //for (int i = 0; i...
Enigma State
2

votes
2

answer
4.4k

Views

Search and Replace of text in a memorystream in C# .NET

I have loaded a memorystream with a word document and I want to be able to alter specific text within the memorystream and save it back to the word document, like search and replace functionality. Please can anyone help me with this as I don't want to use the Word Interop libraries. I have the cod...
7

votes
3

answer
5.5k

Views

Advantage / Disadvantage MemoryStream.Position or MemoryStream.Seek [duplicate]

This question already has an answer here: Stream.Seek(0, SeekOrigin.Begin) or Position = 0 2 answers What is the advantage or disadvantage (or the difference) to use memoryStream.Seek(0, SeekOrigin.Begin); instead of memoryStream.Position = 0
SeToY
4

votes
3

answer
3.4k

Views

C# How to use MemoryStream with multithreading

My current code is: public static byte[] ImageToByte(Image img) { byte[] byteArray = new byte[0]; using (MemoryStream stream = new MemoryStream()) { img.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp); stream.Close(); byteArray = stream.ToArray(); } return byteArray; } Is there any way...
Polamin Singhasuwich
5

votes
1

answer
16.1k

Views

Decompress byte array to string via BinaryReader yields empty string

I am trying to decompress a byte array and get it into a string using a binary reader. When the following code executes, the inStream position changes from 0 to the length of the array, but str is always an empty string. BinaryReader br = null; string str = String.Empty; using (MemoryStream inStrea...
jkh
20

votes
3

answer
53.8k

Views

StreamWriter writing to MemoryStream

I was under the impression that when you called Flush() in a StreamWriter object it writes to the underlying stream, but apparently this isn't the case with my code. Instead of writing to my file it will just write nothing. Any ideas where I'm going wrong? public FileResult DownloadEntries(int id)...
ediblecode
6

votes
3

answer
3.2k

Views

Writing arbitrary type instances into a MemoryStream in C#

In Delphi, one can do the following: var ms : TMemoryStream; i : Integer; begin ms := TMemoryStream.Create; i := 1024; ms.Write(@i, SizeOf(Integer)); ms.Free; end; This will write the memory contents of i into ms. The .Net version of MemoryStream doesn't have such a feature (neither the managed n...
IamIC
4

votes
1

answer
5.6k

Views

Encrypted Serialization of Objects in C#

I am storing my DataTable in a file using the function given below which i had taken from a website. The code works well. The problem is: I want to apply some sort of encryption here. How can i achieve that? public void SerializeObject(T serializableObject, string fileName) { if (serializableObject...
Moons
1

votes
3

answer
1.8k

Views

Export file using image saved to memory stream

I am wanting to save an image to file which has been stored in a memory stream. I have saved a asp.net chart to a memory stream. stream1 = new MemoryStream(); chart_location_3.SaveImage(stream1, ChartImageFormat.Png); and i am then using the following code to export to a jpg. It triggers the save to...
Chris
6

votes
4

answer
7.8k

Views

Convert float to its binary representation (using MemoryStream?)

I'd like to convert a given float into its binary representation. I tried to write the float value into a MemoryStream, read this MemoryStream byte by byte and convert the bytes into their binary representation. But every attempt failed. "Can't read closed stream" (but I only closed the writer) For...
Cubinator73
5

votes
1

answer
9.1k

Views

Email Attachment from memory stream is coming as blank in C#

I am able to send an attachment in a mail but the attachment content is blank and size is being shown as 0 bytes. After doing some search over the internet found that we need to reset the memory stream position to 0 in order to start from start. I tried that as well but it seems it is not working. C...
Vineet More
2

votes
5

answer
552

Views

Safely access data in MemoryStream

Assume that I have a MemoryStream and function that operates on bytes. Current code is something like this: void caller() { MemoryStream ms = // not important func(ms.GetBuffer(), 0, (int)ms.Length); } void func(byte[] buffer, int offset, int length) { // not important } I can not change func but I...
Bobrovsky
2

votes
4

answer
1.5k

Views

MemoryStream.SetLength(0) and MemoryStream.Capacity = 0 don't clear used memory

I have a program that streams media, which creates two huge MemoryStreams, and then needs to zero them when done. The problem is, the memory does not seem to be reclaimed after I call BOTH MemoryStream.SetLength(0); and MemoryStream.Capacity = 0. GC.Collect() seems to solve the problem, but I have h...
IDWMaster

View additional questions