1

我一直在尝试使用 Windows 8 和 Metro UI,我已经编写了一个完全合理的位图图像加载,但它似乎没有加载,感谢任何帮助。

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

在此示例中,宽度和高度始终采用零值。它在同一个类中呈现..

    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

这是创建/调用它的地方:

V = New Sprite("Images/Test.bmp")
gameRoot.Children.Clear()
V.Render(100, 100, gameRoot)

===

这不是我的画布对象的问题,因为如果我用它填充它, Sprite.Fill = New SolidColorBrush(Colors.White)它就可以正常工作,并且我在屏幕上看到一个白色方块,有问题的位图设置为“始终复制”,并且位于目标文件夹中当我查看它时。

我不太确定我做错了什么。

4

1 回答 1

0

好吧,我自己设法解决了这个问题。

您可以使用 Me.Uri 获取基本 uri;或在 C# This.Uri 中。

这是完成的代码,任何人都可以将位图图像加载到画布中;甚至旋转它。

这是我自己写的,所以任何发现它有同样问题的人都可以随意使用它。

图片必须采用“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
于 2011-10-21T21:48:10.483 回答