How to ask absolute path from C# System.IO.IOException [duplicate] - c#

I am having a WPF window, with a background image of one file in my local system. So the XAML file looks like this:
<Window.Background>
<ImageBrush ImageSource="/MYASSEMBLY;component/Resources/MyPic.png"/>
</Window.Background>
In the XAML designer it actually shows this background image, so the image does exist in MYASSEMBLY\Resources folder. However, in the InitializeComponent() function call when I launch the application, it throws the exception
Cannot locate resource 'resources/mypic.png'.
So I am totally lost now. Are the directories different between the design and runtime so doesn't exist in the runtime?

In Solution Explorer set the image file's Build Action property to Resource. Then clean the solution and rebuild it.

Related

Errors after changing tool strip button image

Once i add this line to the code:
this.tsbAdd.Image = Bitmap.FromFile(#"..\..\Resources\add.bmp");
I'm unable to open editor of that form.
Screenshot of designer
I can compile app and images work as they should.
Expected results - new image is displayed without breaking designer.
Real results - new image breaks designer.
Once i build it into .exe it doesn't open. Without images it works flawlessly.
Nope it won't. The picture will be built, but referencing it by this path won't be working.
The resource file WOULD be built into your exe, but not at "....\Resources\add.bmp". This path only exists in your IDE configuration position, when your program is at "bin\Debug", you understand me?
Imagine you put your exe into C:\, then where is "....\Resources"? You cannot refer to a image in this way.
You should add resources in the project panel (I believe you have done that), and the way you get this file is via ResourceManager, not using this path. Like this:
ResourceManager rm = Resources.ResourceManager;
this.tsbAdd.Image = (Image) rm.GetObject("add");
The resourcemanager will pull the resource bitmap out from your built exe. Just using that path won't work. As the designer is not ran in \bin\Debug, no wonder it is broken too because it cannot find your file using that path.

C# WPF Visual Studio Resource Showing In Designer But Not At Run Time

I'm using Visual Studio 2019 Community
How I added the .png to my project.
Open Resources.resx in the project
Click Add Resource -> Add Existing File
Select the .png file to add
It is added to the program. I can see it in resources. It exists in my designer and displays properly in it.
There are other images used in this program that have the exact same properties and that I can call at any time and they work just fine. However, these new ones do not. They are both .png files. I have tried cleaning and rebuilding the solution multiple times as well.
Code for my xaml:
<Image Source="Resources/FileName.png" Width="50" HorizontalAlignment="Left" Margin="5,5,0,0"/>
Resources Properties:
Build Action = Embedded Resource
Copy to Output Directory = Do not copy
Image Properties
Persistence = Linked at compile time (same for working and not working resources)
You should mark the Build Action as Resource if you want to access it via folder, otherwise(in your case Embedded Resource) you would have to access the application pack with a more complicated URI.
1) Change build action in the image properties to Resource.
2) If your image is located in the Resources folder of your project
you can refer to it like below:
<Image Source="/Resources/FileName.png" Width="50" HorizontalAlignment="Left" Margin="5,5,0,0"/>
More detailed information is located in the Microsoft Docs here: Pack URIs in WPF
Set Build action to Resource,then use the following code
<Image Source="Pack://Application:,,,/Resources/FileName.png"/>

Image from WPF project's resource stopped showing

I added some images as resources to my WPF project, changed their Build Action to Resource in Solution Explorer and added them to my Main Window like this:
<Image ... Source="Resources/user.png"/>
Everything was working until I added and removed one project from my solution, then these images stopped showing both in design and runtime. The <Image Source/> tag in XAML editor and the Error List now show the message:
Could not find a part of the path "C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\Resources\user.png".
I tried restarting Visual Studio and the computer, and also cleaned and rebuilt it, but I still don't get the images.
When you use <Image Source="" />, the Source is relative to the XAML file path. The XAML file was on the root, and I moved it to a View folder. I didn't know about this until I read this comment.
The images are stored in a Resources folder inside the project, so in this case the Source property must start with a forward slash to point it to root, like this:
<Image ... Source="/Resources/user.png"/>

WPF can't find part of path in ShadowCache

I have an image in a Asset class library project. The build action is Content and the Copy to Output Directory setting is Copy Always because I want to be able to allow users to replace these files without my intervention.
The problem is, I'm getting a design-time error (everything works find on compile and run) that says "Could not find a part of the path 'C:\Users\Name\AppData\Local\Microsoft\VisualStudio\12.0\Designer\ShadowCache\oreaxsnr.cjd\orsr3pqd.wnf\Images\TestImage.jpg'."
I'm using Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); to get the path of the assembly, and then I concatenate that with a \\Images\\TestImage.jpg string. Thinking that was the source of the problem, I commented out everything related to getting that image, cleaned the solution, and rebuilt. It still has a problem with finding part of the path.
I went to the orsr3pqd.wnf folder and there's the class library .dll that calls the image, and nothing else. I deleted all of the ShadowCache folder after closing Visual Studio, restarted VS2013, and it immediately put two folders in the ShadowCache folder.
Maybe it's thinking there's supposed to be a resource in the .dll, even though I'm setting the build action to Content? Any direction would be helpful.
You should be able to reference it straight from the XAML if it is in a project that is part of the application like this:
<Image Source="/AssemblyName;component/Images/TestImage.jpg" />
If that doesn't work, then this should:
<Image>
<Image.Source>
<BitmapImage UriSource="pack://application:,,,
/AssemblyName;component/Images/TestImage.jpg"/>
</Image.Source>
</Image>
If neither of these work for you, then I'd also suggest setting your image file Build Action to Resource, rather than Content.

Image XAML tag (Win8) not working with some paths

The Source attribute of the Image tag works only with the files which were added to the 'Assets' folder in the Solution Explorer.
I've got two files in the Assets directory: Logo.png and Logo2.png, but only Logo.png is added to 'Assets' in the Solution Explorer. This image works properly:
<Image Source="/Assets/Logo.png"/>
while this image doesn't show up:
<Image Source="/Assets/Logo2.png"/>
File paths like 'C:\Users\user\Pictures\img.jpg' don't work either.
Is it possible to access images which are not specified in the 'Assets' folder in the Solution Explorer?
EDIT:
Here is what I do:
Create new project in VS2012: Visual C# -> Windows Store -> Blank App (XAML)
Open MainPage.xaml
Choose Image from Toolbox and draw an image element in the Design mode
The only working value for the Source attribute is '/Assets/Logo.png'. These are not working: 'C:\images\img.jpg', '/Assets/Logo2.png', 'Project_name;component/Assets/Logo.png', 'Project_name;component/Assets/Logo2.png', etc.
Here is the format you will want to use:
Project;component/ImagePath
Where Project is the name of the assembly (project in most cases) that you want to reference, and "component" specifies that the assembly being referred to is referenced from the local assembly.
So for the Logo2, you would do:
<Image Source="ImageDemo;component/Assets/Logo2.png" Name="custLogo"/>
Where "ImageDemo" is the project name and "Assets/Logo2.png" is the path inside project.
As for absolute paths, the following worked just fine for me:
<Image Source="C:\Images\001.jpg"></Image>
Ok, here's what I've found out:
Windows Store Apps (Win8) have got limited access to files. So I can only use the files which are shipped with the application. However, it is possible to get the user's files if he chooses them in a file picker.
There is also a great sample here.

Categories