将上载的图像保存在WPF的项目文件夹中

daupos2t  于 2023-02-05  发布在  其他
关注(0)|答案(1)|浏览(140)

我想保存我上传的图像在项目文件夹中。但它被保存在库存\bin\调试\资产bin文件夹中我的项目。
将图像保存在文件夹中的目的,因为我只想将图像名称保存到数据库,当我需要显示图像时,我可以从/assets/xyz.png等文件夹中获取
这是我保存图像的代码,

private void btnUploadLogo_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog op = new OpenFileDialog();

            op.Title = "Select a picture";
            op.Filter = "All supported graphics|*.jpg;*.jpeg;*.png|" +
              "JPEG (*.jpg;*.jpeg)|*.jpg;*.jpeg|" +
              "Portable Network Graphic (*.png)|*.png";
            if (op.ShowDialog() == true)
            {
                string appPath = Path.GetDirectoryName(System.AppDomain.CurrentDomain.BaseDirectory) + @"\assets\";
                var fileNameToSave = DateTime.Now.ToFileNameFormat() + Path.GetExtension(op.SafeFileName);
                var imagePath = Path.Combine(appPath + fileNameToSave);

                if (!Directory.Exists(appPath))
                {
                    Directory.CreateDirectory(appPath);
                }

                File.Copy(op.FileName, imagePath);

                imgUpload.Source = new BitmapImage(new Uri(imagePath));
                panelImg.Visibility = Visibility.Visible;
            }
        }

因为我想把assets/xyz.png保存在我的数据库中。

4nkexdtk

4nkexdtk1#

私有void btnUploadLogo_Click(对象发送方,路由的事件参数e){打开文件对话框op =新的打开文件对话框();

op.Title = "Select a picture";
        op.Filter = "All supported graphics|*.jpg;*.jpeg;*.png|" +
          "JPEG (*.jpg;*.jpeg)|*.jpg;*.jpeg|" +
          "Portable Network Graphic (*.png)|*.png";
        if (op.ShowDialog() == true)
        {
            string appPath = Path.GetDirectoryName(System.AppDomain.CurrentDomain.BaseDirectory) + @"\assets\";
            var fileNameToSave = DateTime.Now.ToFileNameFormat() + Path.GetExtension(op.SafeFileName);
            var imagePath = Path.Combine(appPath + fileNameToSave);

            if (!Directory.Exists(appPath))
            {
                Directory.CreateDirectory(appPath);
            }

            File.Copy(op.FileName, imagePath);

            imgUpload.Source = new BitmapImage(new Uri(imagePath));
            panelImg.Visibility = Visibility.Visible;
        }
    }`private void btnUploadLogo_Click(object sender, RoutedEventArgs e)
    {
        OpenFileDialog op = new OpenFileDialog();

        op.Title = "Select a picture";
        op.Filter = "All supported graphics|*.jpg;*.jpeg;*.png|" +
          "JPEG (*.jpg;*.jpeg)|*.jpg;*.jpeg|" +
          "Portable Network Graphic (*.png)|*.png";
        if (op.ShowDialog() == true)
        {
            string appPath = Path.GetDirectoryName(System.AppDomain.CurrentDomain.BaseDirectory) + @"\assets\";
            var imagePath = Path.Combine(appPath + op.SafeFileName);

            if (!Directory.Exists(appPath))
            {
                Directory.CreateDirectory(appPath);
            }

            File.Copy(op.FileName, imagePath, true);

            imgUpload.Source = new BitmapImage(new Uri(imagePath));
            panelImg.Visibility = Visibility.Visible;
        }
    }

相关问题