在路径

时间:2017-07-05 19:16:17

标签: c# image file picturebox guid

因此,我复制了图像并通过GUID重命名了这些图像并且没有遇到任何问题。 但是,当我想将此图像打开到例如图片框中时,我得到了这个: enter image description here

debuger中带路径的生成名称如下所示:@"images\full_45e72053-440f-4f20-863c-3d80ef96876f.jpeg"

如何打开此文件?

这是我的代码,它向我展示了这个问题:

private void picBoxMini2_Click(object sender, EventArgs e)
        {
            string dir = ConfigurationManager.AppSettings["imageFolderPath"].ToString();
            string imgName = this.picBoxMini2.ImageLocation;
            string[] tmp = imgName.Split('_');
            this.picBoxMain.Image = Image.FromFile($"{dir}\full_{tmp[tmp.Length - 1]}");
        } 

ImageLocation包含100%的信息,我在这种情况下投保了:

 string dir = ConfigurationManager.AppSettings["imageFolderPath"].ToString();
            if (imgs.Length >= 1)
            {
                this.picBoxMain.Image = Image.FromFile($@"{dir}\full_{imgs[0]}");
                this.picBoxMain.ImageLocation = $@"{dir}\full_{imgs[0]}";
                this.picBoxMini1.Image = Image.FromFile($@"{dir}\85_{imgs[0]}");
                this.picBoxMini1.ImageLocation = $@"{dir}\85_{imgs[0]}";

                this.picBoxMini2.Image = null;
                this.picBoxMini2.ImageLocation = null;
                this.picBoxMini3.Image = null;
                this.picBoxMini3.ImageLocation = null;
            }
            if (imgs.Length >= 2)
            {
                this.picBoxMini2.Image = Image.FromFile($@"{dir}\85_{imgs[1]}");
                this.picBoxMini2.ImageLocation = $@"{dir}\85_{imgs[1]}";
            }
            if (imgs.Length == 3)
            {
                this.picBoxMini3.Image = Image.FromFile($@"{dir}\85_{imgs[2]}");
                this.picBoxMini3.ImageLocation = $@"{dir}\85_{imgs[2]}";
            }

1 个答案:

答案 0 :(得分:1)

问题在于这一行:

this.picBoxMain.Image = Image.FromFile($"{dir}\full_{tmp[tmp.Length - 1]}");

您忘记了@告诉编译器将字符串视为逐字处理。没有那个标记,它认为你的路径有一个嵌入式的ctrl + f字符(来自\full中的\ f),这不是Windows中文件名的合法字符。

您的选择是:

  • 包括@this.picBoxMain.Image = Image.FromFile($@"{dir}\full_{tmp[tmp.Length - 1]}")
  • 转义目录分隔符:this.picBoxMain.Image = Image.FromFile($"{dir}\\full_{tmp[tmp.Length - 1]}")
  • 使用System.IO.Path.Combine做一些其他动画来自动处理目录/文件名分隔符。 this.picBoxMain.Image = Image.FromFile(System.IO.Path.Combine(dir, $"full_{tmp[tmp.Length - 1]}"))(这可能是最安全,最便携的解决方案,但对您的需求可能有点过分。)