Reverse MemoryStream.ToArray() - c#

I am getting an httpwebresponse stream (of a flash file), and I would like to save it as binary and later access that binary and display it as flash. Right now, I am writing the response stream to a MemoryStream and then calling ToArray() on the MemoryStream. I get a handy byte[].
How do I reverse that function? How do I get the stream of the flash file from the byte[] I've generated?
Thanks!

Super easy:
Stream s = new MemoryStream(byteArray);
More info: http://msdn.microsoft.com/en-us/library/e55f3s5k.aspx

You can use this:
var s = new MemoryStream(byteArray);

Related

NetworkStream a thing i don´t understand in my code

I got help in a previous question on how to send an image.
The thing done was to first send the lenght of the image(size), and then the actual image, then it would know when it was done.
IT looks like this:
BinaryWriter writer = new BinaryWriter(netStream);
while (someCondition) {
Image img = SomeImage();
MemoryStream ms = new MemoryStream();
img.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
byte[] buffer = new byte[ms.Length];
ms.Seek(0, SeekOrigin.Begin);
ms.Read(buffer, 0, buffer.Length);
writer.Write(buffer.Length);
writer.Write(buffer);
This code is from: Angelo Geels , who helped me in my previous question.
Now, i tried to optimize this in a way. And well, it works. But ONLY when the file is bmp (uncompressed), and i don´t know why.
using (MemoryStream ms = PrintWindow(process))
{
writer.Write((int)ms.Length);
writer.Write(ms.GetBuffer());
}
So PrintWindow save an image to a memorystream and returns it. so ms = memorystream with my image in it.
So for me this should work perfectly, cause form what i can se i do the same thing.
i send the size of the file (length of the memorystream).
Then i send the byte[] data in the memorystream.
So, it´s the same thing.
But, it only works with bmp.
The only thing i can think of is that when i save in a compressed format, the bmp is first written and then encoded, which messes up the getbuffer() or something.
But i still think it should work.
You write too many bytes, use the Write() overload that lets you specify how much to write:
using (MemoryStream ms = PrintWindow(process)) {
writer.Write((int)ms.Length);
writer.Write(ms.GetBuffer(), 0, (int)ms.Length);
}
Don't use GetBuffer. From the documentation:
Note that the buffer contains allocated bytes which might be unused.
For example, if the string "test" is written into the MemoryStream
object, the length of the buffer returned from GetBuffer is 256, not
4, with 252 bytes unused. To obtain only the data in the buffer, use
the ToArray method; however, ToArray creates a copy of the data in
memory.
Use:
writer.Write(ms.ToArray());
Or if you are in 4.0 use the CopyTo method:
ms.CopyTo(netStream);
Check this if you are not in 4.0 for a way to copy streams:
How do I copy the contents of one stream to another?

How can I send and receive a large file over HTTP in C#

I am working on developing an HTTP Server/Client and I can currently send small files over it such as .txt files and other easy to read files that do not require much memory. However when I want to send a larger file say a .exe or large .pdf I get memory errors. This are occurring from the fact that before I try to send or receive a file I have to specify the size of my byte[] buffer. Is there a way to get the size of the buffer while reading it from stream?
I want to do something like this:
//Create the stream.
private Stream dataStream = response.GetResponseStream();
//read bytes from stream into buffer.
byte[] byteArray = new byte[Convert.ToInt32(dataStream.Length)];
dataStream.read(byteArray,0,byteArray.Length);
However when calling "dataStream.Length" it throws the error:
ExceptionError: This stream does not support seek operations.
Can someone offer some advice as to how I can get the length of my byte[] from the stream?
Thanks,
You can use CopyTo method of the stream.
MemoryStream m = new MemoryStream();
dataStream.CopyTo(m);
byte[] byteArray = m.ToArray();
You can also write directly to file
var fs = File.Create("....");
dataStream.CopyTo(fs);
The network layer has no way of knowing how long the response stream is.
However, the server is supposed to tell you how long it is; look in the Content-Length response header.
If that header is missing or incorrect, you're out of luck; you'll need to keep reading until you run out of data.

Convert 64base byte array to PDF and show in webBroswer compont

This is what i would like to do:
Get a 64base byte array from database (which is actually in pdf format). This works.
Then i would like to show the pdf in a webbrowser component.
I first started with saving the pdf to a file.pdf and then open it:
byte[] bitjes = isc.GetFileById(fileid); // Getting the bytes
FileStream stream = new FileStream(#"C:\NexusPDF\" + filename, FileMode.CreateNew);
BinaryWriter writer = new BinaryWriter(stream);
writer.Write(bitjes, 0, bitjes.Length);
writer.Close();
webBrowser.Navigate(#"C:\NexusPDF\" + filename);
But that gave me all sorts of problems involving read/write acces. So i figured i have to use the memorystream class to solve this problem.
byte[] bitjes = isc.GetFileById(fileid);
MemoryStream memstream = new MemoryStream(bitjes);
BinaryWriter writer = new BinaryWriter(memstream);
writer.Write(bitjes, 0, bitjes.Length);
writer.Close();
But here's where i'm stuck! I can't just show this in a webBrowser component can i?
Do i have to use the binaryreader before i can show the pdf?
Am i approaching this problem the right way, or are there better alternatives?
Main thing is that i don't want to save the file on disk.
Any help will be appreciated.
You may be able to use the data URL scheme. This URL scheme specifies the content inline.
webBrowser.Navigate("data:application/pdf;base64," + X);
Where X is the base 64 string.
No need to convert the base 64 PDF string into a byte array!
See http://www.ietf.org/rfc/rfc2397.txt for more details.

Save a file to SQL database using Silverlight and LINQ

What is the best way to save a file to my SQL database using Silverlight and LINQ?
I have read through some articles, some of them here on StackOverflow and there is so much information that I am not sure what is the best.
I have something that works using:
// Read the file
var reader = new StreamReader(openFileDialog.File.OpenRead());
contents = reader.ReadToEnd();
reader.Close();
// Convert to byte[]
byte[] inputbuffer;
var encoding = new UTF8Encoding();
inputBuffer = encoding.GetBytes(contents);
but according to something I read here on StackOverflow, using UTF8Encoding is not a good idea.
Also I can get the file from the database using LINQ when I need it, but how do I convert it back from the byte[] to the actual file?
Or would using WCF to save and retrieve a file be better?
Any ideas are greatly appreciated.
yes UTF8Encoding is not a good option.
You can use the FileStream's copyto method to copy the files bytes into a memorystream and use it's ToArray method to get all bytes instead.
If you can access the DB directly from Silverlight than this should be ok but the second part of your questions indicates that you might not be sure(?) - if so please put this into another question.
Here is a snippet to return the bytes from the file:
var stream = openFileDialog.File.OpenRead();
using (var memStream = new System.IO.MemoryStream())
{
stream.CopyTo(memStream);
return memStream.ToArray();
}
To save it back you will have to use the SaveFileDialog class in silverlight

How to convert byte array to image file?

I have browsed and uploaded a png/jpg file in my MVC web app.
I have stored this file as byte[] in my database.
Now I want to read and convert the byte[] to original file.
How can i achieve this?
Create a MemoryStream passing the array in the constructor.
Read the image from the stream using Image.FromStream.
Call theImg.Save("theimage.jpg", ImageFormat.Jpeg).
Remember to reference System.Drawing.Imaging and use a using block for the stream.
Create a memory stream from the byte[] array in your database and then use Image.FromStream.
byte[] image = GetImageFromDatabase();
MemoryStream ms = new MemoryStream(image);
Image i = Image.FromStream(ms);
May you have trouble with the mentioned solutions on DotNet Core 3.0 or higher
so my solution is:
using(var ms = new MemoryStream(yourByteArray)) {
using(var fs = new FileStream("savePath", FileMode.Create)) {
ms.WriteTo(fs);
}
}
Or just use this:
System.IO.File.WriteAllBytes(string path, byte[] bytes)
File.WriteAllBytes(String, Byte[]) Method (System.IO) | Microsoft Docs

Categories

Resources