locked
Saving a InMemoryAccessStream as jpg in Windows Store app

    Question

  • Hi,

    I am having a InMemoryStream of one JPEG image.

    I want to save this stream to local application data folder as a jpeg file.

    Searched many of the class's in MSDN could not find the apt one.

    Please share the sample code to save InMemoryStream as jpeg file.

    Thanks,

    Bhash

    Sunday, March 30, 2014 3:36 PM

Answers

  • Hi,

    The InMemoryRandomAccessStream has a method of GetInputStreamAt which can give you an IInputStream. And Windows.Storage.Stream also includes a DataReader class that you create based on an IInputStream object and then read numerous .NET objects from the stream as well as arrays of bytes.

    http://www.charlespetzold.com/blog/2011/11/080203.html , http://msdn.microsoft.com/library/windows/apps/BR208119

    DataReader dataReader = new DataReader(inputStream);
    await dataReader.LoadAsync((uint)size);
    byte[] buffer = new byte[(int)size];
    dataReader.ReadBytes(buffer);

    Now you have a buffer containing all the read bytes. Then you can now save this buffer to a file http://blog.jerrynixon.com/2012/06/windows-8-how-to-read-files-in-winrt.html

    var file = await Windows.Storage.ApplicationData.Current.LocalFolder.CreateFileAsync("MyWav.wav", Windows.Storage.CreationCollisionOption.ReplaceExisting);
    await Windows.Storage.FileIO.WriteBytesAsync(file, buffer);
    Best Wishes!



    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey. Thanks<br/> MSDN Community Support<br/> <br/> Please remember to &quot;Mark as Answer&quot; the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    • Marked as answer by BhashTech Tuesday, April 1, 2014 4:31 AM
    Monday, March 31, 2014 2:24 AM