Results 1 to 4 of 4

Thread: [RESOLVED] Converting bitmap to bitmapimage

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jun 2009
    Posts
    230

    Resolved [RESOLVED] Converting bitmap to bitmapimage

    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?

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Converting bitmap to bitmapimage

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

  3. #3
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Re: Converting bitmap to bitmapimage

    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:
    1. Private Function ToBitmapImage(img As System.Drawing.Image) As BitmapImage
    2.     Dim ms As New IO.MemoryStream
    3.     Dim bi As New BitmapImage
    4.     img.Save(ms, System.Drawing.Imaging.ImageFormat.Png)
    5.     bi.BeginInit()
    6.     bi.StreamSource = ms
    7.     bi.EndInit()
    8.     Return bi
    9. End Function

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

    BB

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jun 2009
    Posts
    230

    Re: Converting bitmap to bitmapimage

    Quote Originally Posted by boops boops View Post
    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:
    1. Private Function ToBitmapImage(img As System.Drawing.Image) As BitmapImage
    2.     Dim ms As New IO.MemoryStream
    3.     Dim bi As New BitmapImage
    4.     img.Save(ms, System.Drawing.Imaging.ImageFormat.Png)
    5.     bi.BeginInit()
    6.     bi.StreamSource = ms
    7.     bi.EndInit()
    8.     Return bi
    9. End Function

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

    BB
    Thanks, Boops! This works great

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width