Loading Image from Code Using Relative Path in Windows Forms

Loading image from code using relative path in Windows Forms

For my program, Path.GetDirectoryName (Assembly.GetExecutingAssembly().Location) returns
C:\code\test\Junk\bin\Debug.

cell.Value = Image.FromFile(
Path.Combine (
Path.GetDirectoryName (Assembly.GetExecutingAssembly().Location),
"Resources/warning_Icon"));

Of course, usually you would embed the resources in your assembly unless you want to change them without a recompile.

How to take a photo from relative path for Background in a Windows Form, Visual Studio?

Look at: Path.Combine

this.BackgroundImage = Image.FromFile(Path.Combine(Application.StartupPath, @"..\..\..\imgs\test.jpg"));

In my case, I've placed test.jpg in folder imgs which is at project's root level.

How do I use a relative path instead of an absolute path

Based on the paths you've supplied if the current working directory is BirdTouchScreenBasic then @".\pigeonImages\pigeon5.png" would be a correct path. If you want these images to be accessible regardless of where the program runs you should add them to your solution and make it so they're copied to the output directory. If that is the case then you can use a relative path throughout your code without problems. If you make the paths @".\pigeonImages\fileName.ext" but don't actually structure your project so that folder and the files within it exist in the CWD then you're going to have problems.

If you don't know how to add the images a resource in your C# project here are basic steps; right click the proj file and select add then new folder, create the pigeonImages folder. Right click the folder and select add then new item. Navigate to the image file you want to add and select it, press ok. Right click the image you just added, select properties. There's is an option called "Copty to Output Directory" set that to Copy if newer or Copy Always.

Loading PictureBox Image from resource file with path (Part 3)

The path should be something like: "Images\a.bmp". (Note the lack of a leading slash, and the slashes being back slashes.)

And then:

pictureBox1.Image = Image.FromFile(@"Images\a.bmp");

I just tried it to make sure, and it works. This is besides the other answer that you got - to "copy always".

Windows forms ImageList - Add images with Relative path - No file copy

If you want to use file paths, for items that are in your project, you must set the "Copy to Output Directory" property to "Copy Always" or "Copy if newer", otherwise it won't be in the bin folder, and then you'll be trying to pass a path to a file that doesn't exist. Build action isn't all that important in this scenario.

If you want to use compiled resources, and reference them via the Resources object, see the rest of my answer. I assume you are using Visual Studio, 2005 or later.

To add an image as a compiled resource to a clean Windows Forms project, so that you can access it via Resources.SomeName do the following:

  1. In Solution Explorer, under the windows forms project (mine is called WinFormsApplication1), expand the "Properties" folder. By default this folder should contain: AssemblyInfo.cs, Resources.resx, Settings.settings.
  2. Double-click on the Resources.resx file. This will open an editor for it. You'll probably see a table of strings, with columns "Name", "Value", "Comment".
  3. Click the drop-down arrow on the "Add Resource" button, and select "Existing File", which will allow you to browse to the image you want to add.
  4. You should now see the image appear in a gallery of sorts. Mine has the name TestImage

Now when you edit the code (mine is Fom1.cs), I can access the image as a System.Drawing.Bitmap as Properties.Resources.TestImage.

To my mind, this is the best way to do images that you want compiled into the application. If you want user-added images, you'll need to use OpenFileDialog, or something like that to get your file path. Then the Image.FormFile() will be what you want.

Databinding PictureBox to relative path in winform

I know this is long overdue but I should update the answer for anyone who stumble upon this problem the same as me. Thanks @TaW for the answer. In the binding.Format change the code to this:

String path = convertEventArgs.Value.ToString();
if (!path.StartsWith(Application.StartupPath + @"\Image\"))
{
convertEventArgs.Value = Application.StartupPath + @"\Image\" + convertEventArgs.Value;
}

This should prevent the string to be added each time the DataBinding is called and help bind the data to the control properly.



Related Topics



Leave a reply



Submit