Crash on changing the Image source property - c#

<Image x:Name="pageImg" Margin="-19,-1,37,19" Source="/img/1.png" Stretch="Uniform" />
The images property also set to Build Action = Resource and Copy Output Directory = Cope if newer
When the button get clicked the application crashes:
private void Button_Click_1(object sender, RoutedEventArgs e)
{
pageImg.Source = new BitmapImage(new Uri(#"/img/2.png"));
}
But when I pass C:\Users\myuser\Desktop\2.png instead of /img/2.png it works just fine.
Why this is happening?

An image resource file (with Build Action set to Resource) should be loaded by a Resource File Pack URI:
pageImg.Source = new BitmapImage(new Uri("pack://application:,,,/img/2.png"));
It is not necessary to get it copied to the output directory.

This solved my problem :
pageImg.Source = new BitmapImage(new Uri(#"/img/2.png", UriKind.Relative));

Related

Change background image source on button click in wpf forms

My default background image is "lobby.jpg" and when I click the "Lights" button I want it to swap with "lobby1.jpg" and vice versa. These images are stored in "obj\Debug\Images\".
Also I'd like to implement relative(?) imagesource uris so that I can access the images on any machine (without using the whole uri, just the "obj\Debug\Images\").
Edit: So the main issue seems to be that I tried changing the window background without realising that it was getting "covered" by the grid background of the page. So what I did is I set the main window background to "lobby.jpg", I made the grid background invisible and used the code from the answer to swap between the 2 backgrounds.
You can use AppDomain basepath to exe (this is are simplest way)
var basePath= AppDomain.CurrentDomain.BaseDirectory;
var imageDirPath = $"{basePath}\\Images\\";
Example:
bool clicked = false;
private void button_Click(object sender, RoutedEventArgs e)
{
var basePath = AppDomain.CurrentDomain.BaseDirectory;
var imageDirPath = $"{basePath}\\Images\\";
if (clicked)
image.Source = new BitmapImage(new Uri(imageDirPath+ "lobby.jpg"));
else
image.Source = new BitmapImage(new Uri(imageDirPath + "lobby1.jpg"));
clicked = !clicked;
}

C# WPF Change grid background image [duplicate]

This question already has an answer here:
how can I set a background image in code?
(1 answer)
Closed 7 years ago.
How can I change grid background image of clicked button? I tried this code but it didn't work. I need help.
Code:
WpfApplication5.Properties.Settings.Default.GridImage = "Pictures\file.jpg";
Background can be set by using ImageBrush:
var imgBrush = new ImageBrush();
imgBrush.ImageSource = new BitmapImage(new Uri(#"Pictures\file.jpg", UriKind.Relative));
myGrid.Background = imgBrush;
When using relative path, you need to have Pictures folder with file.jpg in bin\Debug folder.
Like this.You should set Background of Button
<Grid>
<Button Name="button1" Click="button1_Click">
</Button>
</Grid>
private void button1_Click(object sender, RoutedEventArgs e)
{
Uri uri = new Uri("image path", UriKind.Relative);
BitmapImage img = new BitmapImage(uri);
button2.Background = new ImageBrush(img );
}

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));

Saving, Storing, Loading Photographs in Windows Phone

Good morning everyone!
I've been having some issues regarding photos in my Windows Phone app. I'm trying to take a photo and save it to a record, and then load it up again at a different point.
My idea is to take the photo using the code in this tutorial (http://msdn.microsoft.com/en-us/library/windowsphone/develop/hh394006(v=vs.105).aspx)
In my constructor:
cameraCaptureTask = new CameraCaptureTask();
cameraCaptureTask.Completed += new EventHandler<PhotoResult>(cameraCaptureTask_Completed);
lower down..
void cameraCaptureTask_Completed(object sender, PhotoResult e)
{
if (e.TaskResult == TaskResult.OK)
{
//Code to display the photo on the page in an image control named myImage.
//System.Windows.Media.Imaging.BitmapImage bmp = new System.Windows.Media.Imaging.BitmapImage();
//bmp.SetSource(e.ChosenPhoto);
//System.Diagnostics.Debug.WriteLine(e.OriginalFileName);
//string imageLoc = e.OriginalFileName;
//Uri imageUri = new Uri(imageLoc, UriKind.RelativeOrAbsolute);
//StreamResourceInfo resourceInfo = Application.GetResourceStream(imageUri);
//Code I'm using just now which just displays the photograph
System.Windows.Media.Imaging.BitmapImage bmp2 = new System.Windows.Media.Imaging.BitmapImage();
bmp2.SetSource(e.ChosenPhoto);
myImage.Source = bmp2;
}
}
From my code, you can see that the commented out bit returns the location of the image which has just been taken, then attempts to load it up from there. However, when I try that, I get URI path exceptions and the like. What is going wrong here and what method should I use to remedy it?
I'd like to be able to add the file path to a record and then be able to load it back up elsewhere in my app.
Thanks for any help!

WPF Directory Not Found Exception and PresentationCore.pdb not loaded

I am trying to add a background image to a textbox but on textChange event, the image goes away as expected, however if I backspace or delete text in the textbox so that it is empty, I get the DirectoryNotFoundException was handeled.
And the directory:
Could not find a part of the path 'C:\myProjectFolder\bin\Debug..img\txtBackground.png'.
The XAML:
<TextBox Name="myTextBox" Width="200" TextChanged="myTextBox_TextChanged">
<TextBox.Background>
<ImageBrush ImageSource="img/txtBackground.png" />
</TextBox.Background>
C# Code:
private void myTextBox_TextChanged(object sender, TextChangedEventArgs e)
{
if (myTextBox.Text == "")
{
ImageBrush textImageBrush = new ImageBrush();
textImageBrush.ImageSource =
new BitmapImage(
new Uri(#"..img/txtBackground.png", UriKind.Relative)
);
myTextBox.Background = textImageBrush;
}
else
{
myTextBox.Background = null;
}
}
Deleted the references, re-added them, build/clean solution and rebuild but nothing.
These errors only occur when I try to add the background to the textbox.
Assuming that the img folder that has image is under the Project (not under the debug folder, ideally it should not be in Debug folder) and image's BuildAction is set to Resource, you can try this:
new BitmapImage(new Uri("pack://application:,,,/img/txtBackground.png", UriKind.Absolute));
If you have img in Debug folder then you have to reach upto that
new Uri(#"bin/Debug/img/txtBackground.png", UriKind.Relative)
Your Uri should read
new Uri(#"/img/txtBackground.png", UriKind.Relative)
At least that's what the error message says.
You can also try this:
textImageBrush.ImageSource =new BitmapImage("/Projectname;Component/img/txtBackground.png");
The image's Build Action must be set to Resource.

Categories

Resources