0

I had set background image in XAML file. I had added Mouse Enter Event listener. And added the code:

var image = new ImageBrush();
image.ImageSource = new BitmapImage(new Uri("Images/buttonHover.png", UriKind.Relative));
this.Background = image;

But it is throwing DirectoryNotFound Exception. I do not know how to get the image from the folder. My project consists of a folder named Images.

How can i access the image from the Images folder?

EDIT

private void button1_MouseEnter(object sender, MouseEventArgs e)
        {
            var image = new ImageBrush();
            image.ImageSource = new BitmapImage(new Uri("/WordFill;component/Images/buttonHover.png", UriKind.Relative));
            this.Background = image;
        }

Thanks

4
  • Have you set the Copy to output directory property of the image file to Copy Always or Copy if Newer? Commented Jan 27, 2014 at 15:18
  • @AndersGustafsson yes... Commented Jan 27, 2014 at 15:24
  • Can you show your directory structure please? Commented Jan 27, 2014 at 15:44
  • @jamesthollowell i referred stackoverflow.com/questions/4009775/… but the background image is not displaying, its displaying default background. Commented Jan 27, 2014 at 15:47

6 Answers 6

2

You could store the image as a resource in your project and access it this way:

var SourceUri = new Uri("pack://application:,,,/MyCompany.MyProduct.MyAssembly;component/MyIcon.ico", UriKind.Absolute);
thisIcon = new BitmapImage(SourceUri);
Sign up to request clarification or add additional context in comments.

4 Comments

I asked not about XAML
Thanks i got...But image not displaying, its displaying default background
As a test, what if you do this: this.Background = Brushes.Blue;
Yes works...But when hovering a gray background is displaying and after mouse leave the image is displayed. What to do?
1

AppDomain.CurrentDomain.BaseDirectory will give you the full path of where your program is running.

Change your code to:

image.ImageSource = new BitmapImage(new Uri(AppDomain.CurrentDomain.BaseDirectory+"Images\buttonHover.png", UriKind.Relative));

Comments

1

have you tried this?

/<application_name>;component/Images/<image_name.png>

basically you should replace your existing path with the one above, while remember to replace code between <> tags

EDIT

and here is the xaml version

<Grid Name="mainGrid"> <Rectangle><Rectangle.Fill><ImageBrush ImageSource="myCodeSnippet"/></Recatngle.Fill></Rectangle></Grid>

6 Comments

Tried but still the same
@Harikrishnan I want to get things right here: IS your Solution tree like this:? WordFiller -> Images,-> Some other folder like Views
private void button1_MouseEnter(object sender, MouseEventArgs e) { var image = new ImageBrush(); image.ImageSource = new BitmapImage(new Uri("/WordFill;component/Images/buttonHover.png", UriKind.Relative)); this.Background = image; }
Aha! have you tried using my code in the xaml file? like: <Grid Name="mainGrid"> <Rectangle><Rectangle.Fill><ImageBrush ImageSource="myCodeSnippet"/></Recatngle.Fill></Rectangle></Grid> let us know @Harikrishnan
But i asked about cs file
|
0

Have you checked that the folder is also in the directory where the compiled files go to? I.e. "/bin/debug"?

In visual studio if the image is in your project you can change the properties of the image to "Always Copy Local" and this will ensure a copy of the file is added to the output directory when you compile.

2 Comments

Tried but same result
Yes...There is IMages folder in /bin/debug
0

Have you set the "Build Action" to content for the image ? You can find the options under properties of the file.

4 Comments

@Harikrishnan if you go into the debug folder of the application do you see the image folder and the images ?
I had changed resource and content and checked with same exception
Yes checked and folder exists
@Harikrishnan image.ImageSource = new BitmapImage(new Uri("/Images/buttonHover.png", UriKind.Relative)); This should work i have created a test project and it works.
0

I have tested your code and it results no errors although I set the image to the layoutroot rather than this.background which result of nothing to me.

To verify that the problem is related to the path of image and no other code cause the problem, try insert an image object in the xaml page and select the image from the properties through source option, and use the same path that would be created automatically, if no list of images is exists., this mean you have created the image folder in the wrong place it should be created under the main project, and you might created the folder under the web project.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.