如何在WPF C#中动态包含基于矢量的图像资源

时间:2015-04-08 02:43:33

标签: c# wpf xaml canvas vector

我在运行时动态创建一个按钮,并在按钮上添加一个图像作为内容(star.png)。下面的代码效果很好,但这是交易。我想要像素化图像上的矢量化图像。我可以在XAML中做到这一点,但我不知道如何通过C#在我的.cs文件中动态地重现这个。这是我的工作像素化星形图像的代码。

在我的MainWindow.xaml.cs

while (sqlite_datareader.Read())
{
     System.Windows.Controls.Button starBtn = new System.Windows.Controls.Button();
     Uri resourceUri = new Uri("Images/star.png", UriKind.Relative); //HERE I WANT TO ADD THE CANVAS OF THE XAML AS IMAGE
     StreamResourceInfo streamInfo = System.Windows.Application.GetResourceStream(resourceUri);
     BitmapFrame temp = BitmapFrame.Create(streamInfo.Stream);
     var brush = new ImageBrush();
     brush.ImageSource = temp;
     starBtn.Width = 24;
     starBtn.Height = 24;
     starBtn.Style = (Style)FindResource("StarButtonStyle");
     starBtn.Background = brush;
     splMain.Children.Add(starBtn);
}

在我的App.xaml内部

    <ControlTemplate x:Key="StarBlue">
        <Viewbox Stretch="Uniform">
            <Canvas Name="Layer_1" Width="300" Height="300" ClipToBounds="True" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
                <Canvas>
                    <Path Fill="#FF44B39B">
                        <Path.Data>
                            <PathGeometry FillRule="Nonzero" Figures="M150.52,15.398C75.958,15.398 15.519,75.84 15.519,150.399 15.519,224.954 75.958,285.402 150.52,285.402 225.077,285.402 285.521,224.954 285.521,150.399 285.521,75.84 225.076,15.398 150.52,15.398z M150.52,266.312C86.502,266.312 34.607,214.415 34.607,150.399 34.607,86.383 86.502,34.49 150.52,34.49 214.533,34.49 266.43,86.384 266.43,150.399 266.43,214.414 214.532,266.312 150.52,266.312z" />
                        </Path.Data>
                    </Path>
                    <Path Fill="#FF44B39B">
                        <Path.Data>
                            <PathGeometry FillRule="Nonzero" Figures="M254.947,150.401C254.947,208.073 208.191,254.825 150.519,254.825 92.842,254.825 46.089,208.073 46.089,150.401 46.089,92.725 92.842,45.969 150.519,45.969 208.191,45.969 254.947,92.725 254.947,150.401z" />
                        </Path.Data>
                    </Path>
                    <Path Fill="#FFFFFFFF">
                        <Path.Data>
                            <PathGeometry FillRule="Nonzero" Figures="M196.328,220.879L150.523,196.796 104.72,220.871 113.465,169.872 76.405,133.759 127.623,126.317 150.52,79.918 173.424,126.317 224.631,133.759 187.573,169.872z" />
                        </Path.Data>
                    </Path>
                </Canvas>
            </Canvas>
        </Viewbox>
    </ControlTemplate>

在我的MainWindow.xaml

    <Button x:Name="unBtn" Style="{StaticResource MinButtonStyle}" Height="30" Margin="448,256,10,0" VerticalAlignment="Top" Click="unBtn_Click" FontSize="10" BorderBrush="{x:Null}">
        <StackPanel Orientation="Horizontal">
              <ContentControl Template="{StaticResource StarBlue}" />
        </StackPanel>
    </Button>

所以确切的问题是,如何在我的按钮上动态地使用{StarBlue}画布图形作为内容,在.cs文件中,就像while()循环中的那样?

1 个答案:

答案 0 :(得分:0)

由于克莱门斯的暗示,我想出了一个解决方案。我使用的是Viewbox而不是ControlTemplate。

在我的MainWindow.xaml.cs

while(sqlite_datareader.Read())
{
    System.Windows.Controls.Button starBtn = new System.Windows.Controls.Button();
    Viewbox dynamicViewbox = (Viewbox)System.Windows.Application.Current.FindResource("StarBlue");
    dynamicViewbox.StretchDirection = StretchDirection.Both;
    dynamicViewbox.Stretch = Stretch.Fill;
    starBtn.Style = (Style)FindResource("StarButtonStyle");
    starBtn.Padding = new Thickness(0,0,0,0);
    starBtn.Margin = new Thickness(0,0,0,0);
    starBtn.Width = 24;
    starBtn.Height = 24;
    starBtn.Content = dynamicViewbox;
    splMain.Children.Add(starBtn);
}

在我的App.xaml内部

<Viewbox x:Key="StarBlue">
    <Canvas Name="Layer_1" Width="300" Height="300" ClipToBounds="True" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
        <Canvas>
            <Path Fill="#FF44B39B">
                <Path.Data>
                    <PathGeometry FillRule="Nonzero" Figures="M150.52,15.398C75.958,15.398 15.519,75.84 15.519,150.399 15.519,224.954 75.958,285.402 150.52,285.402 225.077,285.402 285.521,224.954 285.521,150.399 285.521,75.84 225.076,15.398 150.52,15.398z M150.52,266.312C86.502,266.312 34.607,214.415 34.607,150.399 34.607,86.383 86.502,34.49 150.52,34.49 214.533,34.49 266.43,86.384 266.43,150.399 266.43,214.414 214.532,266.312 150.52,266.312z" />
                </Path.Data>
            </Path>
            <Path Fill="#FF44B39B">
                <Path.Data>
                    <PathGeometry FillRule="Nonzero" Figures="M254.947,150.401C254.947,208.073 208.191,254.825 150.519,254.825 92.842,254.825 46.089,208.073 46.089,150.401 46.089,92.725 92.842,45.969 150.519,45.969 208.191,45.969 254.947,92.725 254.947,150.401z" />
                </Path.Data>
            </Path>
            <Path Fill="#FFFFFFFF">
                <Path.Data>
                    <PathGeometry FillRule="Nonzero" Figures="M196.328,220.879L150.523,196.796 104.72,220.871 113.465,169.872 76.405,133.759 127.623,126.317 150.52,79.918 173.424,126.317 224.631,133.759 187.573,169.872z" />
                </Path.Data>
            </Path>
        </Canvas>
    </Canvas>
</Viewbox>