Doro
Doro

Reputation: 671

How to upload an image in Visual Studio Resources folder programmatically?

I am trying to upload an image into an image box and then save that image in Visual Studio Solution Explorer. I managed to browse and display the image but I need help to how to save the image. This is my code and thank you for any help.

OpenFileDialog openFileDialog = new OpenFileDialog();
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
    pbxImage.Load(openFileDialog.FileName);
} 

Upvotes: 0

Views: 2142

Answers (1)

ESD
ESD

Reputation: 545

After reading the comments I can direct you toward what you can do to achieve : Having an image available on the next time that the program is ran.

You cannot add an Image to your visual studio solution programmatically.

That being said what you can do is :

  • (optional) Save the image in the application's folder so that if the original is deleted you still have it
  • Save the path of the image in a text file (raw text, xml, json) the format is your choice.
  • When the program loads, read the text file and load the images

If you save the images in a specific folder, you can also simply loop through all of the files in that folder to load all of the saved images instead of having to save the paths in a text file.

To save the image you can use :

using System.IO;

if(!Directory.Exists(@"path to your images directory"))
    Directory.Create(@"path to your images directory");

File.Copy(@"original path", @"destination path");

The original path here is the path to the image you want to save. Knowing you allready loaded an image using a file dialog i'm assumming you know how to get this path. And then to load the images when the program is ran :

foreach(string img in Directory.GetFiles(@"path to your images directory"))
{
    code to load your images (img is the path as a string)
}

Upvotes: 1

Related Questions