Windows运行时:如何在椭圆形按钮中设置图像?
Windows Runtime: How to set image in a ellipse shape button?
<Button x:Name="btnProfilePicture" HorizontalAlignment="Center" Click="btnProfilePicture_Click">
<Button.Template>
<ControlTemplate>
<Ellipse x:Name="ellipsePicture" Fill="Turquoise" Width="150" Height="150" Stroke="White" StrokeThickness="10">
</Ellipse>
</ControlTemplate>
</Button.Template>
</Button >
我有一个椭圆形的按钮,默认情况下有颜色填充。
我想在运行时的代码隐藏中将填充更改为图像。
我该怎么做?
更多信息:
我尝试使用 "ellipsePicture" 名称设置椭圆填充,但在后面的代码中无法引用此名称,但我不知道原因。
试试这个:
<Button x:Name="btnProfilePicture" HorizontalAlignment="Center" Click="btnProfilePicture_Click">
<Button.Template>
<ControlTemplate>
<Ellipse x:Name="ellipsePicture" Width="150" Height="150" Stroke="White" StrokeThickness="10">
<Ellipse.Fill>
<ImageBrush ImageSource="/DessCol;component/Images/Recommencer.ico"/>
</Ellipse.Fill>
</Ellipse>
</ControlTemplate>
</Button.Template>
试试吧。在此修改后,您可以将图像设置为内容。您可以在 XAML 和运行时进行此更改。
<Button Content="C:\Round.JPG">
<Button.Template>
<ControlTemplate>
<Ellipse x:Name="ellipsePicture" Width="150" Height="150" Stroke="White" StrokeThickness="10">
<Ellipse.Fill>
<ImageBrush ImageSource="{Binding RelativeSource={RelativeSource AncestorType={x:Type Button}},
Path=Content}"/>
</Ellipse.Fill>
</Ellipse>
</ControlTemplate>
</Button.Template>
</Button>
您可以使用按钮的背景 属性 在 background.You 上设置图像,也可以从后面的代码中使用它,例如:
public BitmapImage LoadBackgroundImage(string fileName)
{
var image = new BitmapImage();
try
{
image.BeginInit();
if (!string.IsNullOrEmpty(fileName) && File.Exists(fileName))
{
var bytes = File.ReadAllBytes(fileName);
image.StreamSource = new MemoryStream(bytes);
}
else
{
var bytes = File.ReadAllBytes(Path.GetFullPath(Properties.Resources.DefaultBackgroundImage));
image.StreamSource = new MemoryStream(bytes);
}
image.CacheOption = BitmapCacheOption.OnLoad;
image.EndInit();
image.Freeze();
}
catch (FileNotFoundException ex)
{
throw ex;
}
return image;
}
在您的按钮点击事件中添加以下行
btnProfilePicture.Background=LoadBackgroundImage(yourfilename.jpg);
//你可以使用 .jpg,.jpeg,*.png
试试下面的代码..
<Button x:Name="BtnProfilePicture" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10">
<Button.Template>
<ControlTemplate>
<Grid Height="100">
<Ellipse x:Name="ellipsePicture" Fill="{TemplateBinding Background}"/>
</Grid>
</ControlTemplate>
</Button.Template>
</Button >
在您的代码后面添加以下行..
BtnProfilePicture.BackgroundImage =
new ImageBrush { ImageSource = LoadBackgroundImage(yourfilename.jpg)};
如果可行,请投票给我!
<Button x:Name="btnProfilePicture" HorizontalAlignment="Center" Click="btnProfilePicture_Click">
<Button.Template>
<ControlTemplate>
<Ellipse x:Name="ellipsePicture" Fill="Turquoise" Width="150" Height="150" Stroke="White" StrokeThickness="10">
</Ellipse>
</ControlTemplate>
</Button.Template>
</Button >
我有一个椭圆形的按钮,默认情况下有颜色填充。 我想在运行时的代码隐藏中将填充更改为图像。
我该怎么做?
更多信息: 我尝试使用 "ellipsePicture" 名称设置椭圆填充,但在后面的代码中无法引用此名称,但我不知道原因。
试试这个:
<Button x:Name="btnProfilePicture" HorizontalAlignment="Center" Click="btnProfilePicture_Click">
<Button.Template>
<ControlTemplate>
<Ellipse x:Name="ellipsePicture" Width="150" Height="150" Stroke="White" StrokeThickness="10">
<Ellipse.Fill>
<ImageBrush ImageSource="/DessCol;component/Images/Recommencer.ico"/>
</Ellipse.Fill>
</Ellipse>
</ControlTemplate>
</Button.Template>
试试吧。在此修改后,您可以将图像设置为内容。您可以在 XAML 和运行时进行此更改。
<Button Content="C:\Round.JPG">
<Button.Template>
<ControlTemplate>
<Ellipse x:Name="ellipsePicture" Width="150" Height="150" Stroke="White" StrokeThickness="10">
<Ellipse.Fill>
<ImageBrush ImageSource="{Binding RelativeSource={RelativeSource AncestorType={x:Type Button}},
Path=Content}"/>
</Ellipse.Fill>
</Ellipse>
</ControlTemplate>
</Button.Template>
</Button>
您可以使用按钮的背景 属性 在 background.You 上设置图像,也可以从后面的代码中使用它,例如:
public BitmapImage LoadBackgroundImage(string fileName)
{
var image = new BitmapImage();
try
{
image.BeginInit();
if (!string.IsNullOrEmpty(fileName) && File.Exists(fileName))
{
var bytes = File.ReadAllBytes(fileName);
image.StreamSource = new MemoryStream(bytes);
}
else
{
var bytes = File.ReadAllBytes(Path.GetFullPath(Properties.Resources.DefaultBackgroundImage));
image.StreamSource = new MemoryStream(bytes);
}
image.CacheOption = BitmapCacheOption.OnLoad;
image.EndInit();
image.Freeze();
}
catch (FileNotFoundException ex)
{
throw ex;
}
return image;
}
在您的按钮点击事件中添加以下行
btnProfilePicture.Background=LoadBackgroundImage(yourfilename.jpg); //你可以使用 .jpg,.jpeg,*.png
试试下面的代码..
<Button x:Name="BtnProfilePicture" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10">
<Button.Template>
<ControlTemplate>
<Grid Height="100">
<Ellipse x:Name="ellipsePicture" Fill="{TemplateBinding Background}"/>
</Grid>
</ControlTemplate>
</Button.Template>
</Button >
在您的代码后面添加以下行..
BtnProfilePicture.BackgroundImage =
new ImageBrush { ImageSource = LoadBackgroundImage(yourfilename.jpg)};
如果可行,请投票给我!