按钮以编程方式设置图像

时间:2016-02-17 11:23:25

标签: c# wpf windows-phone-8.1

我在按钮上以编程方式设置了图像问题。 我有下一个WPF按钮:

<Button Name="MyBtn" x:FieldModifier="public" HorizontalAlignment="Center" VerticalAlignment="Center" Click="FlashOnOf_Click" Height="256" Width="256" BorderBrush="{x:Null}"/>

并尝试使用下一种情况设置图像:

        var brush = new ImageBrush();
        brush.ImageSource = new BitmapImage(new Uri("ms-appx://Assets/light_bulb_off.png"));
        brush.Stretch = Stretch.Fill;

        MyBtn.Background = brush;

又一个案例:

        MyBtn.Content = new Image
        {
            Source = new BitmapImage(new Uri("ms-appx://Assets/light_bulb_off.png")),
            VerticalAlignment = VerticalAlignment.Center,
            Stretch = Stretch.Fill,
            Height = 256,
            Width = 256
        };

也没有运气。我犯错的任何想法?

4 个答案:

答案 0 :(得分:3)

你应该像这样设置图像:

private void btn_Click(object sender, RoutedEventArgs e)
{
    btn.Content = new Image
    {              
        Source = new BitmapImage(new Uri("/WpfApplication1;component/image/add.jpg", UriKind.Relative)),
        VerticalAlignment = VerticalAlignment.Center,
        Stretch = Stretch.Fill,
        Height = 256,
        Width = 256
    };
}

其中Uri("/WpfApplication1;component/image/add.jpg"是地址到图片,WpfApplication是WPF应用程序的名称,image是文件夹,add.jpg是图片的名称。

基本方法如下:

Button myButton = new Button
{
    Width = 50,
    Height = 50,
    Content = new Image
    {
        Source = new BitmapImage(new Uri("image source")),
        VerticalAlignment = VerticalAlignment.Center
    }
};

答案 1 :(得分:0)

您需要在创建BitmapImage

时指定UriKind
    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        MyBtn.Content = new Image
        {
            Source = new BitmapImage(new Uri("/Assets/Imagename.png", UriKind.RelativeOrAbsolute)),
            VerticalAlignment = VerticalAlignment.Center,
            Stretch = Stretch.Fill,
            Height = 256,
            Width = 256
        };
    }

另请使用。

查看您的图片
<Image Height="256" Width="256" Source="/Assets/Imagename.png" />

答案 2 :(得分:0)

尝试将此图像设置为按钮背景,

private void Button_Click_1(object sender, RoutedEventArgs e)
          {
            Button button = sender as Button;
            ImageBrush brush = new ImageBrush();
            BitmapImage bitmap = new BitmapImage();
            bitmap.BeginInit();
            bitmap.UriSource=new Uri (@"C:/imagename.jpg",UriKind.Absolute);
            bitmap.EndInit();
            brush.ImageSource = bitmap;
            button.Background = brush;
          }

要将图片设置为按钮内容,请尝试此操作。

        private void Button_Click_1(object sender, RoutedEventArgs e)
           {
            Button button = sender as Button;
            button.Content = new Image
            {
                Source = new BitmapImage(new Uri("/Images/imagename.JPG", UriKind.RelativeOrAbsolute)),
                VerticalAlignment = VerticalAlignment.Center,
                Stretch = Stretch.Fill,
                Height = 256,
                Width = 256
            };
          }

答案 3 :(得分:-1)

问题在URI名称中: 下一行

// Query database
$sqlFind = 'SELECT `video_path` FROM `videos`';
$result = mysqli_query($conn, $sqlFind);
$db = []; // create empty array
while ($row = mysqli_fetch_row($result))
    array_push($db, $row[0]);
// Check files
$files1 = scandir($directory, 1);
if ( $files1 !== false ) {
    foreach ($files1 as $i => $value) {
        if (in_array($value, $db)) {
            // File exists in both
        } else {
            // File doesn't exist in database
        }
    }
} else {
    echo 0;
}

应改为:

Source = new BitmapImage(new Uri("ms-appx://Assets/light_bulb_off.png")),