Writing Stream to File

  • Thread starter Thread starter Annette Miller
  • Start date Start date
A

Annette Miller

Hi All,

I'm just wondering if someone can show me how to write a stream to a file.
Specifically I have a file (bitmap) as an embedded resource in my app i.e.
"MyApp.Resources.BitmapTemplate.bmp" and was wondering how I can write it
to, say "C:\temp.bmp" from the following

Stream stream =
System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("MyApp.Resources.BitmapTemplate.bmp");

Cheers!
 
Apologies for not offering a better roadmap, but I have not done this
particular task. Below is the direction I would look at to solve the problem.

The .NET Stream object is simply a transport from one [data] store to
another. The store can be persistent, meaning it is written off to a more
permanent medium, like the file system (thus, a FileStream). Streams can also
write to non-persistent store, like another stream.

One you get the bits into a memory location, you can stream out of that
location (MemoryStream perhaps) and use that stream in the constructor of a
stream that can write to disk drive (probably a FileStream). It is also
possible you will have to use a BinaryStream to guarantee the file is written
out as binary and not ascii.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

***************************
Think Outside the Box!
***************************
 
Cowboy (Gregory A. Beamer) - MVP <[email protected]>
wrote:

It is also
possible you will have to use a BinaryStream to guarantee the file is written
out as binary and not ascii.

All Streams are binary - they're specifically for binary data rather
than text data. Readers/Writers are for text data, but any stream
should preserve all binary data, I'd expect.
 
Annette Miller said:
Hi All,

I'm just wondering if someone can show me how to write a stream to a file.
Specifically I have a file (bitmap) as an embedded resource in my app i.e.
"MyApp.Resources.BitmapTemplate.bmp" and was wondering how I can write it
to, say "C:\temp.bmp" from the following

Stream stream =
System.Reflection.Assembly.GetExecutingAssembly().
GetManifestResourceStream("MyApp.Resources.BitmapTemplate.bmp");

You need something like:

using (Stream output = new FileStream (...))
{
byte[] buffer = new byte[32*1024];
int read;

while ( (read=stream.Read(buffer, 0, buffer.Length)) > 0)
{
output.Write(buffer, 0, read);
}
}

In other words, you read a chunk at a time into a buffer, and write out
the chunks immediately after you've read each one.
 
Hi Jon,

Thanks heaps. It works a treat! One question - when declaring the byte
array, why 32 * 1024. Obviously 1024 because it makes a kilobyte, but why by
32?

Cheers.
Jon Skeet said:
Annette Miller said:
Hi All,

I'm just wondering if someone can show me how to write a stream to a
file.
Specifically I have a file (bitmap) as an embedded resource in my app
i.e.
"MyApp.Resources.BitmapTemplate.bmp" and was wondering how I can write it
to, say "C:\temp.bmp" from the following

Stream stream =
System.Reflection.Assembly.GetExecutingAssembly().
GetManifestResourceStream("MyApp.Resources.BitmapTemplate.bmp");

You need something like:

using (Stream output = new FileStream (...))
{
byte[] buffer = new byte[32*1024];
int read;

while ( (read=stream.Read(buffer, 0, buffer.Length)) > 0)
{
output.Write(buffer, 0, read);
}
}

In other words, you read a chunk at a time into a buffer, and write out
the chunks immediately after you've read each one.
 
Annette Miller said:
Thanks heaps. It works a treat! One question - when declaring the byte
array, why 32 * 1024. Obviously 1024 because it makes a kilobyte, but why by
32?

It was just a simple way of creating a 32K buffer to read into and
write from. It could be any size, but in my experience 32K is a good
balance between speed efficiency (pulling reasonable chunks at a time)
and memory efficiency (not creating a buffer which is too big).
 
Annette said:
Hi All,

I'm just wondering if someone can show me how to write a stream to a
file. Specifically I have a file (bitmap) as an embedded resource in
my app i.e. "MyApp.Resources.BitmapTemplate.bmp" and was wondering
how I can write it to, say "C:\temp.bmp" from the following

Stream stream =
System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("MyApp.Resources.BitmapTemplate.bmp");

you could initialize a Bitmap object with the stream and then call
Save().


Richard
 
Back
Top