0

I am having issues understanding the proper way I am supposed to use files loaded in memory streams or memory mapped files in conjunction to methods that only accept string file paths.

Let's say I am writing a file to memory stream like this:

using (MemoryStream ms = new MemoryStream())
        using (FileStream file = new FileStream("path_to_my_external_file.jpg", FileMode.Open, FileAccess.Read))
        {
            byte[] bytes = new byte[file.Length];
            file.Read(bytes, 0, (int)file.Length);
            ms.Write(bytes, 0, (int)file.Length);  
        }

How would I be able to use the memory stream then with a function that only accepts a string file path as a parameter like GenericFunction (string filePath)?

IneedHelp
  • 1,630
  • 1
  • 27
  • 58
  • There isn't. If `GenericFunction` only needs a `Stream` it should provide such an overload. Your only option is to write to a file and then pass the path to the method so it can read it. – Mike Zboray Feb 06 '15 at 05:39
  • so if I were to create a new BitmapImage which only accepts a Uri, the only way to load an image residing within the memory stream is by saving the image to an actual file on the hard disk? – IneedHelp Feb 06 '15 at 05:43
  • In that case, use the default constructor of `BitmapImage` and set the `StreamSource` property to whatever source stream you want to use. – Mike Zboray Feb 06 '15 at 05:46
  • Aha.. I understand. So in conclusion, there is no way to use a memory loaded file as a regular disk file, unless the function that requires the file is capable of reading streams directly. Thank you for clarifying. – IneedHelp Feb 06 '15 at 05:49
  • An alternative is to have a byte[] array with accesors. And convert the array to a memory stream inside your required function : http://stackoverflow.com/questions/4736155/how-do-i-convert-byte-to-stream-in-c – Kavindu Dodanduwa Feb 06 '15 at 05:50
  • The issue is that I can not modify the function that I need to use, it is coded to accept only a file path, and I wanted to be able to pass it a file loaded in memory without having to write it to disk first. – IneedHelp Feb 06 '15 at 05:52

0 Answers0