image load Visual Studio 2010 c# - 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.

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.

VS 2015 - Created solution folders are not appearing on file system

I think this is a known problem with real folders and solution folders in visual studio. I'm facing now the same issue. I got a project handedover from other developer and he already created, directly under the solution some folders and sub folders. All these directories and sub directories are appearing in visual studio solution explorer and also on the file system. As I mentioned, I'm using VS 2015. Here a screenshot:
I have no idea how he did this or if he used any tool for that.
When I try to create a sub folder under SQL-Scripte folder and add some files to it, then the added files are appearing directly under the solution directory. See this screenshot:
Any help is highly appreciated :).
Thanks!
Actually the folders that you create as solution folders, are virtual ones that you cannot see on the file system. To be able to see the folders on both sides (VS and file system), I managed to fix this with the following work around:
1- Create a solution folder (let us name it newSql) under your SQL-Scripte folder
2- Add the files to it.
3- You will notice that the files are located directly under your solution folder in the file system. Exactly as you mentioned in the second screenshot.
4- Now close your visual studio and navigate to your solution directory.
5- Create under the folder SQL-Scripte (on file system), a folder named newSql and copy the files from your solution directory (in your case the SQL file that starts with 1.17....) to this newSql physical system folder.
6- Then open your (.sln) file with Notepad or Notepad++
7- Locate the entry for the new added newSql folder and there change the path to point to your new file system newSql folder. Like this: SQL-Scripte\newSql\1.17.sql
8- Save and again open VS. If you are using TFS then you should right click on the 1.17.sql file and click add to source control and check in.
Like this, you will see the folder on both sides and you can manage it only from VS.
Hope it helps.

Changing output path of XAML files in VIsual Studio 2015 Universal App

I have a VCXProj file for a Universal Windows app that compiles some xaml files
<Page Include="..\View\ViewModel.xaml" />
One result of this is the xaml file is deployed as part of the package as a resource: ms-appx:///AppNamespace/ViewModel.xaml
If the file path was instead "View\ViewModel.xaml" then the resource path would also be "AppNamespace/View/ViewModel.xaml"
Unfortunately, since it is in the parent directory, it gets flattened and goes in the root. I want a way to specify the output path, rather than let the MSBuild system preserve it, something like
<Page Include="..\View\ViewModel.xaml">
<TargetPath>View\MyViewModel.xaml</TargetPath>
</Page>
But I cannot for the life of me find a way to do this. Is there anything out there?
I think this is by design, I couldn't find an option in VS tool that can specify the output path of a xaml file.
By default if you create a View folder in your project from VS, and create a new Page in this View folder, the VS compiler will automatically extract this XAML file out of the View folder and put it in the root folder of your project folder. And even you created one folder in the project from VS, this folder will not be created actually in your project physical folder.
For example, I created a folder named "Test" from VS, and I copied an image into this folder. The path of this image is actually like this:<Image Include="C:\Users\(pc-account)\Pictures\1.jpeg" />, and you will not find the "Test" folder in the physical path of this project. Now if we create a folder also named "Test" in the physical path, not only from VS, and copy another image into this folder, open this project from you VS, right click the "Test" folder, by adding existing item to select the second image into this "Test" folder, then you can see from VS:
And in the Appx folder of your project it looks like this:
As you can see, the first image which is added to the project from VS is extracted from the Test folder to the root folder, and the second image which is manfully added to the project is still in the Test folder.
You can see the path of this two images in the VCXProj file like this:
I'm afraid there is no options in VS tool that can automatically do this work, we can only
manually create a folder with the same name as one created from VS tool.
copy the .xaml, .xaml.cpp, and .xaml.h file into this manually-created folder.
add existing item from vs to this folder.
debug your project then you can see this View2 folder in the Debug folder.
in the meanwhile, in your VCXProj file you can see this xaml page's path:

Unity3D loading resources after build

I have many sets of images (PNG) that are placed in different subfolders inside the Resources folder on the Assets of the project. When working on the editor I'm able to load the images from the different subfolders without a problem, by just simply use the Resources.Load() command and providing the path to the specific image that I'm trying to load, such as:
firstLeftCC = Resources.Load("Case2/Left/CC/IMG-0004-00001", typeof(Texture2D)) as Texture2D;
In this example the image "IMG-0004-00001" is placed in the CC folder, and the CC folder is inside the Left folder, and the Left folder is inside the Case2 folder, and finally the Case2 folder is in the Resources Folder.
But after building the project, for a Windows application, when I run the .exe file it doesn't load any of those images. After some research, it seems that the problem is related with the presence of the subfolders inside the Resources, since the path given to the Resources.Load() function to load the image doesn't exist in the build.
I would like to know if anyone knows a solution for this problem, or if it is possible to load the images from a given folder instead of trying to load them from the Resources folder.
Thanks in advance for the help.
Optional method of loading your PNG file during run time.
You can leave the Case2 in the Asset Folder during development. After you build for Windows, go to folder the exe built file is. Let's assume it is called Game.exe. There will be a folder called Game_Data in the-same directory the Game.exe executable file is.
Copy the Case2 folder to the Game_Data folder.
It should now look like this .... Game_Data/Case2/Left/CC/IMG-0004-00001.PNG
Now with code below, you can easily read your png files with the code below:
Texture2D firstLeftCC = null;
byte[] imageBytes;
string imagePath = Application.dataPath + "/Case2/Left/CC/IMG-0004-00001.PNG";
if (File.Exists(imagePath))
{
Debug.Log("Exist");
imageBytes = File.ReadAllBytes(imagePath);
firstLeftCC = new Texture2D(10, 10);
firstLeftCC.LoadImage(imageBytes);
}
Now you can freely use firstLeftCC as a Texture2D
Note: Unity creates .meta file/folder for every file/folder in the Assets directory. When when you copy the directory,IMG-0004-00001.PNG will have another file called IMG-0004-00001.PNG.meta. You should either NOT place your Case2 folder in the Assets folder during development or you will have to delete these yourself.
Include using System.IO;
Alternatively to Resources.Load, you can load images from a given folder using the WWW class. To do this the URL must use the file:// protocol and contains the path to the files according to your OS.
http://docs.unity3d.com/ScriptReference/WWW.html
http://answers.unity3d.com/questions/517414/how-to-use-www-to-load-local-files.html

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

Categories

Resources