I have a BitmapImage, and want to set it as Background of Grid. I have tried this
xaml:
<Grid x:Name="ContentPanel">
<Grid.Background>
<ImageBrush x:Name="imgBg" />
</Grid.Background>
</Grid>
c#:
BitmapImage bmp = new BitmapImage();
bmp.DecodePixelWidth =(int) scrnWidth;
bmp.DecodePixelHeight = (int)scrnHeight;
bmp.SetSource(e.ChosenPhoto);
ImageBrush ib = new ImageBrush() { ImageSource = bmp };
imgBg.ImageSource = ib.ImageSource;
Output: Output is just black.
Question: using above code I am unable to set bitmapimage as background of Grid element, Am I missing something ?
Update I knew, it works fine when we set image as background to grid like:
ImageBrush ib = new ImageBrush() { ImageSource = bmp };
ContentPanel.Background = ib;
But I need to use xaml way, Question is same.
<Grid x:Name="ContentPanel">
<Grid.Background>
<ImageBrush Stretch="None"
ImageSource="Your Path/source"
AlignmentY="Center"
AlignmentX="Center" />
</Grid.Background>
</Grid>
This might help you, cant promise you it will but try it. was found on this post.
Here
Related
I want to add a Background colour and 9 Foreground Image to 9 buttons from code.
I wish to change the images from C# not in WPF / xaml.
The Background colour works OK using:
button1.Background.SetValue(SolidColorBrush.ColorProperty, Windows.UI.Colors.Red);
Windows forms has an easy solution using:
pictureBox1.Image = Properties.Resources.P1; // this does not work for UWP
What I have tried so far ends up in error messages:
I have changed the Build Action property of P1.png from Content to PRIResource with no success.
string url = "../../Images/P1.png";
//string url = "PW.png";
image1.Source = new BitmapImage(new Uri(url, UriKind.Relative)); //.Uri cannot be converted into a Windows.Foundation.Uri.
//image1.Source = new BitmapImage(new Uri(url, UriKind.Absolute)); //format of url could not be determined
<Button x:Name="button1" Content="Button" Grid.Column="0" Grid.Row="0"
Tag="1" Background="Gray" Padding="0" UseLayoutRounding="False"
HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Click="button1_Click">
<Button.Foreground>
<ImageBrush Stretch="Fill" ImageSource="P1.PNG"/>
</Button.Foreground>
</Button>
Solution:
To insert image to Button in C# using VS2015, UWP and Windows 8.1:
Add an Image to the front of your Button. eg Name = button1 and imageX.
Ensure that you set “Build Action” Properties of *.png to Content.
enter code hereImageBrush imageBrush = new ImageBrush();
imageBrush.ImageSource = new BitmapImage(new Uri("ms-appx:///Images/PW.PNG"));
button1.Background = imageBrush;
BitmapImage bitImage = new BitmapImage();
bitImage.UriSource = new Uri("ms-appx:///Images/PW.PNG");
imageX.Source = bitImage;
Thanks Romasz for your assistance.
The Image that I have created from Code behind is visible in my Visual tree but is not rendered on screen when I run my app.
This is my Code Behind
public MainPage()
{
this.InitializeComponent();
//Image that was created in Xaml
image.Source = new BitmapImage(new Uri("ms-appx:///testimage.jpg"));
//Image creation from C# code
Image image1 = new Image();
image1.Width = 200;
image1.Height = 200;
image1.Margin = new Thickness(50, 50, 0, 0);
image1.Source = new BitmapImage(new Uri("ms-appx:///download.jpg"));
//grid is the Grid name in Xaml
grid.Children.Add(image1);
}
This is my XAML
<Page Name="HI"
x:Class="test1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:test1"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid x:Name="grid">
<Image x:Name="image" Visibility="Visible" HorizontalAlignment="Left" Height="200" Margin="10,200,0,0" VerticalAlignment="Top" Width="200"/>
</Grid>
What am I missing here?
Your Xaml isn't complete. You are missing a closing tag for Page.
Having the next XAML code
<Window.Resources >
<ImageBrush x:Key="tile" ImageSource="afraid.png"
Opacity=" 20" TileMode="Tile"
ViewportUnits="Absolute"
Viewport=" 0,0,32,32"
></ImageBrush>
</Window.Resources>
I want to change the image by c# code, during the run-time
I have the next code to do this, but the code does not work & I don`t know the reason:
ImageBrush img = ( ImageBrush )this.FindResource( "tile" );
ImageSourceConverter conv = new ImageSourceConverter();
ImageSource src = ( ImageSource )conv.ConvertFromString( "mad.png" );
img.ImageSource = src;
Please refer to this answer.
Try this, create a BitmapImage from a file and set it as the ImageSource of the ImageBrush.
ImageBrush img = (ImageBrush)this.FindResource("tile");
//change the imagesource in runtime,
img.ImageSource = new BitmapImage(new Uri("mad.png", UriKind.Relative));
Im using the following code to take a photo in my app, but when I accept the photo, the image gets stretched, but when I navigate away from that page, and towards it again the image resolution is correct however a part of the image is black. my question is how can I correctly display the image without it stretching.
CameraCaptureTask cam = new CameraCaptureTask();
cam.Completed += task_Completed;
cam.Show();
void task_Completed(object sender, PhotoResult e)
{
if (e.TaskResult == TaskResult.OK)
{
System.Windows.Media.Imaging.BitmapImage bmp = new System.Windows.Media.Imaging.BitmapImage();
bmp.SetSource(e.ChosenPhoto);
imgProfilePic.ImageSource = bmp;
}
and the xaml is:
<Button HorizontalAlignment="Center"
toolkit:TurnstileFeatherEffect.FeatheringIndex="2"
toolkit:TiltEffect.IsTiltEnabled="True"
x:Name="btnprofilepic"
Width="250"
HorizontalContentAlignment="Center"
VerticalContentAlignment="Center"
MouseLeave="btnprofilepic_MouseLeave"
Tap="imgProfilePic_Tap"
Margin="0,20,0,20"
Height="200" BorderThickness="0"
MouseLeftButtonUp="btnprofilepic_MouseLeftButtonUp"
MouseLeftButtonDown="btnprofilepic_MouseLeftButtonDown">
<Button.Background>
<ImageBrush x:Name="imgProfilePic"
Stretch="Fill" />
</Button.Background>
</Button>
In your xaml make strech = "Uniform" because fill will strech the image according to container ...
I have this ResourceDictionary in Main.xaml:
<Window.Resources>
<ResourceDictionary>
<BitmapImage x:Key="Customer" UriSource="Icons/customer.png"/>
<BitmapImage x:Key="Project" UriSource="Icons/project.png"/>
<BitmapImage x:Key="Task" UriSource="Icons/task.png"/>
</ResourceDictionary>
</Window.Resources>
I initially set the image using:
<Image Name="TypeIcon" HorizontalAlignment="Left" VerticalAlignment="Center"
Source="{StaticResource Customer}" Height="16" Width="16"/>
I'm trying to change TypeIcon's Source from Customer to Project in a C# method.
I've tried using:
TypeIcon.Source = "{StaticResource Project}";
But I get this error:
Cannot implicitly convert type string to System.Windows.Media.ImageSource
I've tried defining the image using new ImageSource(), but this doesn't work either.
How can I change the image's Source programmatically in C#?
After much Googling, whilst writing this question, I figured out how to do it:
TypeIcon.Source = (ImageSource) Resources["Project"];
It is not for static resources but perhaps will be useful anyway... :)
i.e. how to set background for Grid dynamically
var myBrush = new ImageBrush();
var image = new Image
{
Source = new BitmapImage(
new Uri(
"pack://application:,,,/YourAppName;component/Images/Boo.png"))
};
myBrush.ImageSource = image.Source;
MainGrid.Background = myBrush;
i.e. how to set icon of the app dynamically
var idleIco = new Image
{
Source = new BitmapImage(
new Uri(
"pack://application:,,,/YourAppName;component/Images/idle.ico"))
};
SomeObjectYouAreUsingToSet.IconSource =idleIco.Source;
You can use the ImageSourceConverter class to get what you want, for example:
img1.Source = (ImageSource)new ImageSourceConverter().ConvertFromString("/Assets/check.png");