locked
convert BitmapSource to BitmapImage? RRS feed

  • Question

  • Hello,

    I have an Image object in XAML that is databound to a BitmapImage.

    In my program, I take that BitmapImage, run it through an encoder to get a PNG encoded memory stream. I send this memory stream to a SQL database. When I read it back, I encounter the following problem:

    I have the memory stream, and I can make a bitmapsource out of it by running the stream through the decoder.

    How can I convert a BitmapSource to a BitmapImage so I can use the image retrieved from the SQL database in my XAML Image object?

    Thanks.
    Wednesday, April 29, 2009 3:18 PM

Answers

  • Hi wilycoder.

    I have not used the PNG version, but use IconBitmapDecoder to get my memory stream from Icons so I think it is in the same lines.
    I will give you the short version of what I do:

    ImageSource imageSource = myIconBitmapDecoder.Frames[0];
    
    return imageSource; <-- in my converter directly to XAML for 'Source' of 'Image' tag


    or I think in code you can just do a myImage.Source = imageSource
    My Icon has several frames in my icons, so I have to get the correct frame through a converter depending on the size of the placeholder and then I return the ImageSource which nicely is taken in the XAML.

    Hope this helps.

    noorbakhsh
    • Proposed as answer by noorbakhsh Wednesday, April 29, 2009 5:25 PM
    • Marked as answer by Jim Zhou - MSFT Monday, May 4, 2009 12:35 PM
    Wednesday, April 29, 2009 4:06 PM

All replies

  • Hi wilycoder.

    I have not used the PNG version, but use IconBitmapDecoder to get my memory stream from Icons so I think it is in the same lines.
    I will give you the short version of what I do:

    ImageSource imageSource = myIconBitmapDecoder.Frames[0];
    
    return imageSource; <-- in my converter directly to XAML for 'Source' of 'Image' tag


    or I think in code you can just do a myImage.Source = imageSource
    My Icon has several frames in my icons, so I have to get the correct frame through a converter depending on the size of the placeholder and then I return the ImageSource which nicely is taken in the XAML.

    Hope this helps.

    noorbakhsh
    • Proposed as answer by noorbakhsh Wednesday, April 29, 2009 5:25 PM
    • Marked as answer by Jim Zhou - MSFT Monday, May 4, 2009 12:35 PM
    Wednesday, April 29, 2009 4:06 PM
  • Hi noorbaksh,

    Your last piece of advice worked: I ditched the databinding in the XAML and instead assigned Image.source = mybitmapsource in the C# file and it worked!

    thanks :)

    -wilycoder
    Wednesday, April 29, 2009 5:24 PM