【问题标题】:Using Vector images as an image source. WPF , C#使用矢量图像作为图像源。 WPF , C#
【发布时间】:2018-02-02 17:04:50
【问题描述】:

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

我在 illustrator 中创建了矢量图。我在 .ai 项目文件中有它们,我该如何从这里开始?

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

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

【问题讨论】:

    标签: c# wpf svg vector imagesource


    【解决方案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);
        }
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 如何将 Source 属性绑定到 ImageSource 类型的视图模型属性?以及如何获取我的 .ai 文件的几何图形?
      • 请在网上搜索 MVVM 和 WPF 数据绑定。创建视图模型类时,添加 ImageSource 类型的属性。在您的视图模型类中,将您的矢量数据转换为适当的 Greometry 并将其分配给 DrawingImage 的 Geometry 属性。然后从属性 getter 返回 DrawingImage。
      • 当然,使用 Path 元素并将其 Data 属性绑定到 Geometry 可能更简单。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-18
      • 2016-06-17
      • 1970-01-01
      • 2013-01-25
      相关资源
      最近更新 更多