我在按钮上以编程方式设置了图像问题。 我有下一个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
};
也没有运气。我犯错的任何想法?
答案 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")),