I've been experimenting with Windows 8 and Metro UI, I've written a perfectly reasonable load of a bitmap image, but it doesn't seem to be loading, any help is appreciated.
Public Sub New(Image As String)
Debug.Print("ms-resource://MyAssembly/" & Image)
Img = New ImageBrush()
Dim Bitmap As New BitmapImage()
Bitmap.UriSource = New Uri("ms-resource://MyAssembly/" & Image, UriKind.Absolute)
Img.ImageSource = Bitmap
Width = Bitmap.PixelWidth
Height = Bitmap.PixelHeight
Debug.Print("Height: " & Height & " Width: " & Width)
End Sub
In this example Width and Height always take values of zero. It is rendered in the same class..
Public Sub Render(X As Integer, Y As Integer, Canv As Canvas)
Dim Sprite As New Shapes.Rectangle
Sprite.Width = CDbl(Width)
Sprite.Height = CDbl(Height)
Sprite.Fill = Img
Sprite.SetValue(Canvas.TopProperty, CDbl(Y))
Sprite.SetValue(Canvas.LeftProperty, CDbl(X))
Canv.Children.Add(Sprite)
End Sub
End Class
And here is where it is created/called:
V = New Sprite("Images/Test.bmp")
gameRoot.Children.Clear()
V.Render(100, 100, gameRoot)
===
it isn't a problem with my canvas object, because if i fill it with Sprite.Fill = New SolidColorBrush(Colors.White)
then it works fine, and I see a white square on my screen, the bitmap in question is set to "Copy Always", and is in the destination folder when i view it.
I'm not quite sure what I'm doing wrong.
Well I managed to figure this out myself.
You can get a base uri by using Me.Uri; or in C# This.Uri.
Here is the finished code that will enable anyone to load a bitmap image into a canvas; and even rotate it.
I wrote this myself, so anyone who discovers it having the same problem can feel free to use it as they please.
Image must take the format "FilePath/Image.extension"
Public Class Sprite
'Public Shared Baseuri As Uri
Public Img As ImageBrush
Public Bitmap As BitmapImage
Public Sub New(Image As String, Base As Uri)
'Debug.Print("BASE:" & Base.ToString)
'Debug.Print("USING:" & Image)
'Debug.Print("RESULT:" & New Uri(Base, Image).ToString)
Img = New ImageBrush()
Bitmap = New BitmapImage(New Uri(Base, Image))
End Sub
Public Overloads Sub Render(X As Integer, Y As Integer, Width As Integer, Height As Integer, Canv As Canvas)
Img.ImageSource = Bitmap
Dim Sprite As New Shapes.Rectangle
Sprite.Width = CDbl(Width)
Sprite.Height = CDbl(Height)
Sprite.Fill = Img
Sprite.SetValue(Canvas.TopProperty, CDbl(Y))
Sprite.SetValue(Canvas.LeftProperty, CDbl(X))
Canv.Children.Add(Sprite)
End Sub
Public Overloads Sub Render(X As Integer, Y As Integer, Width As Integer, Height As Integer, Canv As Canvas, Angle As Integer)
Img.ImageSource = Bitmap
Dim Sprite As New Shapes.Rectangle
Sprite.Width = CDbl(Width)
Sprite.Height = CDbl(Height)
Sprite.Fill = Img
Sprite.SetValue(Canvas.TopProperty, CDbl(Y))
Sprite.SetValue(Canvas.LeftProperty, CDbl(X))
Dim v As New Windows.UI.Xaml.Media.RotateTransform
v.Angle = CDbl(Angle)
v.CenterX = Width / 2
v.CenterY = Height / 2
Sprite.RenderTransform = v
Canv.Children.Add(Sprite)
End Sub
End Class