Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#:从资源加载图像时FileNotFoundException_C#_Image_Resources_Filenotfoundexception - Fatal编程技术网

C#:从资源加载图像时FileNotFoundException

C#:从资源加载图像时FileNotFoundException,c#,image,resources,filenotfoundexception,C#,Image,Resources,Filenotfoundexception,我试图从我的资源文件中加载一组图像,但由于某种原因,我得到了FileNotFoundException。图像名称如下所示: “image01.png”、“image02.png”、,“image10.png”,image11.png 最后,我希望能够在屏幕上显示所有的图像 以下是我所拥有的: String imgName; int row = 0, col = 0; for (int i = 1; i <= 15; i++) {

我试图从我的资源文件中加载一组图像,但由于某种原因,我得到了FileNotFoundException。图像名称如下所示: “image01.png”、“image02.png”、,“image10.png”,image11.png

最后,我希望能够在屏幕上显示所有的图像

以下是我所拥有的:

  String imgName;
        int row = 0, col = 0;

        for (int i = 1; i <= 15; i++)
        {
            //get the name of the current image
            if (i < 10)
                imgName = "image0" + i + ".png";
            else
                imgName = "image" + i + ".png";

            Image img = null;
            try { 
                img = Image.FromFile(imgName);//read the image from the resource file
            }
                catch (Exception e) { Console.WriteLine("ERROR!!!" + e); }
}
截图:


我还修复了第56行的一个类型,从“PictureForm.PuzzForm.”到“PicturePuzzle.”,但仍然不走运。

您没有指定加载文件的路径。它们将从程序集运行的位置加载

请注意,Image.FromFile并没有加载嵌入式资源,而是从磁盘加载.png。我想这就是您想要的

检查VisualStudio中图像的属性,并确保“复制到输出”目录为“如果较新,请复制”或“始终复制”。下面是一个屏幕截图(在我的示例中,它是一个光标资源,但对图像的想法相同)

更新

如果已将图像嵌入EXE或其他文件中,则可以使用类似的代码

System.Reflection.Assembly thisExe;
thisExe = System.Reflection.Assembly.GetExecutingAssembly();
System.IO.Stream file = 
    thisExe.GetManifestResourceStream("AssemblyName.ImageFile.jpg");
this.pictureBox1.Image = Image.FromStream(file);
()

注意


您可以将图像嵌入到二进制文件(通常是.exe)中通过将属性生成操作设置为Embedded Resource,或通过将生成操作设置为Content,将它们作为单独的文件保留。如果保留为Content,请将Copy to Output Directory设置为True。

代码中没有说明文件的位置,因此默认为文件不在的位置。如果文件位于同一位置n作为您的exe,然后尝试以下操作 imgNmae=“./image0”+i+”.png”


调整相对路径以说明文件的实际位置。

您的“资源文件”是什么“,它存储在哪里,您的代码从哪里执行,与该资源文件相关?嵌入的资源文件内置在输出程序集中。在这段代码中,您认为您是在告诉计算机在程序集中查找文件吗?您似乎试图从文件系统加载它们。@adv12:看起来他并不是真的试图加载嵌入式资源,而是从磁盘加载.png文件。我的资源文件是我项目文件中的一个文件。路径是这样的:C:\Users\me\blah\Project\MyImageProject\resources它不是类似于java吗?在java中,您的项目可以有一个res文件夹,您可以将图像放在其中,然后从那里读取图像?如果他的文件与他的.exe位于同一位置,则无需添加路径的
/
部分。它已经在那里了。忽略我之前的评论——我可以从你在OP上的评论中看出,你在解读他的帖子,暗示他想要本地的文件。抱歉。@BrianWarshaw:我更新了我的答案,以说明这两种可能性。谢谢,我试过这么做,但最后一行有一个错误。错误是:“…不包含'picturebox1'的定义,并且找不到'picturebox1'接受MyProject.MyClass'的第一个参数类型的扩展方法”可能是因为我的第二行到最后一行是这样的:System.IO.Stream file=thisExe.GetManifestResourceStream(“AssemblyName.”+imgName);您需要输入程序集的实际名称(例如MyProject)和图像文件的实际名称。“AssemblyName”是程序名称的占位符。
System.Reflection.Assembly thisExe;
thisExe = System.Reflection.Assembly.GetExecutingAssembly();
System.IO.Stream file = 
    thisExe.GetManifestResourceStream("AssemblyName.ImageFile.jpg");
this.pictureBox1.Image = Image.FromStream(file);