【问题标题】:XAML graphic vector as image sourceXAML 图形矢量作为图像源
【发布时间】:2015-11-19 10:06:37
【问题描述】:

现在许多图形设计师将图像提供为 xaml 文件而不是 ico、png、...,我如何使用 xaml 文件作为图像源?

谢谢

【问题讨论】:

  • 你能展示一个 xaml 文件的例子,而不是 ico,png。可能是照片或打印屏幕

标签: wpf xaml


【解决方案1】:

来自the MSDN

<Image>
  <Image.Source>
    <DrawingImage PresentationOptions:Freeze="True">
      <DrawingImage.Drawing>
        <GeometryDrawing>
          <GeometryDrawing.Geometry>
            <GeometryGroup>
              <EllipseGeometry Center="50,50" RadiusX="45" RadiusY="20" />
              <EllipseGeometry Center="50,50" RadiusX="20" RadiusY="45" />
            </GeometryGroup>
          </GeometryDrawing.Geometry>
          <GeometryDrawing.Brush>
            <LinearGradientBrush>
              <GradientStop Offset="0.0" Color="Blue" />
              <GradientStop Offset="1.0" Color="#CCCCFF" />
            </LinearGradientBrush>
          </GeometryDrawing.Brush>
          <GeometryDrawing.Pen>
            <Pen Thickness="10" Brush="Black" />
          </GeometryDrawing.Pen>
        </GeometryDrawing>
      </DrawingImage.Drawing>
    </DrawingImage>
  </Image.Source>
</Image>

您可以将其称为静态资源,保留在 Resourcedictionary 中,而不是内嵌 GeometryDrawing。

<Image>
  <Image.Source>
    <DrawingImage PresentationOptions:Freeze="True" 
                  Drawing="{StaticResource myDrawing}">
  </Image.Source>
</Image>

您还会发现,这种方式只能显示一次绘图。因此,如果您希望此图像出现在 UI 中的多个位置,请将图像放在控件模板中并使用模板设置控件的样式。

【讨论】:

  • 非常感谢,我知道这种方法,我的问题是当我有 100 个设计师提供的 xaml 图形文件时会发生什么,现在我必须从每个文件中提取内容以移动到资源字典中并为每个设置一个密钥。我只问时间是否更简单。
  • @ToaderIoan - 你应该在问题中提到这一点。一切都取决于您要如何存储图纸。您可以编写一个简单的 powershell 脚本,读取字典中的所有 xaml 文件并将它们合并到一个大资源字典文件中,每个图像都以文件名作为键。
【解决方案2】:

非常感谢,我知道这种方法,我的问题是当我有 100 个设计师提供的 xaml 图形文件时会发生什么,现在我必须从每个文件中提取内容以移动到资源字典中并为每个。我只问是否有更简单的方法。

【讨论】:

  • 这不是原始问题的答案。
【解决方案3】:

再次感谢大家。

我得到了这样的图形

<Viewbox xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" Width="128" Height="128">
  <Canvas Width="10240" Height="10240">
	 <Path Data="M5986 6814l828 -828c124,-124 328,-124 452,0l2428 2428c124,124 124,328 0,452l-828 828c-124,124 -328,124 -452,0l-2428 -2428c-124,-124 -124,-328 0,-452z" Fill="#EDC87E"/>
	 <Path Data="M6082 5442l412 412 -640 640 -412 -412c-534,401 -1197,638 -1916,638 -1764,0 -3194,-1430 -3194,-3194 0,-1764 1430,-3194 3194,-3194 1764,0 3194,1430 3194,3194 0,719 -237,1382 -638,1916zm-2556 -4471c-1411,0 -2555,1144 -2555,2555 0,1411 1144,2555 2555,2555 1411,0 2555,-1144 2555,-2555 0,-1411 -1144,-2555 -2555,-2555z" Fill="#808080"/>
  </Canvas>
</Viewbox>

作为内容控件的内容,这对于用户来说非常容易,但是对于需要图像源的控件,我不能将其转换为:

 <DrawingImage>
            <DrawingImage.Drawing>
                <DrawingGroup>
                    <DrawingGroup.Children>
                        <GeometryDrawing  Geometry="M5986 6814l828 -828c124,-124 328,-124 452,0l2428 2428c124,124 124,328 0,452l-828 828c-124,124 -328,124 -452,0l-2428 -2428c-124,-124 -124,-328 0,-452z" Brush="#EDC87E">
                            <GeometryDrawing.Pen>
                                <Pen Thickness="1000" Brush="Blue"></Pen>
                            </GeometryDrawing.Pen>
                        </GeometryDrawing>
                        <GeometryDrawing  Geometry="M6082 5442l412 412 -640 640 -412 -412c-534,401 -1197,638 -1916,638 -1764,0 -3194,-1430 -3194,-3194 0,-1764 1430,-3194 3194,-3194 1764,0 3194,1430 3194,3194 0,719 -237,1382 -638,1916zm-2556 -4471c-1411,0 -2555,1144 -2555,2555 0,1411 1144,2555 2555,2555 1411,0 2555,-1144 2555,-2555 0,-1411 -1144,-2555 -2555,-2555z" Brush="#808080"/>
                    </DrawingGroup.Children>
                </DrawingGroup>
            </DrawingImage.Drawing>
        </DrawingImage>

工作变得微不足道:(

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-11
    • 2013-01-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多