Image not found after ClickOnce publish - c#

I am creating a PDF document, using MigraDoc. The image is located in the root folder of the project, so I use this code to insert it in the PDF-document:
Image img = tfLogoDate.AddImage("../../db_logo_500p.png");
The Build Action is set to 'Content':
This works fine during development, using the Visual Studio debug build. But when I create a build with ClickOnce (publish), it says that the image is not found.
Am I using the wrong Build Action? Or is it because the relative path changes, when I use ClickOnce to publish my application?

I found a way to make it work, by changing the image location. I now use this instead:
// I have added the 'pack://(...)' in front of the path
Image img = tfLogoDate.AddImage("pack://application:,,,/../../db_logo_500p.png");
I had to use the following settings for the PNG-file:
Build Action: Content
Copy to Output Directory: Do not copy
If anyone knows exactly why the above works, I would be delighted to know. But the most important thing is, that it works...

Related

how to set image in Visual Studio C# Windows Forms with Image.FromFile

So I'm trying to set a custom image for a form application I've made. The images I'm trying to target are in a folder called "Images" on the same level as my Solution file. The solution file is a C# windows forms (net core framework) solution. It's a basic form app that I want to display an image based on a users selection, however right now I get an unhandled exception everytime I try to set the image with this code:
picFood.Image = Image.FromFile("../../Images/burger.jpg");
The exact error is "System.IO.FileNotFoundException: ../../Images/burger.jpg"
In another totally unrelated solution this works. Folder structure is the same. A folder called Images, on the same directory level as the .sln file holds the images there. They're in my solution explorer and everything. I've tried this with one "../" and no "../" as well so I'm not sure what to do from here.
Files with relative paths are opened relative to the working directory of your application.
In this case, when launching from within Visual Studio, the default is the bin folder where the compiled application is put by default.
So if your binary is in <project dir>/bin/Debug/App.exe this path will resolve to <project dir>/Image/burger.jpg.
If you have changed something in your build configuration, or your application switches directory at runtime (e.g. via Directory.SetCurrentDirectory), this path may be different than you expect.
To understand your issue, I suggest you start looking at what your working directory is. You can obtain that in your code via Directory.GetCurrentDirectory().
You can also resolve your relative path using Path.GetFullPath.
Print these two values to see where your program attempts to load the file from.
Keep in mind that any image files you put in the solution/project folder will need to be copied with your binary if you want to use them.
To use relative paths without .. you can copy them alongside your binary during compilation, see:
VS2010 How to include files in project, to copy them to build output directory automatically during build or publish and Copying Visual Studio project file(s) to output directory during build for how to do that.

XAML Image source

I'm writing a WPF app with VS2015. I have a User Control that contains an Image control. I have my image on the file system in an Images subfolder. In the Design mode, the image showed up fine, but when I ran it, it didn't. I specified the image location thus:
Source="Images/ball.png"
within the Image tag. I tried all the obvious things, like copying the folder to the bin\Debug folder and such. Experimenting, this error message popped up over the Source tag:
"Could not find a part of the path 'C:\Program Files (x86)\New folder\Microsoft Visual Studio 14.0\Common7\IDE\Images\red-ball.png'"
I finally got it to show up by giving it the FULL PATH to the image, but I really don't think I should have to. Isn't there a way to give it a relative path that's relative to the application and not the location of the IDE?
First, make sure that your Images subfolder is relative to your project ie:
Project
Images
ball.png
Then with "ball.png" added to your project, set its Build Action to "Resource". Your link will now work as you expect.
I'm guessing its looking for the image in the place you mentioned because it couldn't find it in the resources where it wants to look. Note that sounds and video clips do not work this way, you have to copy those to your build output or use a pack URI.

Access a jpg in an Image folder of a project

I am working on a program that generates a PDF using a third party .dll. I am trying to add a header image to the PDF by sending in the string path of the jpg (Logo.jpg) in the project that is in a folder I created (Images), but it is not working using code like this..
PDFPage.AddHeaderImage("String path of the jpg in the project")
PDFPage.AddHeaderImage("~/Images/Logo.jpg))
There is a "could not find file" exception.
It works fine when I point to a file on my computer like this...
PDFpage.AddHeaderImage("C:/Source Code/Source/images/Logo.jpg");
But, I do not want to point to a file on my computer. I want to point to a the file in the Images folder in my project where I put Logo.jpg. I also put the image in Resources, but do not know what the string would be to access it. Either way would be fine.
the .AddHeaderImage is expecting a string path.
If you set Build Action = Content and Copy To Output Directory = Copy Always on the properties of the image file in Solution Explorer in Visual Studio, then the image file will be output to your bin folder along with the compiled application. You can then use a path relevant to the executable.
PDFPage.AddHeaderImage("Images/Logo.jpg));

image load Visual Studio 2010 c#

I'm having problem loading images to my project.
I have the image in a folder named microassig this folder is on my desktop, this folder is to be sent online to my tutor, thats my line of code:
private Image imageOpen = Image.FromFile("\microassig\openOff.bmp");
I don't want to put the directory c:/ because is directory will be different from mine hence why i'm just using the ("\microassig\openOff.bmp");
The problem is that the image doesn't load.
Locate your project's
bin\debug folder
You can do this by right-clicking on your project solution and clicking Open in Windows Explorer.
Store/Save the file as
openOff.bmp
Then you can just do:
private Image imageOpen = Image.FromFile("openOff.bmp");
You should not be using the Image.FromFile. You need to add the image as a resource into the project. See this link: http://msdn.microsoft.com/en-us/library/7k989cfy(v=vs.80).aspx
The \ doesn't imply your desktop. You should use GetFolderPath for that (See C# Get Special Folder)
There are a few ways to go about this. You could add your image as a resource.
Or, more in the line of what you are already doing you could change your code to something like this:
private Image imageOpen = Image.FromFile("openOff.bmp");
Then move the openOff.bmp to your bin folder (where the exe is saved).
While you keep the openOff.bmp in the same folder as your executable it should find it.

XamlParseException on System.Drawing.Icon in WPF C#

I set up a NotifyIcon to store my application in the system tray. For the Icon I was using
myNotifyIcon.Icon = new System.Drawing.Icon(#"c:\MyIcon.ico");
and this works. However I would like to store my icon in my project directory and refer to it where I deploy my app rather than have some external image. i tried just
myNotifyIcon.Icon = new System.Drawing.Icon("MyIcon.ico");
and this where i get the XamlParseException when I try to run the app. The image does exist in the root of my project. Does anyone know the solution to this? Should I be using a PNG instead? I tried researching Pack Uris but i was just getting more confused and i am not sure thats what i need here. Thanks!
It sounds like you are not copying the icon to the deployment directory on build. Make sure the .ico is sitting alongside your .exe rather than just in your project directory.

Categories

Resources