ImageBrush (Canvas) path not working [duplicate] - c#

This question already has answers here:
How to load image to WPF in runtime?
(2 answers)
Closed 5 years ago.
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?

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

Related

My WPF UserControl can't locate resources when I use it with a project other than the one it was built in

So I built a WPF user control as part of a larger solution. I'm building another app where this user control would work nice. I added a reference to the DLL from the new app, wired it up and compiled. The window loads and I can see the image that it's telling me it can't find. After the main window displays and the user control is populated, an exception is thrown saying...
System.IO.IOException: 'Cannot locate resource 'resources/nosortnofilter.png'.'
The user control is a DataGrid with some extensions added to it. The column it threw on was "id". As you can see in the image, the red arrow shows the nosortnofilter.png image being displayed in all columns. So why is it throwing this exception?
The line of code it throws on is here.
If ImageName = "" Then ImageName = "NoSortNoFilter"
img.Source = New BitmapImage(New Uri("pack://application:,,,/Resources/" & ImageName & ".png"))
So it all looks good from my perspective. Hoping someone can see what I'm not seeing.
EDIT: Found a solution. This works. But it still doesn't answer the questions why the original pack:// formatted URI only worked with the original solution.
img.Source = New BitmapImage(New Uri($"Resources/{ImageName}.png", UriKind.Relative))
EDIT: Thanks to rfmodulator for giving me the correct URI for DLL's.
img.Source = New BitmapImage(New Uri("pack://application:,,,/AdvancedSortFilterDataGrid;component/Resources/" & ImageName & ".png"))
This is the URI of a resource in the Application's assembly:
pack://application:,,,/Resources/[RESOURCENAME]
To get a resource in a DLL, the URI looks more like this:
pack://application:,,,/[DLLNAME];component/Resources/[RESOURCENAME]
For details, see Pack URIs in WPF.

How to use image from resources? [duplicate]

This question already has answers here:
WPF - Import image as resource
(3 answers)
How do I change an image source dynamically from code-behind in WPF with an image in Properties.Resources?
(1 answer)
Closed 3 years ago.
I would like to use path to image from program resources instead of using full path from windows.
tlo.Fill = new ImageBrush
{
ImageSource = new BitmapImage(new Uri(#"D:\Willie\Documents\ColorTester\ColorTester\Resources\1.jpg", UriKind.Absolute))
};
I want to use path like this:
ImageSource = new BitmapImage(new Uri("ColorTester.Resources.1.jpg"));
Can anyone help me with this, because when I try to use it, visual studio throws and exception "System.UriFormatException"?
I don't know the correct path to your image, if you have a folder named Resources under your project and images are in there, you can address them like below:
ImageSource = new BitmapImage(new Uri("/Resources/1.jpg", UriKind.Relative));
If the image is inside a UserControl and you want to use it in another project, then address it like:
ImageSource = new BitmapImage(new Uri("pack://application:,,,/{YourAssemblyWhereResourceIsLocated};component/Resources/1.jpg"));
Useful links:
WPF image resources
Adding resource dictionaries to a usercontrol library in wpf
https://learn.microsoft.com/en-us/dotnet/framework/wpf/app-development/pack-uris-in-wpf

WPF C# Bitmap resource as Imagesource for ImageBrush [duplicate]

I want to change the background of a button manually in my WPF app.
I have an image imported into my resources and I want to do:
MyButton.Background = MyProject.Properties.Resources.myImage;
But I get the error:
cannot implicitly convert system.drawing.bitmap to media.brush
How can I do this??
You should read about brushes first here.
And then use ImageBrush, something like this:
MyButton.Background = new ImageBrush(...);
(or, maybe, put the brush into resources...)
UPDATE
You can find how to create imageSource from bitmap easilly. for example, here. Like:
var bitmapSource = Imaging.CreateBitmapSourceFromHBitmap(MyProject.Properties.Resources.myImage.GetHbitmap(),
IntPtr.Zero,
Int32Rect.Empty,
BitmapSizeOptions.FromEmptyOptions());
MyButton.Background = new ImageBrush(bitmapSource);
In a WPF application, you do usually not add image resources as you did in WinForms.
Instead you add the image file directly to your Visual Studio project, just like any other file. If there are multiple images, it may make sense to put them in a subfolder of the project (e.g. called "images"). The Build Action of that file has to be set to Resource (which is the default for image files).
Now you can create a BitmapImage from a Pack URI to that file.
Finally you create an ImageBrush from the BitmapImage to set the Background property.
var uri = new Uri("pack://application:,,,/images/myImage.jpg");
var image = new BitmapImage(uri);
MyButton.Background = new ImageBrush(image);

Load ImageFile from DLL

I build a modular application with a WindowsPhone 8 Project and a WP8 DLL. I try to reference a image in the DLL in code behind of the WP8 Project.
I tried
image.Source = new BitmapImage(new Uri(#"pack://application:,,,/DllAssemblyName;component/Assets/image.png",
UriKind.Absolute));
Here I get a Invalid URI: Invalid port specified.
I also tried
image.Source = new BitmapImage(new Uri(#"/DllAssemblyName;component/Assets/image.png",
UriKind.RelativeOrAbsolute));
This work's with out exceptions but the image is not visible.
What do I wrong?

Cannot set image source in code behind

There are numerous questions and answers regarding setting image source in code behind, such as this Setting WPF image source in code.
I have followed all these steps and yet not able to set an image. I code in WPF C# in VS2010. I have all my image files under a folder named "Images" and all the image files are set to Copy always. And Build Action is set to Resource, as instructed from documentation.
My code is as follow. I set a dog.png in XAML and change it to cat.png in code behind.
// my XAML
<Image x:Name="imgAnimal" Source="Images/dog.png" />
// my C#
BitmapImage img = new BitmapImage();
img.UriSource = new Uri(#"pack://application:,,,/FooApplication;component/Images/cat.png");
imgAnimal.Source = img;
Then I get a empty, blank image of emptiness. I do not understand why .NET makes setting an image so complicated..w
[EDIT]
So the following works
imgAnimal.Source = new BitmapImage(new Uri(#"pack://application:,,,/FooApplication;component/Images/cat.png"));
It works, but I do not see any difference between the two code. Why the earlier doesn't work and the latter does? To me they are the same..
Try following:
imgAnimal.Source = new BitmapImage(new Uri("/FooApplication;component/Images/cat.png", UriKind.Relative));
Default UriKind is Absolute, you should rather use Relative one.
[EDIT]
Use BeginInit and EndInit:
BitmapImage img = new BitmapImage();
img.BeginInit();
img.UriSource = new Uri(#"pack://application:,,,/FooApplication;component/Images/cat.png");
img.EndInit();
imgAnimal.Source = img;

Categories

Resources