使用Web图像裁剪库时Server.MapPath抛出错误
我有一个图像存储在服务器上的位置:
C:\inetpub\wwwroot\imageupload\images
在我的代码中,我想使用文件夹图像中的图像,所以我尝试如下:
Server.MapPath("/images/sample1.jpg")
但我收到错误如下:
请帮我解决错误
正如几个要求发布代码的答案,如下
< strong>.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<%@ Register Assembly="CS.Web.UI.CropImage" Namespace="CS.Web.UI" TagPrefix="cs" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<cs:CropImage ID="wci1" runat="server" />
</body>
</html>
.aspx.cs
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
wci1.Crop(Server.MapPath("/imageupload/images/sample1.jpg"));
/*
this part is just to show the image after its been cropped and saved! so focus on just the code above.
*/
Image img = new Image();
img.ImageUrl = "images/sample1.jpg?rnd=" + (new Random()).Next(); // added random for caching issue.
this.Controls.Add(img);
}
}
裁剪方法来自我使用的 http://imagecropping.codeplex.com/
I have an image stored on server at location:
C:\inetpub\wwwroot\imageupload\images
in my code i want to use an image from folder images so i am trying as follows:
Server.MapPath("/images/sample1.jpg")
But i am getting the error as:
Please help me to solve the error
As on several answer asking to post the code,it is as follows
.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<%@ Register Assembly="CS.Web.UI.CropImage" Namespace="CS.Web.UI" TagPrefix="cs" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<cs:CropImage ID="wci1" runat="server" />
</body>
</html>
.aspx.cs
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
wci1.Crop(Server.MapPath("/imageupload/images/sample1.jpg"));
/*
this part is just to show the image after its been cropped and saved! so focus on just the code above.
*/
Image img = new Image();
img.ImageUrl = "images/sample1.jpg?rnd=" + (new Random()).Next(); // added random for caching issue.
this.Controls.Add(img);
}
}
Crop method is from a .dll file i have used from http://imagecropping.codeplex.com/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如堆栈跟踪所述,
CSA.Web.UI.CropImage.Crop
方法中存在错误。据我所知,
Server.MapPath
不会返回空字符串(如果无法映射,它会抛出异常 - 但不会返回NullReferenceException
)。我建议您研究我提到的方法,并可能也发布相关代码。
编辑:
因此,我快速浏览了 您提到的 CodePlex 项目 并找到了以下几行(格式化我的):
现在,在我看来:
Path
不为空*ImageUrl
为 null***虽然它可能“不正确”,但现在不是问题。
**这意味着调用
ToString
是此代码中的破坏因素。基本上,该控件没有设置要裁剪的图像,这在您自己的代码中很明显:
修复方法是通过
ImageUrl
指定图像,AFAIK,但是我不知道该怎么做所以。There error is in the
CSA.Web.UI.CropImage.Crop
method, as described by the stack trace.Server.MapPath
, to my knowledge, will not return a null string (it'll throw an exception if it can't map - but not aNullReferenceException
).I'd suggest you look into the method I mention and possibly post the relevant code, too.
EDIT:
So, I had a quick look at the
Crop
code from the CodePlex project you mention and found the following lines (formatting mine):Now, it seems to me that:
Path
is not null*ImageUrl
is null***Though it might be 'incorrect' it isn't the problem right now.
**Meaning the call
ToString
is the breaking factor in this code.Basically, the control doesn't have an image set to crop and this is evident in your own code:
The fix is to specify an image via
ImageUrl
, AFAIK, however I can't see how to do so.您已将图像存储在
imageupload\images
中,但当您尝试抓取它们时,您忘记添加到imageupload
目录中:You have stored your images in
imageupload\images
but when you tried to grab them you forgot to add in theimageupload
directory:尝试使用相对路径:
Try a relative path instead:
考虑使用“~/images/sample1.jpg”作为您的源。在对 Server.MapPath 的调用中,“~”被替换为应用程序名称
consider using "~/images/sample1.jpg" for your source. the "~" is replace by the application name in the call to Server.MapPath
根据:
https://webcropimage.svn.codeplex.com/svn /trunk/CS.WebControls/WebCropImage/CropImage.cs
方法 Crop(),给出保存裁剪后的图像。
应该裁剪的图像存储在wci1.ImagePath中,
请检查该值是否为null,这应该会指出问题。
由于没有
According to:
https://webcropimage.svn.codeplex.com/svn/trunk/CS.WebControls/WebCropImage/CropImage.cs
Method Crop(), gives a path where to save the cropped image.
Which image should be cropped is stored in wci1.ImagePath
Please check if this value is null, and this should point out the problem.
As there is no
<Form runat="Server">
, maybe it cannot access the ViewState: