FileNotFound当我使用Image.FromFile() [英] FileNotFound when I use Image.FromFile()

查看:212
本文介绍了FileNotFound当我使用Image.FromFile()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用Image.FromFile(string)方法在这种情况下:

I use Image.FromFile(string) method in this situation:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GVEMO
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            PaintBackground();
        }
        public void PaintBackground() {
            gameBoard.Image = Image.FromFile("gvemoBack.jpg");
        }
    }
}



游戏键盘是图片框的名称。但是,当我开始这个程序,我得到异常FileNotFound。图片是在主目录下我试试这个映像复制到所有的项目目录,但异常(使用文件夹仓,类,属性等目录)依然存在。在哪个目录必须是此图像还是什么我做错了什么?我使用VS2012和.NET Framework 4.5版。 THX

gameBoard is name for pictureBox. But when I started this program, I get exception FileNotFound. Image is in main directory (directory with folders bin,classes,properties etc.) I try copy this image into all project directories but exception remain. In which directory must be this image or what I do wrong? I use VS2012 and .NET framework version 4.5. Thx

推荐答案

图片必须是在当前工作目录。在启动时,这就是你开始你的应用程序,这可能是其中的可执行文件所在的(但不能是)的目录。要注意的是在此工作目录可以改变!

The image must be in the current working directory. At startup, this is the directory where you start your application from, this may be the directory where the executable resides (but must not be). Beware that this working directory can change!

而不是使用的路径,我建议你使用另一种机制。如果你使用了 FileOpenDialog 例如当前的工作目录可以被设置为不同的东西。所以,你会过得更好使用资源或映像所在的目录设置。

Rather than using a path I would suggest you to use another mechanism. The current working directory can be set to something different if you use a FileOpenDialog for example. So you would be better off using a resource or a setting for the directory where the images reside.

这篇关于FileNotFound当我使用Image.FromFile()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆