Hari krishnan
Hari krishnan

Reputation: 2108

Access image in CS file C# WPF

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

Upvotes: 0

Views: 2476

Answers (6)

Sami AlGhaberi
Sami AlGhaberi

Reputation: 19

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.

Upvotes: 0

XAMlMAX
XAMlMAX

Reputation: 2363

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>

Upvotes: 1

jamesthollowell
jamesthollowell

Reputation: 1712

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

Upvotes: 1

Rick S
Rick S

Reputation: 6586

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

Upvotes: 2

Tan
Tan

Reputation: 2178

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

Upvotes: 0

TylerD87
TylerD87

Reputation: 1618

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.

Upvotes: 0

Related Questions