代码之家  ›  专栏  ›  技术社区  ›  Ruben Versavel

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

  •  1
  • Ruben Versavel  · 技术社区  · 7 年前

    我正在做一个包含大量图像的项目。问题是,正如标题所说,我必须先将所有矢量图像转换为png,然后才能将它们用作图像源。

    我在illustrator中创建了矢量图像。我把它们放在里面了。ai项目文件,我如何从这里开始?

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

    <Image Width="70" Height="70" Source="MyVectorImage"/>
    
    2 回复  |  直到 7 年前
        1
  •  3
  •   Tim Diekmann suresh madaparthi    7 年前

    如果你想得到 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);
        }
    
        2
  •  1
  •   Clemens    7 年前

    您可以使用 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。