Server.MapPath throws error when using Web Image Cropping Library

I have an image stored on the server at:

C:\inetpub\wwwroot\imageupload\images

in my code I want to use an image from folder images, so I'm trying to do the following:

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

But I get an error like: enter image description here

Please help me resolve this error.

As with multiple code submission responses, it looks like this

.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);
    }
}

The cropping method from the .dll file that I used from http://imagecropping.codeplex.com/

+3
source share
5 answers

Error in the method CSA.Web.UI.CropImage.Cropas described in the stack trace.

Server.MapPath, , ( , , NullReferenceException).

, , , , .

EDIT:

, 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 *
  • 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, , .

+4

imageupload\images, , imageupload:

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

:

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

"~/images/sample1.jpg" . "~" Server.MapPath

0

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

Crop(), , .

, wci1.ImagePath

, , .

<Form runat="Server">, , ViewState:

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

All Articles