Image.FromFile [英] Image.FromFile

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

问题描述

嗨。


我想打开一张图片。但是,我不知道如何以相对的方式做到这一点:

我有一个带图片的目录:图片。

它位于我的工作目录中。


那么,我怎样才能打开图像?


目前,我已经拥有了它,它不起作用:

图片newImage = Image.FromFile(" ../ Picture / Computer.jpg");


我也试过这个:

Image newImage = Image.FromFile(" Picture / Computer.jpg");


但是,同样的结果:它找不到文件。


有什么想法吗?

Hi.

I want to open an Image. But, I don''t know how to do it in a relativ way :
I have a directory with pictures : Picture.
It is located in my work directory.

So, how can I open the image ?

For the moment, I have that, and it does not work :
Image newImage = Image.FromFile("../Picture/Computer.jpg");

I have tried this also :

Image newImage = Image.FromFile("Picture/Computer.jpg");

But, same result : it does not find the file.

Any idea ?

推荐答案

您好Mathieu,


如果你是使用Visual Studio .Net记住代码目录与启动目录不同。因此,如果您的工作目录与代码目录相同,则需要执行Image.FromFile(" ../../ Picture / Computer.jpg");


。 ..意味着转到父目录,这会导致你进入bin,所以再返回一个来到你的工作目录。


快乐的编码!

Morten Wennevik [C#MVP]
Hi Mathieu,

If you are using Visual Studio .Net remember that the code directory is not the same as the startup directory. So, if your work directory is the same as the code directory you need to do Image.FromFile("../../Picture/Computer.jpg");

... means go to parent directory, which causes you to be inside "bin", so go back one more to get to your work directory.

Happy coding!
Morten Wennevik [C# MVP]




" Morten Wennevik" <莫************ @ hotmail.com> aécritdansle message de

news:opr748tzlzklbvpo@morten_x.edunord ...

"Morten Wennevik" <Mo************@hotmail.com> a écrit dans le message de
news:opr748tzlzklbvpo@morten_x.edunord...
Hi Mathieu,

如果您使用的是Visual Studio。 Net记住,代码目录是
与启动目录不同。所以,如果你的工作目录是你需要做的代码目录,那么



Image.FromFile(" ../../ Picture / Computer.jpg" ;);
..意味着转到父目录,这会使您进入bin,所以
Hi Mathieu,

If you are using Visual Studio .Net remember that the code directory is not the same as the startup directory. So, if your work directory is the
same as the code directory you need to do
Image.FromFile("../../Picture/Computer.jpg");
.. means go to parent directory, which causes you to be inside "bin", so



再返回一个以进入您的工作目录。


使用Application.StartupPath,我注意到我在:

\bin\debug


但问题是,当我发布时,它会不会这样?

(在发行版中,我不会在\ debug,nop?)


go back one more to get to your work directory.

With the Application.StartupPath, I have noticed that I am in :
\bin\debug

But, the problem is that when I will do a release, will it be ok like this ?
(in the release, I won''t be in \debug, nop ?)




" Mathieu Chavoutier" <无**** @ no.spam>在消息中写道

新闻:Oi ************** @ TK2MSFTNGP09.phx.gbl ...

"Mathieu Chavoutier" <no****@no.spam> wrote in message
news:Oi**************@TK2MSFTNGP09.phx.gbl...

" ; Morten Wennevik <莫************ @ hotmail.com> écritdansle message de
新闻:opr748tzlzklbvpo@morten_x.edunord ...

"Morten Wennevik" <Mo************@hotmail.com> a écrit dans le message de
news:opr748tzlzklbvpo@morten_x.edunord...
您好Mathieu,

如果您使用的是Visual Studio .Net,请记住代码目录与启动目录不同。因此,如果您的工作目录与您需要执行的代码目录相同
Image.FromFile(" ../../ Picture / Computer.jpg");
Hi Mathieu,

If you are using Visual Studio .Net remember that the code directory is not the same as the startup directory. So, if your work directory is the
same as the code directory you need to do
Image.FromFile("../../Picture/Computer.jpg");

..意味着转到父目录,这会导致你进入bin,所以

.. means go to parent directory, which causes you to be inside "bin", so


再返回一个以进入你的工作目录。

使用Application.StartupPath,我注意到我在:
\ bin \ devbug

但问题是,当我要发布时,将会这样就好了

(在发布中,我不会在\ debug,nop?)


go back one more to get to your work directory.

With the Application.StartupPath, I have noticed that I am in :
\bin\debug

But, the problem is that when I will do a release, will it be ok like this
?
(in the release, I won''t be in \debug, nop ?)




否,它将在发布版本中破解。我会使用条件

编译(我会做的几次之一)根据构建版本不同地定义字符串

或者让构建执行后构建步骤和

将文件复制到特定的运行时文件夹。



No, it will break in the release version. I would either use conditional
compilation(one of the few times I would) to define the string differently
based on the build version or have the build perform a post-build step and
copy the files to a particular runtime folder.



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

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