[UWP] [C#]设置相对于窗口的精灵表大小 - C#UWP XAML [英] [UWP][C#]Set sprite sheet size relative to window - C# UWP XAML
问题描述
我只是在学习使用精灵工作,我无法缩小实际的精灵表,以便相对于其容器
更改大小,为了演示,它可以是整个窗口。我可以改变矩形("recty")本身的大小,但不能改变精灵表。这是我正在使用的XAML:
<RelativePanel x:Name="myRelativePanel" >
<Rectangle Width="500" x:Name="recty" RelativePanel.AlignRightWithPanel="True" RelativePanel.AlignLeftWithPanel="True" RelativePanel.AlignTopWithPanel="True" RelativePanel.AlignBottomWithPanel="True">
<Rectangle.Fill>
<ImageBrush AlignmentX="Left" AlignmentY="Top" ImageSource="/Images/Hut_SpriteSheet-01a.png" Stretch="None">
<ImageBrush.Transform>
<TranslateTransform x:Name="SpriteSheetOffset" X="-0" Y="0" />
</ImageBrush.Transform>
</ImageBrush>
</Rectangle.Fill>
</Rectangle>
<Button x:Name="animateMe" Click="animateMe_Click"/>
</RelativePanel>
我想在C#中设置ImageBrush的ImageSource的大小,但我不知道如何访问该属性或者它是否存在。任何帮助都是
非常感谢!
推荐答案
嗨 IconTroubles,
Hi IconTroubles,
>>我想在C#中设置ImageBrush的ImageSource的大小。
>>I want to set the size of the ImageBrush's ImageSource in C#.
当你设置
ImageBrush.ImageSource ,你应该给它一个 ImageSource object,更常见的是创建一个
BitmapImage 并将其用作值,然后您可以使用 DecodePixelWidth 和 DecodePixelHeight 属性
来设置Image的大小。以下是C#中的代码
When you set the ImageBrush.ImageSource in the code, you should give it a ImageSource object, it's more common to create a BitmapImage and use that as a value, then you can use the DecodePixelWidth and DecodePixelHeight property to set the size of the Image. The following is the code
与您的上述XAML代码相对应。
in C# witch is corresponding to your above XAML code .
public MainPage()
{
this.InitializeComponent();
RelativePanel myRelativePanel = new RelativePanel();
Rectangle rect = new Rectangle() { Width = 500 };
RelativePanel.SetAlignRightWithPanel(rect, true);
RelativePanel.SetAlignLeftWithPanel(rect, true);
RelativePanel.SetAlignTopWithPanel(rect, true);
RelativePanel.SetAlignBottomWithPanel(rect, true);
ImageBrush brush = new ImageBrush();
brush.AlignmentX = AlignmentX.Left;
brush.AlignmentY = AlignmentY.Top;
brush.Stretch = Stretch.None;
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.UriSource = new Uri(this.BaseUri, "Images/Hut_SpriteSheet-01a.png");
bitmapImage.DecodePixelWidth = 30;
bitmapImage.DecodePixelHeight = 30;
brush.ImageSource = bitmapImage;
rect.Fill = brush;
myRelativePanel.Children.Add(rect);
this.Content = myRelativePanel;
}
祝你好运,
Breeze
这篇关于[UWP] [C#]设置相对于窗口的精灵表大小 - C#UWP XAML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!