简体   繁体   English

如何通过保存在 WPF 应用程序输出目录中的文件对话框使用上传的图像文件?

[英]How to use uploaded image file via file dialog which is saved in the output directory on WPF Application?

I have a question regarding including a file after it has been copied to the program debug directory.我有一个关于在将文件复制到程序调试目录后包含文件的问题。

I have a WPF data grid and in this data grid I display a list of staff with the columns name, age and photo.我有一个 WPF 数据网格,在这个数据网格中,我显示了一个包含列名称、年龄和照片的员工列表。 I can double click a row to open up a new window to update the details and this includes the photo.我可以双击一行打开一个新窗口来更新详细信息,这包括照片。

In my project directory, I have these folders which I have included in my project.在我的项目目录中,我将这些文件夹包含在我的项目中。 The default folder consists of .jpg files of default staff member photos and the userData folder will be used to store user uploaded photos.默认文件夹由默认员工照片的 .jpg 文件组成,userData 文件夹将用于存储用户上传的照片。

In my .csproj file I have included this:在我的.csproj文件中,我包含了这个:

<ItemGroup>
  <Content Include="img\**">
    <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
  </Content>
</ItemGroup>

This ensures that the img folder gets copied to the output directory upon build.这确保在构建时将img文件夹复制到输出目录。 I have also added a .txt file in the userData folder as otherwise it does not copy the folder.我还在userData文件夹中添加了一个.txt文件,否则它不会复制该文件夹。

When the user uploads the photo and hits save in my program, it does two things:当用户上传照片并在我的程序中点击保存时,它会做两件事:

  1. Copies the file from source to the output directory of relative path img\\userData\\filename.jpg将文件从source复制到相对路径img\\userData\\filename.jpg的输出目录
  2. Updates the SQLite record for that staff member's photo path, eg From img\\default\\staffmember1default.jpg to img\\userData\\newstaffmember1.jpg更新该员工照片路径的 SQLite 记录,例如从img\\default\\staffmember1default.jpg img\\userData\\newstaffmember1.jpgimg\\userData\\newstaffmember1.jpg

My program currently does not update the data grid to reflect changes so upon closing and running the program, it displays a blank photo for the staff member that had the photo change.我的程序目前不会更新数据网格以反映更改,因此在关闭并运行程序时,它会为更改照片的工作人员显示一张空白照片。 However, if I was to copy that photo into the included img\\userData directory in my project and reload the program it works.但是,如果我将该照片复制到我的项目中包含的img\\userData目录中并重新加载它可以工作的程序。

Is there a way to get around this and allow it to display the new photo from the userData directory?有没有办法解决这个问题并允许它显示来自userData目录的新照片? Is this whole method suitable or is there a better way of tackling this whole process?这整个方法是否合适,或者是否有更好的方法来处理整个过程?

After looking over the link @Clemens this is the solution I have come up with which seems to do the trick.查看链接@Clemens 后,这是我想出的解决方案,似乎可以解决问题。

private void uploadButton_Click(object sender, RoutedEventArgs e)
{
    OpenFileDialog openFileDialog = new OpenFileDialog();

    if(openFileDialog.ShowDialog() == true)
    {
        string filepath = openFileDialog.FileName;
        string filename = openFileDialog.SafeFileName;

        string local_path = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
        string parent_path = local_path + @"\ProjectName";
        string child_path = parent_path + @"\user";

        if(!System.IO.Directory.Exists(parent_path))
        {
            System.IO.Directory.CreateDirectory(parent_path);
        }

        if(!System.IO.Directory.Exists(child_path))
        {
            System.IO.Directory.CreateDirectory(child_path);
        }

        string target_path = child_path + @"\" + filename;
        File.Copy(filepath, target_path);
        photoBox.Source = new BitmapImage(new Uri(target_path));
        staff.photo_path = target_path;
    }
}

So instead of copying the file to the output directory of the program I decided to do it in the local AppData.因此,我决定在本地 AppData 中执行此操作,而不是将文件复制到程序的输出目录。 The file path also gets stored in the SQLite database and this finds the image fine upon load.文件路径也存储在 SQLite 数据库中,这会在加载时找到图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM