Click here to Skip to main content
15,893,266 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello! How to save image to MemoryStream and calling from memorystream to Imagebrush!
this is code
C#
bit = new byte[listPictures.Count];
 //bit = File.ReadAllBytes(listPictures[0]);
foreach (string tt in listPictures.ToArray())
                {
                   Uri resourceUri = new Uri(tt, UriKind.Relative);
                   StreamResourceInfo stream_info = Application.GetResourceStream(resourceUri);
//MemoryStream ms = new MemoryStream();
                    //stream_info.Stream.CopyTo(ms);
                    //ms.ToArray();
using (MemoryStream ms = new MemoryStream(bit))
                    {
                        ms.Write(bit, 0, bit.Length);
                                           
                        encoder.Frames.Add(BitmapFrame.Create(stream_info.Stream));
                        //temp = BitmapFrame.Create(ms);
                        //encoder.Save(ms);
                        bit = ms.ToArray();
                        ms.Close();
                    }

}
Posted

1 solution

I guess to want to convert memory stream to imagebrush
C#
using (var stream = new MemoryStream(data))
{
    var bitmap = new BitmapImage();
    bitmap.BeginInit();
    bitmap.StreamSource = stream;
    bitmap.CacheOption = BitmapCacheOption.OnLoad;
    bitmap.EndInit();
    bitmap.Freeze();
}

now you can this bitmapimage as a source to an image.

The BitmapCacheOption.OnLoad is important in this case because otherwise the BitmapImage might try to access the stream when loading on demand and the stream might already be closed.

Freezing the bitmap is optional but if you do freeze it you can share the bitmap across threads which is otherwise impossible.

Reference : http://stackoverflow.com/questions/5346727/convert-memory-stream-to-bitmapimage[^]

Also refer this
https://social.msdn.microsoft.com/Forums/vstudio/en-US/8327dd31-2db1-4daa-a81c-aff60b63fee6/converting-an-imagebitmapimage-object-into-byte-array-and-vice-versa?forum=wpf[^]
 
Share this answer
 
Comments
Muratov 4-Aug-15 0:12am    
thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900