Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

write System.IO.Stream to file in vb.net

  • 14-12-2006 02:10PM
    #1
    Registered Users, Registered Users 2 Posts: 1,000 ✭✭✭


    I am trying to write a System.IO.Stream to a file in vb.net.
    I can get it to write the file to the browser successfully but can't figure out how to write/save it to disk.

    This part is successful:
    objThumbnail.Save(Response.OutputStream, Imaging.ImageFormat.Jpeg)

    But it's the next bit I can't do! Any ideas?

    txs


Comments

  • Closed Accounts Posts: 4,943 ✭✭✭Mutant_Fruit


    First, read the Response.OutputStream into a memorystream so you can use it a few times, call the memorystream memstream (or whatever). Then:

    memstream.Seek(0, SeekOrigin.Begin); // set the stream to the start again
    objThumbnail.Save(Response.OutputStream, Imaging.ImageFormat.Jpeg)

    memstream.Seek(0, SeekOrigin.Begin); // set the stream to the start again
    System.IO.File.WriteAllBytes(memstream.ToArray(), @C:\MyDirectory\MyThumbnail.thumb.whatever);


  • Registered Users, Registered Users 2 Posts: 1,000 ✭✭✭MargeS


    Dim memstream As MemoryStream = Response.OutputStream
            memstream.Seek(0, SeekOrigin.Begin)
            'objThumbnail.Save(Response.OutputStream, Imaging.ImageFormat.Jpeg)
    
            'memstream.Seek(0, SeekOrigin.Begin) 'set the stream to the start again
            File.WriteAllBytes("d:\test.jpg", memstream.ToArray)
    

    This hasn't worked and I have tried many variations.
    I have put it into try/catch and it doesn't even produce an error that I could start from. It does nothing and its the last part of a long sequence of events that all work until this part.
    objThumbnail.Save(Response.OutputStream, Imaging.ImageFormat.Jpeg)
    
    This part on it's own produces a GDI+ error, so I've been trying to figure out a different way to do it.


  • Closed Accounts Posts: 4,943 ✭✭✭Mutant_Fruit


    You need to create a NEW memorystream and then copy the outputstream into that new memorystream, not cast the outputstream as the memorystream.

    psuedocode;
    byte[] buffer = new byte[response.contentlength];
    Response.OutputStream.Read(buffer, 0, response.contentlength);

    File.WriteAllBytes("d:\test.jpg", buffer);

    MemoryStream s = new MemoryStream(buffer);
    s.Seek(0, SeekOrigin.Begin);
    objThumbnail.Save(Response.OutputStream, Imaging.ImageFormat.Jpeg)


Advertisement