-
Jul 3rd, 2014, 10:09 AM
#1
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
-
Jul 5th, 2014, 03:30 PM
#2
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
Last edited by boops boops; Jul 5th, 2014 at 03:44 PM.
-
Jul 5th, 2014, 08:56 PM
#3
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|