Quantcast
Channel: VBForums - WPF, WCF, WF
Viewing all articles
Browse latest Browse all 256

Issue converting a bitmap to a bitmapsource

$
0
0
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


Viewing all articles
Browse latest Browse all 256

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>