使用Web图像裁剪库时Server.MapPath抛出错误

发布于 2024-11-07 10:39:24 字数 1746 浏览 0 评论 0原文

我有一个图像存储在服务器上的位置:

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:
enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

烈酒灼喉 2024-11-14 10:39:24

如堆栈跟踪所述,CSA.Web.UI.CropImage.Crop 方法中存在错误。

据我所知,Server.MapPath 不会返回空字符串(如果无法映射,它会抛出异常 - 但不会返回 NullReferenceException)。

我建议您研究我提到的方法,并可能也发布相关代码。

编辑:

因此,我快速浏览了 您提到的 CodePlex 项目 并找到了以下几行(格式化我的):

private string ImagePath { get { return ViewState["ImageUrl"].ToString(); } }

drawing.Image image = drawing.Image.FromStream(
    new MemoryStream(
        System.IO.File.ReadAllBytes(
            HttpContext.Current.Server.MapPath(this.ImagePath)
        )
    )
);

cropedImage.Save(path);

现在,在我看来:

  • 参数 Path 不为空*
  • 参数 ImageUrl null**

*虽然它可能“不正确”,但现在不是问题。

**这意味着调用 ToString 是此代码中的破坏因素。

基本上,该控件没有设置要裁​​剪的图像,这在您自己的代码中很明显:

//currently no ImageUrl set...
<cs:CropImage ID="wci1" runat="server" />

//your'e trying to crop here without an ImageUrl...
wci1.Crop(Server.MapPath("/imageupload/images/sample1.jpg"));   

修复方法是通过 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 a NullReferenceException).

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):

private string ImagePath { get { return ViewState["ImageUrl"].ToString(); } }

drawing.Image image = drawing.Image.FromStream(
    new MemoryStream(
        System.IO.File.ReadAllBytes(
            HttpContext.Current.Server.MapPath(this.ImagePath)
        )
    )
);

cropedImage.Save(path);

Now, it seems to me that:

  • Argument Path is not null*
  • Argument 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:

//currently no ImageUrl set...
<cs:CropImage ID="wci1" runat="server" />

//your'e trying to crop here without an ImageUrl...
wci1.Crop(Server.MapPath("/imageupload/images/sample1.jpg"));   

The fix is to specify an image via ImageUrl, AFAIK, however I can't see how to do so.

香橙ぽ 2024-11-14 10:39:24

您已将图像存储在 imageupload\images 中,但当您尝试抓取它们时,您忘记添加到 imageupload 目录中:

Server.MapPath("/imageupload/images/sample1.jpg")

You have stored your images in imageupload\images but when you tried to grab them you forgot to add in the imageupload directory:

Server.MapPath("/imageupload/images/sample1.jpg")
你好,陌生人 2024-11-14 10:39:24

尝试使用相对路径:

Server.MapPath("images/sample1.jpg")

Try a relative path instead:

Server.MapPath("images/sample1.jpg")
墨离汐 2024-11-14 10:39:24

考虑使用“~/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

徒留西风 2024-11-14 10:39:24

根据:
https://webcropimage.svn.codeplex.com/svn /trunk/CS.WebControls/WebCropImage/CropImage.cs

方法 Crop(),给出保存裁剪后的图像。

应该裁剪的图像存储在wci1.ImagePath中,

请检查该值是否为null,这应该会指出问题。

由于没有

,可能无法访问 ViewState:

private string ImagePath { get { return ViewState["ImageUrl"].ToString(); } }

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:

private string ImagePath { get { return ViewState["ImageUrl"].ToString(); } }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文