Server.MapPath arroja un error al utilizar la biblioteca de recorte de imágenes web
-
12-11-2019 - |
Pregunta
Tengo una imagen almacenada en el servidor en la ubicación:
C:\inetpub\wwwroot\imageupload\images
en mi código quiero usar una imagen de la carpeta de imágenes, así que estoy intentando lo siguiente:
Server.MapPath("/images/sample1.jpg")
Pero recibo el error como:
Por favor ayúdenme a resolver el error.
Como en varias respuestas que solicitan publicar el código, es el siguiente
.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);
}
}
El método de recorte es de un archivo .dll que he usado desde http://imagecropping.codeplex.com/
Solución
El error está en el método CSA.Web.UI.CropImage.Crop
, como se describe en la traza de la pila.
Server.MapPath
, a mi leal saber
Le sugiero que investigue el método que también menciono y posiblemente publique el código relevante.
Editar:
Entonces, tuve una mirada rápida al código de NullReferenceException
de el proyecto CodePlex menciona y encontró las siguientes líneas (formateo de la mina):
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);
Otros consejos
Has almacenado tus imágenes en imageupload\images
pero cuando intentaste tomarlos, olvidaste agregarlos en el imageupload
directorio:
Server.MapPath("/imageupload/images/sample1.jpg")
Pruebe una ruta relativa en su lugar:
Server.MapPath("images/sample1.jpg")
Considere el uso de "~ / imágenes / sample1.jpg" para su fuente.El "~" se reemplaza con el nombre de la aplicación en la llamada a servidor.mappath
De acuerdo a:https://webcropimage.svn.codeplex.com/svn/trunk/CS.WebControls/WebCropImage/CropImage.cs
Método Crop(), proporciona una ruta donde ahorrar la imagen recortada.
La imagen que se debe recortar se almacena en wci1.ImagePath
Por favor verifique si este valor es nulo, y esto debería señalar el problema.
Como no hay <Form runat="Server">
, tal vez no pueda acceder a ViewState:
private string ImagePath { get { return ViewState["ImageUrl"].ToString(); } }