处理完成(或最终会)以下事项的项目:
前两个步骤工作正常,我已检查过图像(仅使用测试阶段的两个图像文件)放在子文件夹中。
我使用Directory.GetAllFiles()
函数初始化了一个字符串数组,以便执行此操作,这意味着此数组包含正在复制和操作的所有文件的路径。作为此过程的一部分,我将每个文件路径添加到List<Bitmap>
,以便它们能够显示在图片框中。
但是,每当我尝试运行该功能以在图片框中显示图像时,整个应用程序都会崩溃。没有异常抛出或任何东西,只是整个事情停止工作。我不知道发生了什么。
我使用picbox.Image =
对List<Bitmap>
上的元素或使用picbox.ImageLocation =
的文件路径尝试了此操作,但都没有成功。
应该实现这一目标的代码如下所示:
public void saveLatestImages()
{
//specific path for My Pictures only
string testImagesPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Test Images";
//if there is a Pictures folder
if (Directory.Exists(testImagesPath))
{
//get number of files in folder
int fileCount = Directory.GetFiles(testImagesPath).Count();
//more than one file in folder
if (fileCount > 0)
{
//create data structures to store file info
//filePaths holds path of each file represented as a string
string[] filePaths = Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Test Images");
//for each file in Pictures...
for (int index = 0; index < fileCount; ++index)
{
//get name of image at current index
imageName = filePaths[index];
//separate the part relating to the patient name (everything before (DD/MM/YYYY))
string subSpecifier = imageName.Split('\\').Last();
subSpecifier = subSpecifier.Split('_')[0];
//add to root directory to form subfolder name
subDirectory = Path.Combine(rootDirectory, subSpecifier);
//subdirectory name formulated, check for pre-existing
//subfolder does not exist
if(!Directory.Exists(subDirectory))
{
//create it
Directory.CreateDirectory(subDirectory);
}
//otherwise, file will be added to existing directory
//take everything from end and folder\file division to get unique filename
string fileName = imageName.Split('\\').Last();
//add this to the existing subDirectory
fileName = Path.Combine(subDirectory, fileName);
//copy the image into the subfolder using this unique filename
File.Copy(imageName, fileName, true); //true gives instruction to overwrite any existing file with the same path
//add full filename to list of bitmap images
images.Add(new Bitmap(fileName));
//update the shortcut to the file in the image storage shortcut folder
shortcutDirectory = getShortcut(subSpecifier, fileName);
}
}
}
}
public void displayLatestImages()
{
//there are images in list
if (images.Count > 0)
{
//picbox defaults to first image
picboxImage.ImageLocation = Path.GetFileName(images.First().ToString());
}
//check for images before or after current image to enable buttons
checkFirstLast();
}
这显示了将图像保存到新文件位置的所有内容,然后应该允许显示添加到列表中的第一个图像。
非常感谢任何关于如何纠正这一点的建议!
谢谢, 标记