locked
How to convert stream to bitmapsource and how to convert bitmapimage to bitmapsource in WPF???? RRS feed

  • Question

  • I want to convert stream to bitmapsource  and i also want to convert bitmap image to bitmapsource. i need it urgently plzz help me out.  I don't want to use " Uri " for converting to bitmapsource.
    Friday, October 9, 2009 1:19 PM

Answers

  • The type of the StreamSource property is Stream and FileStream derives from Stream. The method I showed will work for any other type of Stream that supports reading, including MemoryStream, I just chose FileStream for convenience. To write to a MemoryStream, use the following:

    // bmSource is the BitmapSource you want to save.
    PngBitmapEncoder encoder = new PngBitmapEncoder();
    encoder.Frames.Add(BitmapFrame.Create(bmSource));
    MemoryStream stream = new MemoryStream();
    encoder.Save(stream);

    I've used a PNG encoder, but you can use another bitmap encoder.
    • Marked as answer by Bruce.Zhou Thursday, October 15, 2009 1:32 PM
    Friday, October 9, 2009 2:31 PM

All replies

  • Try something like this:

    FileStream fs = File.Open("MyImage.png", FileMode.Open);
    BitmapImage bi = new BitmapImage();
    bi.BeginInit();
    bi.StreamSource = fs;
    bi.EndInit();
    Friday, October 9, 2009 2:11 PM
  • I dont want to give file name. i do not want to give any physical path. What I want is that I have a web cam app I just want to send this images to to other peer via stream i have implemented all that is required i.e I want that the that I have captured from my web cam wants to send it to other peer. Am not storing these pictures any where just capturing and send and this is what I want to do. So plzz tell me how to convert BitmapSource to memory stream and vice versa . i need it urgently
    Friday, October 9, 2009 2:16 PM
  • The type of the StreamSource property is Stream and FileStream derives from Stream. The method I showed will work for any other type of Stream that supports reading, including MemoryStream, I just chose FileStream for convenience. To write to a MemoryStream, use the following:

    // bmSource is the BitmapSource you want to save.
    PngBitmapEncoder encoder = new PngBitmapEncoder();
    encoder.Frames.Add(BitmapFrame.Create(bmSource));
    MemoryStream stream = new MemoryStream();
    encoder.Save(stream);

    I've used a PNG encoder, but you can use another bitmap encoder.
    • Marked as answer by Bruce.Zhou Thursday, October 15, 2009 1:32 PM
    Friday, October 9, 2009 2:31 PM
  • Can you illustrate a way to initialize bmSource?
    Thursday, February 6, 2014 7:51 PM
  • In your code, you do not dispose fs, so resources of fs keep in memory, it is an issue.

    But If use

    <pre class="prettyprint">BitmapImage bi = new BitmapImage();
    using (FileStream fs = File.Open("MyImage.png", FileMode.Open))
    {
        bi.BeginInit();
        bi.StreamSource = fs;
        bi.EndInit();
    }
    
    
    

    then this bi do not work.

    How to solve this issue?



    HJY ---try to be happy in any time---

    Tuesday, December 10, 2019 11:55 AM