2009-08-22 21 views
6

XAMLベクターイメージをウィンドウの背景にする方法を教えてください。 jpgでこれを示すコードはたくさんありますが、私はベクトルベースの画像を好むでしょう。XAMLイメージをWPFウィンドウの背景として使用する

リソースとして持っていればボーナスになりますが、私は最良のアプローチについては困惑しています。

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
    <Viewbox x:Key="Background2" Stretch="Fill"> 
     <Canvas > 
      <!-- Ebene 1/<Path> --> 
      <Path Fill="#ff000000" Data="F1 M 841.890,595.275 L 0.000,595.275 L 0.000,0.000 L 841.890,0.000 L 841.890,595.275 Z"/> 
      <!-- Ebene 1/<Path> --> 
      <Path Data="F1 M 265.910,218.277 C 265.910,169.332 223.865,129.655 172.000,129.655 C 120.135,129.655 78.090,169.332 78.090,218.277 C 78.090,267.222 120.135,306.898 172.000,306.898 C 223.865,306.898 265.910,267.222 265.910,218.277 Z"> 
       <Path.Fill> 
        <RadialGradientBrush MappingMode="Absolute" GradientOrigin="172.733,217.234" Center="172.733,217.234" RadiusX="81.912" RadiusY="81.912"> 
         <RadialGradientBrush.GradientStops> 
          <GradientStop Offset="0.00" Color="#ff0d4976"/> 
          <GradientStop Offset="0.41" Color="#ff06243b"/> 
          <GradientStop Offset="1.00" Color="#ff000000"/> 
         </RadialGradientBrush.GradientStops> 
         <RadialGradientBrush.Transform> 
          <MatrixTransform Matrix="1.146,0.000,0.000,1.082,-26.038,-16.750" /> 
         </RadialGradientBrush.Transform> 
        </RadialGradientBrush> 
       </Path.Fill> 
      </Path> 
     </Canvas> 
    </Viewbox> 
</ResourceDictionary> 

上記のリソースコードは、Viewboxを削除すると問題なく動作します。このウィンドウのコードは次のとおりです。 -

<Window x:Class="Window2" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="Window2" Height="700" Width="800"> 
     <Window.Resources> 
      <ResourceDictionary Source="Resources/Dictionary2.xaml" /> 
     </Window.Resources> 
     <Grid> 
     <StaticResource ResourceKey="Background2"/> 
     </Grid> 
    </Window> 

答えて

5

この

<Window.Resources> 
    <Canvas x:Key="Background2"> 
     <!-- Ebene 1/<Path> --> 
     <Path Fill="#ff000000" Data="F1 M 841.890,595.275 L 0.000,595.275 L 0.000,0.000 L 841.890,0.000 L 841.890,595.275 Z"/> 
     <!-- Ebene 1/<Path> --> 
     <Path Data="F1 M 265.910,218.277 C 265.910,169.332 223.865,129.655 172.000,129.655 C 120.135,129.655 78.090,169.332 78.090,218.277 C 78.090,267.222 120.135,306.898 172.000,306.898 C 223.865,306.898 265.910,267.222 265.910,218.277 Z"> 
      <Path.Fill> 
       <RadialGradientBrush MappingMode="Absolute" 
          GradientOrigin="172.733,217.234" 
          Center="172.733,217.234" 
          RadiusX="81.912" RadiusY="81.912"> 
        <RadialGradientBrush.GradientStops> 
         <GradientStop Offset="0.00" Color="#ff0d4976"/> 
         <GradientStop Offset="0.41" Color="#ff06243b"/> 
         <GradientStop Offset="1.00" Color="#ff000000"/> 
        </RadialGradientBrush.GradientStops> 
        <RadialGradientBrush.Transform> 
         <MatrixTransform 
          Matrix="1.146,0.000,0.000,1.082,-26.038,-16.750" /> 
        </RadialGradientBrush.Transform> 
       </RadialGradientBrush> 
      </Path.Fill> 
     </Path> 
    </Canvas> 
</Window.Resources> 

<Grid > 
    <Grid.Background> 
     <VisualBrush Stretch="Fill" Visual="{StaticResource Background2}" /> 
    </Grid.Background> 
</Grid> 

をお試しくださいabsの場合は、リソースをリソースディクショナリに移動するためにいくつかの変更を加えなければなりません必ず必要です。

+0

サイモン、私はほとんどこれをあきらめました。あなたのソリューションは初めての作業でした。私は現在、リソースディクショナリに移動することを検討しているので、ユーザーが選択できる背景を選択することができます。再度、感謝します。 – Mitch

1

Illustratorを含む多くのツールで、さまざまな形式のXAMLイメージを書き出すことができます。あなたの理想的な目標は、ベクトル画像を含むCanvasまたはGridパネルを含むResourceDictionaryです。その後、Window.Resourcesで辞書を参照し、画像パネル(CanvasまたはGrid)を最上位のウィンドウパネルに追加するだけです。

だからあなたのイメージの.xamlファイルはこのような何かを見ている必要があります

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
    <Canvas x:Name="MyXamlImage"> 
     ... 
    </Canvas> 
</ResourceDictionary> 

次に、あなたのWindowであなたのようなものが必要です:

<Window x:Class="YourNamespace.YourWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="YourWindow" Height="300" Width="300"> 
    <Window.Resources> 
     <ResourceDictionary Source="MyResourceDictionary.xaml"> 
    </Window.Resources> 
    <Grid> 
     <StaticResource ResourceKey="MyXamlImage"/> 
     ... 
    </Grid> 
</Window> 
+0

こんにちはチャーリー、それはちょっと作品がキャンバスのサイズは固定されており、私はそれをウィンドウでサイズを変更する必要があります。以前は、キャンバスをViewboxにStretch = "Fill"で置き換えてサイズを変更しましたが、あなたのメソッドを使ってこれをどのように適応させるかはわかりません。何か案は? – Mitch

+0

CanvasをViewboxにラップすることはできます。 ViewboxをResourceDictionaryの一番上の要素にして、Canvasの代わりに名前を付けます。 – Charlie

+0

Viewboxを一番上の要素にしようとしましたが、今は表示されません。非常に奇妙な.. – Mitch

関連する問題