Results 1 to 3 of 3

Thread: Issue converting a bitmap to a bitmapsource

  1. #1

    Thread Starter
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,712

    Issue converting a bitmap to a bitmapsource

    I'm converting a Windows Form Application to a WPF Browser Application. Currently I'm having issues converting my bitmaps that I have stored in My.Resources to BitmapSource. This is what I've tried:
    Code:
    Private Shared Function ConvertBitmap(ByVal source As System.Drawing.Bitmap) As BitmapSource
        Return System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(source.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions())
    End Function
    However, whenever I try to run my program... the program won't execute properly(I'm using Win7 64 Bit so I'm assuming that it's probably eating the exception). So I've tried wrapping a try/catch in my Loaded event and even then no exception appears:
    Code:
    Private Sub Grid1_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Grid1.Loaded
        Try
            m = New Map
    
            With m
                .CurrentPosition = New Point(3, 6)
                .HorizontalAlignment = Windows.HorizontalAlignment.Stretch
                .ImageKeys.Add(1, ConvertBitmap(My.Resources.water))
                .ImageKeys.Add(2, ConvertBitmap(My.Resources.grass))
                .ImageKeys.Add(3, ConvertBitmap(My.Resources.dirt))
                .PassableKeys.Add(1, False)
                .PassableKeys.Add(2, True)
                .PassableKeys.Add(3, True)
                .VerticalAlignment = Windows.VerticalAlignment.Stretch
            End With
    
            m.LoadMap(IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "map1.txt"), _
                      ","c)
    
            Me.Grid1.Children.Add(m)
        Catch ex As Exception
            Console.WriteLine(ex.ToString)
        End Try
    End Sub
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

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

    Re: Issue converting a bitmap to a bitmapsource

    Hi dday,

    I doubt if it's a question of exception swallowing. The "Win64 bug" applies only to the Form Load event; and if it occurs, the exception isn't thrown so you can't catch it anyway. But apparently you can overcome that in Visual Studio by selecting Debug/Exceptions ... and ticking the Thrown checkbox for CLR exceptions (see here). I just mention this so you could try that checkbox in case it does help!

    Apart from that, maybe it would help to explain what you mean by doesn't execute "properly". What happens or doesn't happen?

    EDIT: by the way, I tried your ConvertBitmap method and it works fine. Duly noted for future use. But I know nothing about your Map class.

    BB

  3. #3

    Thread Starter
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,712

    Re: Issue converting a bitmap to a bitmapsource

    I'm not at my computer that has that project so I'll have to post back later, but it's essentially this only in WPF form. So some changes, like changing Bitmap to BitmapSource and a few others are done.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

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