How to change panorama background programmatically? - c#

Background of the panorama should change depending on FlowDirection of the current language.
So I thought I should do this programmatically (is it possible in the XAML?)
I added this to the OnNavigatedTo event handler of the page:
ImageBrush ib = new ImageBrush();
if (AppResources.ResourceFlowDirection == "RightToLeft")
{
ib.ImageSource = new BitmapImage(new Uri("ms-appx:///Assets/CustomBackgroundMirror.png"));
PanoramaRoot.Background = ib;
}
else
{
ib.ImageSource = new BitmapImage(new Uri("ms-appx:///Assets/CustomBackground.png"));
PanoramaRoot.Background = ib;
}
but after running, there is no image on the background, it is black.
I have a doubt that I am referring to the image badly.
How can I solve this problem?
or if the way is right, how can I get sure that the brush has an image?
Update: I have tested this too, but no difference:
ib.ImageSource = new BitmapImage(new Uri("/Assets/CustomBackground.png", UriKind.Relative));

Your updated code,
ib.ImageSource = new BitmapImage(new Uri("/Assets/CustomBackground.png", UriKind.Relative);
works properly.
You just have to make sure that the Build Action property of the image is set to Content.

Related

WPF loading a BitmapImage into an image programatically

I am trying to load an image into an Image control in a WPF application.
I need to set the Input in one function, and bind it to the Image in another.
This works:
var b = new Binding { Source = (ImageSource)new ImageSourceConverter().ConvertFromString("D:/data/TestPattern.jpg") };
CameraFrame.SetBinding(Image.SourceProperty, b);
But this does not:
BitmapImage bSource = new BitmapImage(new Uri("D:/data/TestPattern.jpg"));
var b = new Binding { Source = (ImageSource)new ImageSourceConverter().ConvertFrom(bSource) };
CameraFrame.SetBinding(Image.SourceProperty, b);
Why is this? Am I missing something in the ConvertFrom function?
Why don't you simply set the Source property to a BitmapImage?
CameraFrame.Source = new BitmapImage(new Uri("D:/data/TestPattern.jpg"));

System.Windows.Controls.Button image as content using WPF c#

Here is the code and the image is not showing. If I use the text content it shows up but the image will not. What am I missing?
//Begin btnLine Code.
btnLine = new System.Windows.Controls.Button();
// Create the image element.
Image simpleImage = new Image();
simpleImage.Width = 200;
simpleImage.Margin = new Thickness(5);
// Create source.
BitmapImage bi = new BitmapImage();
// BitmapImage.UriSource must be in a BeginInit/EndInit block.
bi.BeginInit();
bi.UriSource = new Uri(InstallDir+#"\ToolbarImages\line1.jpg",UriKind.RelativeOrAbsolute);
bi.EndInit();
// Set the image source.
simpleImage.Source = bi;
btnLine.Content= bi;//image button Does not work
//btnLine.Content= simpleImage;//image button Does not work
//btnLine.Content= "o-o";//text button Does work
btnLine.Click += btnLine_Click;
chartWindow.MainMenu.Add(btnLine);
Normally it is much easier to do these things in XAML. it is fairly straight forward to set an image to a a local resource. This is a simple version how.
<Grid>
<Button>
<Image Source="ToolbarImages\image1.jpg"/>
</Button>
</Grid>
If you are looking for a dynamic way to do this, you should use a binding on the source image that way you are leveraging the power of wpf.
change
btnLine.Content= bi;
to
btnLine.Content= simpleImage;

change image.source when tapped

I'm a novice programmer so don't be brutal.
I'm making a game for Windows Store, and I want to animate a run cycle. I made many GIF animations but all have BLACK background, and I need it transparent. So I've decided to make a run cycle using DispatcherTimer. Everything works fine, but the images don't change :/
void timer_Tick(object sender, object e)
{
numer++;
if (numer > 8) numer = 1;
hero.Source.Equals("Assets/Anim/" + nazwa + numer + ".png");
}
Also, When I TAP a different image, it should change the image and other images, but it doesn't... what is wrong?
bool sun = true;
private void Image_Tapped(object sender, TappedRoutedEventArgs e)
{
sun = !sun;
if (sun == false)
{
Image1.Source.Equals("moon.png");
Image2.Source.Equals("ON.png");
}
else
{
Image1.Source.Equals("sun.png");
Image2.Source.Equals("OFF.png");
}
}
The xaml works fine, as the images are shown.
I have checked this question:
ImageTools on Windows Phone 8, changing ImageSource and DataContext
but I get loads of errors. I don't seem to understand how the property changed works.
it seems to be a small mistake. You are using the wrong method.
Image.Source.Equals()
is a boolean method that simply compares the current source with the "source" you give as arguement and will return true or false based on the comparison.
But what you want is to set the source of the image.
So you need to use:
Image1.Source = new BitmapImage(new Uri("moon.png", UriKind.RelativeOrAbsolute));
This will set the source of the Image to the new image you want.
Assuming that "moon.png" is in the main folder in your solution, the two solutions both work:
BitmapImage tn = new BitmapImage();
tn.SetSource(Application.GetResourceStream(new Uri(#"moon.png", UriKind.Relative)).Stream);
Image1.Source = tn;
Or
Image1.Source = new BitmapImage(new Uri("moon.png", UriKind.Relative));

Adding Image to wpf

I want to add an Image dynamically to the wpf application.
What I have tried :
Image img = new Image();
img.Height = 100;
img.Width = 100;
img.Visibility = Visibility.Visible;
Thickness th = new Thickness(100, 100, 0, 0);
img.Margin = th;
string strUri2 = String.Format(#"pack://application:,,,/MyFirstWPF;component/Images/tt.jpg");
img.Source = new BitmapImage(new Uri(strUri2));
I know, the image won't display unless I add it to the Content.
this.Content=img;
but with this the existing controls(shapes) on the app are lost.
Now, my question is how to add the image to the content without losing existing controls from the app.
When you are going to load and show an image dynamically, you still need to think about the layout of your application. I would suggest to add an (initially empty) Image control in XAML (for example in a Grid), and later set the Source property of this control in code.
<Grid>
... other controls ...
<Image Name="img" Grid.Row="..." Grid.Column="..."
Width="100" Height="100" Margin="100,100,0,0"/>
</Grid>
Set Source in code:
var uri = new Uri("pack://application:,,,/MyFirstWPF;component/Images/tt.jpg");
img.Source = new BitmapImage(uri);
by default the window content is a grid so try
(this.Content as Grid).Children.Add(img);

How set dynamically windows back ground image in wpf

I have small doubt in wpf,i kept image on button.so when we click on button that image should be set as back ground?what should i do? any one have idea>
please let me know.
Thanks in Adavance
Developer
If you need to put an image as a background of any control (button or grid, didn't understancd you), just set, in code, the Background property to an ImageBrush with an ImageSource of your image.
This code is for button, edit it ...
Button button = new Button();
button.Margin = new Thickness(220, -880, 0, 0);
button.Width = w;
button.Height = h;
/////////////////////////////////////////////////////////
ImageBrush berriesBrush = new ImageBrush();
berriesBrush.ImageSource = new BitmapImage(new Uri(#"Image/country.PNG", UriKind.Relative));
button.Background = berriesBrush;
button.BorderThickness = new Thickness(0);
This might you ...

Categories

Resources