how to read the whole file directly to a stream?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mina2040
    New Member
    • Dec 2008
    • 6

    how to read the whole file directly to a stream?

    I know that I can read the whole file into a byte array and then convert the array to a stream using the following two lines:

    Code:
    byte[] fileInBytes = File.ReadAllBytes(Filename);
    Stream fileInStream = new MemoryStream(fileInBytes);
    But this is not efficient for my code (my code should be very fast and memory efficient). Is there any way to do this straight away without a conversion? or any other better way?
  • Christian Binder
    Recognized Expert New Member
    • Jan 2008
    • 218

    #2
    File.Open() returns a Stream (FileStream).

    Comment

    • mina2040
      New Member
      • Dec 2008
      • 6

      #3
      Thanks Christian,

      It seems that my question wasn't clear enough.
      I need to have the whole content of the file in Stream format because then I can pass it to a bitmap like this:

      Code:
      Bitmap Origbmp = new Bitmap(fileInStream);
      File.Open doean't retrun a stream that contain the content of the whole file.

      Comment

      • Christian Binder
        Recognized Expert New Member
        • Jan 2008
        • 218

        #4
        Why do you have to have the whole content in a Stream? You can read/write from/into a stream like it was an array or something like that.
        If you need all content read before, so your first approach would be fine.
        Code:
        byte[] fileInBytes = File.ReadAllBytes(Filename);
        Stream fileInStream = new MemoryStream(fileInBytes);

        Comment

        Working...