服务器。MapPath在使用web图像裁剪库时抛出错误
-
12-11-2019 - |
题
我有一个图像存储在服务器上的位置:
C:\inetpub\wwwroot\imageupload\images
在我的代码中,我想使用文件夹图像中的图像,所以我正在尝试如下:
Server.MapPath("/images/sample1.jpg")
但我得到的错误是:
请帮我解决错误
正如几个要求发布代码的答案,如下所示
.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的。政务司司长
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);
}
}
作物方法是从a.我从使用的dll文件 http://imagecropping.codeplex.com/
解决方案
有错误是在 CSA.Web.UI.CropImage.Crop
方法,如堆栈跟踪所述。
Server.MapPath
, ,据我所知,不会返回一个空字符串(它会抛出一个异常,如果它不能映射-但不是一个 NullReferenceException
).
我建议你看看我提到的方法,并可能发布相关的代码。
编辑:
所以,我快速浏览了一下 Crop
代码来自 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
不是null* - 论点
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,但是我看不到如何这样做。
其他提示
您已将图像存储在 imageupload\images
但是当你试图抓住它们时,你忘了添加 imageupload
目录:
Server.MapPath("/imageupload/images/sample1.jpg")
尝试相对路径:
Server.MapPath("images/sample1.jpg")
. 考虑使用源的“〜/图像/ sample1.jpg”。“〜”在呼叫中替换为server.mappath
根据: https://webcropimage.svn.codeplex.com/svn/trunk/cs.webontrols/webcropimage/cropimage.cs
方法cook(),给出到 save 裁剪图像的路径。
应该裁剪的图像存储在 wci1.imagepath 中
请检查此值是否为 null ,这应该指出问题。
由于没有世代odicetagcode,也许它无法访问ViewState:
.private string ImagePath { get { return ViewState["ImageUrl"].ToString(); } }