如何以编程方式在 Visual Studio Resources 文件夹中上传图片?

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

我正在尝试将图像上传到图像框中,然后将该图像保存在 Visual Studio 解决方案资源管理器中。我设法浏览并显示了图像,但我需要有关如何保存图像的帮助。这是我的代码,感谢您的帮助。

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

阅读评论后,我可以指导您实现目标:在下次程序运行时提供图像 运行。

您不能以编程方式将图像添加到您的 visual studio 解决方案。

话虽如此,您可以做的是:

  • (可选)将图像保存在应用程序的文件夹中,这样即使删除了原始图像,您仍然可以看到它
  • 将图片的路径保存在文本文件中(原始文本,xml,json),格式由您选择。
  • 当程序加载时,读取文本文件并加载图像

如果将图像保存在特定文件夹中,您还可以简单地遍历该文件夹中的所有文件来加载所有已保存的图像,而不必将路径保存在文本文件中。

要保存图像,您可以使用:

using System.IO;

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

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

这里的原路径就是你要保存的图片的路径。知道您已经使用文件对话框加载图像,我假设您知道如何获取此路径。然后在程序为 运行 时加载图像:

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