使用矢量图像作为图像源。 WPF,C#

时间:2018-02-02 17:04:51

标签: c# wpf svg vector imagesource

我正在开展包含大量图片的项目。事情是,就像标题所说,我必须将我的所有矢量图像转换为png才能将它们用作图像源。

我有以插画家创建的矢量图像。我把它们放在.ai项目文件中,我怎么离开这里?

我的目标是能够做到这一点:

<Image Width="70" Height="70" Source="MyVectorImage"/>

2 个答案:

答案 0 :(得分:1)

您可以使用DrawingImage作为图片的来源。 DrawingImage使用几何(即矢量图形)而不是位图。

<Image>
    <Image.Source>
        <DrawingImage>
            <DrawingImage.Drawing>
                <GeometryDrawing>
                    <GeometryDrawing.Pen>
                        <Pen Thickness="2" Brush="Black"/>
                    </GeometryDrawing.Pen>
                    <GeometryDrawing.Geometry>
                        <RectangleGeometry Rect="100,100,100,100"/>
                    </GeometryDrawing.Geometry>
                </GeometryDrawing>
            </DrawingImage.Drawing>
        </DrawingImage>
    </Image.Source>
</Image>

您当然也可以将Source属性绑定到ImageSource类型的视图模型属性,该属性包含您将从原始矢量数据创建的DrawingImage。

答案 1 :(得分:1)

如果您想将几何路径图像中的ImageSource放入cs文件中,可以将drawingImage放入资源文件中:Icons.xaml

<DrawingImage x:Key="hazelnut">
    <DrawingImage.Drawing>
        <DrawingGroup>
            <GeometryDrawing Brush="Black">
                <GeometryDrawing.Geometry>
                    <GeometryGroup>
                        <PathGeometry Figures="M 16.6309,18.6563C 17.1309,8.15625 29.8809,14.1563 29.8809,14.1563C 30.8809,11.1563 34.1308,11.4063 34.1308,11.4063C 33.5,12 34.6309,13.1563 34.6309,13.1563C 32.1309,13.1562 31.1309,14.9062 31.1309,14.9062C 41.1309,23.9062 32.6309,27.9063 32.6309,27.9062C 24.6309,24.9063 21.1309,22.1562 16.6309,18.6563 Z M 16.6309,19.9063C 21.6309,24.1563 25.1309,26.1562 31.6309,28.6562C 31.6309,28.6562 26.3809,39.1562 18.3809,36.1563C 18.3809,36.1563 18,38 16.3809,36.9063C 15,36 16.3809,34.9063 16.3809,34.9063C 16.3809,34.9063 10.1309,30.9062 16.6309,19.9063 Z ">
                            <PathGeometry.Transform>
                                <TransformGroup>
                                    <ScaleTransform
                                        CenterX="23"
                                        CenterY="20"
                                        ScaleX="0.45"
                                        ScaleY="0.45" />
                                </TransformGroup>
                            </PathGeometry.Transform>
                        </PathGeometry>
                    </GeometryGroup>
                </GeometryDrawing.Geometry>
            </GeometryDrawing>
        </DrawingGroup>
    </DrawingImage.Drawing>
</DrawingImage>

然后添加App.xaml

<Application.Resources>
    <ResourceDictionary>

        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/Resources/Icons.xaml"/>
        </ResourceDictionary.MergedDictionaries>

    </ResourceDictionary>
</Application.Resources>

并在您的cs文件中使用它,如:

    ImageSource image = ResourcePathToImageSource("hazelnut");


    private ImageSource ResourcePathToImageSource(string resourcesName)
    {
        return (DrawingImage)Application.Current.TryFindResource(resourcesName);
    }