Load image from folder in solution? - c#

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.

Related

MigraDoc image not found

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

How to get the system icon for any file/folder

How can I retrieve the system icon associated with a file/folder so that
I can show it in the list view adjacent to the file/folder name?
You need to use Icon.ExtractAssociatedIcon
Icon icon = Icon.ExtractAssociatedIcon(filepath);
Take a look at Icon.ExtractAssociatedIcon documentation
Note: this works only for files. For folders you need P/Invoke sample is here Edit: Page is now defunct, please refer to this copy on the Wayback Machine.
See Obtaining (and managing) file and folder icons using SHGetFileInfo in C#.

How to use a Resource Image in Word document?

I'm working on a project that will auto-generate Word and HTML reports. If they don't provide an Image to use in the header of the Word report, the customer wants to use their logo. I have the logo stored in the resources of one of the projects as a .jpg.
The method to add a picture to a range needs the path as a string to the image, without any overloads. I know that when a file is added to the resources of the project, it doesn't exist as it gets embedded inside the .dll that gets created. Is there no way to utilize that embedded resource in that method?
Do I need to copy that file as part of the install to the directory? I'm thinking that may be the easiest solution, however what should I do for testing purposes?
I was making this a lot harder than it needed to be.
All you have to do is make an image object from the resource:
Image img = Data.Properties.Resources.ImageFile;
Then after that, all you have to do is save it:
img.Save(#"C:\DestinationFolder\Image.jpg");
Now you have the file to use however you want. It won't matter if you're in debugging or not (as far as your Application.Run location) in the event you have it in a subfolder in your project (as you should), but wouldn't necessarily have it in the same path at the end.
I was way over-thinking this the other day when I asked this question. Hopefully I'll be able to help someone else out who's having this same issue.

c# picturebox point to filesystem

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

How do I copy folder(and subfolders) from my solution content into IsolatedStorage?

I have a folder with content in my solution, it contains lots of subdirectories. I need to copy this folder into IsolatedStorage. I read msdn forum message where it was said that there are no means to get folder content from code. How do I solve this problem?
The forum ist right. There is no way to enumerate "Content" resources.
You could set build action "Resource" or "Embedded Resource" as some answers suggest - then there are ways to enumerate the resources using ResourceManager or similar means. But I wouldn't recommend it as this will embed all resources into your assembly bloating it and making your app slow to load.
Here is a similar question (about enumerating image files). No solution though. The answer of Matt contains the only workaround: Prepare a list of filenames at design time and build this list into the app. Then instead of enumerating the files at runtime you read the filenames one by one from this list.
If you need this just for developing and testing then like the others I also recommend looking at the ISETool. You setup your app once with a reference storage and use the tool to save the state of the isolated storage. When you need to restore the state from isolated storage you can use the tool to copy the saved one back to the phone or emulator. An example for doing this can be found in this blog post.
Do you have a lot of files as well in the subdirectories? I see three solutions:
Set the file build action as Resource so they are embedded in the dll. You can retrieve the folder anme in the filename of the resources (MyAssembly.MyFolder.Filename.extension).But it will slow down the loading of your assembly and so the startup time of your application.
Set the files build action to Content so they are included in the XAP file but I'm not sure you can iterate the content without knowing the path
You can put the content in a zipped file on a remote server, get it on the first startup and use http://slsharpziplib.codeplex.com/ to dezip the content in Isolated Storage.
Mighter,
If I understand you right, you need the contents of your folder, along with the sub-directories in Isolated Storage, right? Putting them in your solution only gets them into the XAP.
You could use the Isolated Storage Explorer that comes bundled with the Windows Phone SDK 7.1 to manipulate file storage in Isolated Storage. This would be the easiest way to get your folder's content into Iso.
You could start learning about Isolated Storage Explorer [ISETool.exe] from here.
Hope this helps!

Categories