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/
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:
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:
//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.