I have a script lets say in folder 'root'. I have a folder inside root called 'Images', and inside images an image called 'logo.png'.
root
script.cs
Images
logo.png
I have the following code to create a section:
Document document = new Document();
Section section = document.AddSection();
And I have tried this:
section.AddImage("../../Images/logo.png");
and this:
section.AddImage("Images/logo.png");
to try and add an image, but both return 'image not found'.
I've already tried updating MigraDoc to v1.5 but it didn't seem to work.
The "script" gets compiled and you get an assembly that is executed. You are using relative paths and by default the image will be searched relative to the working directory of the process which most likely is relative to the folder with the assembly. With ASP.NET things can be even more complicated.
Tip: The DocumentRenderer class has a WorkingDirectory property. If you set it then images with relative paths will be searched relative to this working directory. Set it to "root" and the second path "Images/logo.png" should work.
Forgive me because I realize the age of this thread before providing an answer, but I too had the same issue that the accepted answer was unable to resolve.
In C# you are able to use HostingEnvironment.MapPath to reference relative paths. In your case, it'd look like:
section.AddImage(HostingEnvironment.MapPath("~/Images/logo.png"));
Related
I have some problems with loading from path a model.
axis = Content.Load<Model>("XYZ");
I try to change it to :
axis = Content.Load<Model>("C:\\XYZ");
But it gives me this error :
Error loading "C:\XYZ". Cannot open file.
I also googled that thing, but I found nothing, looked at this question : How Do I Load A XNA Model From A File Path Instead Of From The Content (eg. Model.FromStream but it doesnt exist) , but it didn't help me.
In the properties of your content project, you specify a root folder in your project. When content is built, it is placed in your bin\content directory matching the hierarchy of your content project.
Content is local to your content project, not absolute on disk. Loading an unprocessed model from an absolute path will not get your a model inside of XNA like you expect.
I'm using the MVVM structure and I wish to read a csv file, which I've added in the same folder as the viewmodel. I've tried to use the second answer in this question, but my variable var is null, so it doesn't seem to find the file. Beneath is an image of the folders and what I've done so far.
Can anyone help me get the destination of the file so I can use it as a stream?
You need to provide a path relative to your package, thus it should work if done like this:
var dest = App.GetResourceStream(new Uri(#"StartUpViewModels/" + destination, UriKind.Relative));
Here is also reference to answer to similar question.
Remember also to check if your resource file added to package has Build Action set to Content, otherwise if it's set to None, your file won't be added.
I'm trying to load an image from a folder in the solution, but I only get a error message that it's not found. What have I done wrong? The code below is in the MainForm.cs that is at the same level as the Resource folder. Help is preciated! Thanks!
// Images
Image imageCircle = Image.FromFile("Resources/circle.png");
// Set deafult picture on start
pictureBox1.Image = imageCircle;
Edit: Fixed Broken Links
Take a look at this MSDN article, it discusses Adding and Editing Resources and what your options are, and this MSDN article discussing Linked and Embedded resources using the Resource Designer.
Then select your file
Then you can access it like Madurika suggests.
i.e.
Image imageCircle = YourPojectName.Properties.Resources.YourFileNameHere;
It always take the path from the where executable is located(bin folder). So if you can access it using full path, problem will solved. Or you can have a configuration item for the root folder. then access like Image.FromFile(rootFolder+ "Resources/circle.png");. Anyway this issue wont be there when you deploy it.
And if you are using resource file,
<projectName>.Properties.Resources.<ImageName>;
will return the image.
The program is executed in bin/debug (the place where all .dll's are), put the Resources folder there.
i have a picture box in my win forms app. the question is, how can i make the image load from a directory within that application,
for example, i have a program and it lies under Debug directory. Now i have an image located under Debug/source/images/logo.png. I dont want to make use of resources file (embedded within the assembly)
How can i point it towards that path, instead of getting the image/picture from the local resources file.
Try this
String.Format(#"{0}\myDesiredPath\MyFile.MyExtension",System.Reflection.Assembly.GetExecutingAssembly().Location);
There are a number of ways to do this, you can use the ImageLocation property or the Load method to specify the path just take a look at the documentation here
i need to get the full folder path in a windows project using c#.I tried with path.getFulPath(filename).bt it returns the application path+filename.how can i get the actual path like "D:\eclipse_files\ads_data"?
A relative path such as myfile.txt is always resolved in relation to the current working directory.
In your case the current working directory seems to be D:\eclipse_files\ads_data so your relative file path gets resolved to D:\eclipse_files\ads_data\myfile.txt when you call Path.GetFullPath.
To solve the problem, either make sure that you start with an absolute path from the beginning, or, that your working directory is set correctly.
You can get/set the working directory using the Directory.GetCurrentDirectory and Directory.SetCurrentDirectory methods.
Your question is not very clear, but I think you're looking for this:
string path = Path.GetDirectoryName(filename);
If I have understood correctly, you have a filename, for example 'doc.txt', and you want to have a method to return the full path of this file regardless of where the application runs from?
If this is what you ask it is not possible. Have you considered that there might be several files called 'doc.txt' on your harddrives?
The best you can hope to do it to search all harddrives, and return a list of all files found with the same name, but that will just be ridicously slow.