Home All Groups Group Topic Archive Search About

Writing Stream to File

Author
6 Oct 2005 10:00 AM
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!

Author
6 Oct 2005 12:23 PM
Cowboy (Gregory A. Beamer) - MVP
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!
***************************


Show quote
"Annette Miller" wrote:

> 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!
>
>
>
Author
6 Oct 2005 9:34 PM
Jon Skeet [C# MVP]
Cowboy (Gregory A. Beamer) - MVP <NoSpamMgbworld@comcast.netNoSpamM>
wrote:

<snip>

> 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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Author
6 Oct 2005 9:35 PM
Jon Skeet [C# MVP]
Annette Miller <ann.miller@nospam.com> wrote:
> 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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Author
6 Oct 2005 11:10 PM
Annette Miller
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.
Show quote
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MPG.1dafa9009d44aecf98c88a@msnews.microsoft.com...
> Annette Miller <ann.miller@nospam.com> wrote:
>> 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.
>
> --
> Jon Skeet - <sk***@pobox.com>
> http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
> If replying to the group, please do not mail me too
Author
7 Oct 2005 5:42 AM
Jon Skeet [C# MVP]
Annette Miller <ann.miller@nospam.com> wrote:
> 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).

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Author
15 Oct 2005 8:12 PM
Richard Grimes [MVP]
Annette Miller wrote:
> 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

AddThis Social Bookmark Button