# .NET and More > WPF, WCF, WF >  [RESOLVED] Converting bitmap to bitmapimage

## garry79

I'm trying to add a bitmap image to the source property of the Image control but can't find a way to convert the bitmap to bitmapimage. Is there any simple way do do this?

----------


## Hack

_Moved From The CodeBank (which is for sharing code with others rather than posting questions  )_

----------


## boops boops

Hi Garry, you're in luck, I just got this figured out for myself. You can do it in code by saving the bitmap to an IO.MemoryStream as a PNG file and then using BitmapImage.StreamSource. Here's a VB.Net function as an example:

vb Code:
Private Function ToBitmapImage(img As System.Drawing.Image) As BitmapImage
    Dim ms As New IO.MemoryStream
    Dim bi As New BitmapImage
    img.Save(ms, System.Drawing.Imaging.ImageFormat.Png)
    bi.BeginInit()
    bi.StreamSource = ms
    bi.EndInit()
    Return bi
End Function

Don't close the MemoryStream until you no longer need the BitmapImage.

BB

----------


## garry79

> Hi Garry, you're in luck, I just got this figured out for myself. You can do it in code by saving the bitmap to an IO.MemoryStream as a PNG file and then using BitmapImage.StreamSource. Here's a VB.Net function as an example:
> 
> vb Code:
> Private Function ToBitmapImage(img As System.Drawing.Image) As BitmapImage
>     Dim ms As New IO.MemoryStream
>     Dim bi As New BitmapImage
>     img.Save(ms, System.Drawing.Imaging.ImageFormat.Png)
>     bi.BeginInit()
>     bi.StreamSource = ms
> ...


Thanks, Boops! This works great

----------

