Search code examples
c#wpfcanvasimagebrush

ImageBrush (Canvas) path not working


I am working on WPF for interactive applications. When I assign image path to ImageBrush (Canvas) from folder included in solution, its not working, it goes to bin/debug/.. in WPF. But it worked perfectly, when I assign path from directory. Following is the screenshot for issue:

screenshot

Following is the code snippet I have written which is not working:

ImageBrush myBrush = imgContent1;
myBrush.ImageSource = new BitmapImage(new Uri(@"Informatin_Concept_100.jpg", UriKind.Relative));
canContent1Info.Background = myBrush;

But following code snippet work when I am assigning images from directory:

C = new System.IO.DirectoryInfo(Environment.CurrentDirectory).Parent.Parent.FullName;
imgContent1.ImageSource = new BitmapImage(new Uri(C + "\\" + "Content1\\Default\\Informatin_Concept_100.jpg", UriKind.Absolute));

Can't figure out where is the error?


Solution

  • Use a WPF Resource File Pack URI to load a BitmapImage from an assembly resource. The Build Action of the image file in your Visual Studio project must be set to Resource.

    myBrush.ImageSource = new BitmapImage(
        new Uri("pack://application:,,,/Informatin_Concept_100.jpg"));