如何将png转换为xaml DrawingBrush?

时间:2018-05-23 16:08:13

标签: xaml png

我有一些png文件需要在矢量格式的WPF(xaml)应用程序中用作DrawingBrush。

如何将png转换为xaml DrawingBrush?

1 个答案:

答案 0 :(得分:0)

PNG不是矢量图形,making it one isn't something you can do by setting an attribute。你可以缩小它,但如果你试图将它缩放得更大,你会看到一些神器。而且您不需要将其放在DrawingBrush中以便以任何方式进行缩放。

答案是使用ImageBrush

执行此操作
<Window.Resources>
    <ImageBrush
        x:Key="MyBrush"
        ImageSource="SantaClaus.png" 
        TileMode="Tile" 
        Viewport="0,0,100,100" 
        ViewportUnits="Absolute" 
        Stretch="Fill"
        />
</Window.Resources>

<Grid Background="{StaticResource MyBrush}">
</Grid>

enter image description here

但如果您正在处理设计糟糕的第三方控件,只能使用DrawingBrush,那么您也可以这样做:

    <DrawingBrush 
        x:Key="MyBrush" 
        TileMode="Tile" 
        Viewport="0,0,100,100" 
        ViewportUnits="Absolute" 
        Stretch="Fill"
        >
        <DrawingBrush.Drawing>
            <DrawingGroup>
                <ImageDrawing ImageSource="SantaClaus.png" Rect="0,0,100,100" />
            </DrawingGroup>
        </DrawingBrush.Drawing>
    </DrawingBrush>