C# 使用 System.Drawing.Image.FromFile 创建 Image 对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15897044/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating Image object using System.Drawing.Image.FromFile
提问by user1889838
I am trying to get the image dimensions of an image that user selects from list box. Image files are available on FTP server. I am displaying file names in a list box for users to select. Upon selection, I want to show the preview of image, for that I want to get dimensions so that I can resize it if i need to.
我正在尝试获取用户从列表框中选择的图像的图像尺寸。图像文件在 FTP 服务器上可用。我在列表框中显示文件名供用户选择。选择后,我想显示图像的预览,为此我想获得尺寸,以便我可以在需要时调整它的大小。
I am storing file name that is linked to currently selected list item into a string variable. I know that path on the server. I am using following code to create the Image object, but having no luck
我将链接到当前选定列表项的文件名存储到字符串变量中。我知道服务器上的路径。我正在使用以下代码创建 Image 对象,但没有运气
try
{
string dir = Session["currentUser"].ToString();
System.Drawing.Image img = System.Drawing.Image.FromFile("~/Uploads/"+dir+"/"+fName, true); //ERROR here, it gives me file URL as error message!
}
catch(Exception ex)
{
lbl_Err.Text = ex.Message;
}
Not sure what is going wrong. Any ideas?
不知道出了什么问题。有任何想法吗?
采纳答案by ????
use Server.MapPath
to fetch the image from the server.
As follows
用于Server.MapPath
从服务器获取图像。
如下
System.Drawing.Image img =
System.Drawing.Image.FromFile(Server.MapPath("Uploads/"+dir+"/"+fName), true);
You can use following as well
您也可以使用以下内容
- Server.MapPath(".") returns the current physical directory of the file (e.g. aspx) being executed
- Server.MapPath("..") returns the parent directory
- Server.MapPath("~") returns the physical path to the root of the application
- Server.MapPath("/") returns the physical path to the root of the domain name (is not necessarily the same as the root of the application)
- Server.MapPath(".") 返回正在执行的文件(例如 aspx)的当前物理目录
- Server.MapPath("..") 返回父目录
- Server.MapPath("~") 返回应用程序根目录的物理路径
- Server.MapPath("/") 返回域名根的物理路径(不一定与应用的根相同)
参考
Server.MapPath("."), Server.MapPath("~"), Server.MapPath(@"\"), Server.MapPath("/")。有什么不同?