任何人都可以解释为什么Stretch
在Ellipse
元素中无效。无论我使用None
,Fill
,UniformToFill
还是Uniform
,结果始终是相同的。
如果我在DataTemplate
(GridView
)中使用相同的代码,那么它将正常工作。
<Button x:Name="UI_Application_LogIn_ProfilePictureButton_Button" Style="{StaticResource LogInButton}" BorderBrush="{ThemeResource SystemControlForegroundBaseMediumBrush}" IsTabStop="False" UseSystemFocusVisuals="False">
<Grid x:Name="UI_Application_LogIn_ProfilePicture_Grid" IsHitTestVisible="False">
<Ellipse x:Name="UI_Application_LogIn_ProfilePicture" Width="160" Height="160">
<Ellipse.Fill>
<ImageBrush x:Name="UI_Application_LogIn_ProfilePictureImageBrush" Stretch="UniformToFill" AlignmentY="Top"/>
</Ellipse.Fill>
</Ellipse>
<Ellipse x:Name="UI_Application_LogIn_ProfilePictureNonStaticLightEffect" Width="160" Height="160" Fill="{ThemeResource SystemControlHighlightTransparentRevealBorderBrush}"/>
</Grid>
</Button>
答案 0 :(得分:1)
如果您想创建一个四舍五入的图像,请使用 windows社区工具包
中的Imagex控件您可以查找并使用imagex here进行游戏 或者您可以通过 Microsoft.Toolkit.Uwp.UI.Controls 上的nuget引用您的应用程序 对于您当前的问题,这是因为拉伸仅会影响日食内部的笔刷,而不会影响日食本身,因此为了进行比例调整,您必须将日食包裹在 Viewbox 上控件,然后以与在图像上相同的方式设置其 Stretch 属性
答案 1 :(得分:1)
我发现了拉伸无效的原因。真的是脑子屁。创建新的ImageBrush时,我忘记在代码中使用Stretch。因此,在创建新的ImageBrush之后添加行if (BrushStretch != null) (TargetShape.Fill as ImageBrush).Stretch = BrushStretch.Value;
可以解决此问题。
//STORAGE FILE TO SHAPE
public static async Task<Shape> StorageFileToShape(Shape TargetShape, StorageFile SourceStorageFile, Stretch? BrushStretch, AlignmentX? BrushAlignmentX, AlignmentY? BrushAlignmentY)
{
//IF SHAPE NULL RETURN NULL
if (TargetShape == null) return null;
//IF STORAGEFILE NULL OR NOT AVAILABLE RETURN NULL
if (SourceStorageFile == null || !SourceStorageFile.IsAvailable) return null;
//IF BRUSH IS NULL OR ITS TYPE ISN'T 'ImageBrush' CREATE NEW BRUSH
if (TargetShape.Fill == null || TargetShape.Fill.GetType() != typeof(ImageBrush)) TargetShape.Fill = new ImageBrush();
//SET STRETCH
if (BrushStretch != null) (TargetShape.Fill as ImageBrush).Stretch = BrushStretch.Value;
//SET ALIGNMENT X
if (BrushAlignmentX != null) (TargetShape.Fill as ImageBrush).AlignmentX = BrushAlignmentX.Value;
//SET ALIGNMENT Y
if (BrushAlignmentY != null) (TargetShape.Fill as ImageBrush).AlignmentY = BrushAlignmentY.Value;
//GET PICTURE
(TargetShape.Fill as ImageBrush).ImageSource = await StorageFileToBitmapImage(SourceStorageFile);
//SET SHAPE FILL
TargetShape.Fill = TargetShape.Fill as ImageBrush;
//RETURN SHAPE
return TargetShape;
}
//STORAGE FILE TO BITMAP IMAGE
public static async Task<BitmapImage> StorageFileToBitmapImage(StorageFile SourceStorageFile)
{
BitmapImage TargetBitmapImage = new BitmapImage();
TargetBitmapImage.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
using (var BitmapImageFileStream = await SourceStorageFile.OpenAsync(FileAccessMode.Read))
{
await TargetBitmapImage.SetSourceAsync(BitmapImageFileStream);
}
return TargetBitmapImage;
}