|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Writing Stream to FileHi 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. -- Show quoteGregory A. Beamer MVP; MCP: +I, SE, SD, DBA *************************** Think Outside the Box! *************************** "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! > > > Cowboy (Gregory A. Beamer) - MVP <NoSpamMgbworld@comcast.netNoSpamM>
wrote: <snip> > It is also All Streams are binary - they're specifically for binary data rather > possible you will have to use a BinaryStream to guarantee the file is written > out as binary and not ascii. 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 Annette Miller <ann.miller@nospam.com> wrote:
> Hi All, You need something like:> > 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"); 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 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 Annette Miller <ann.miller@nospam.com> wrote:
> Thanks heaps. It works a treat! One question - when declaring the byte It was just a simple way of creating a 32K buffer to read into and > array, why 32 * 1024. Obviously 1024 because it makes a kilobyte, but why by > 32? 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 Annette Miller wrote:
> Hi All, you could initialize a Bitmap object with the stream and then call > > 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"); Save(). Richard -- http://www.grimes.demon.co.uk/workshops/fusionWS.htm http://www.grimes.demon.co.uk/workshops/securityWS.htm
Other interesting topics
|
|||||||||||||||||||||||