Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

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

Options
  • 14-12-2006 3:10pm
    #1
    Registered Users Posts: 1,002 ✭✭✭


    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 Posts: 1,002 ✭✭✭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