サーバー。Webイメージクロップライブラリを使用すると、MapPathがエラーをスローします

StackOverflow https://stackoverflow.com/questions/5977200

質問

私は場所にサーバーに保存されている画像を持っています:

C:\inetpub\wwwroot\imageupload\images

私のコードでは、folder imagesから画像を使用したいので、次のようにしています:

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

しかし、私はエラーを取得しています:enter image description here

エラーを解決するために私を助けてください

コードを投稿するように求めているいくつかの答えのように、それは次のとおりです

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

クロップ法はaからです。私が使用したdllファイル http://imagecropping.codeplex.com/

役に立ちましたか?

解決

にエラーがあります... CSA.Web.UI.CropImage.Crop スタックトレースで説明されているように、メソッド。

Server.MapPath, 、私の知る限り、null文字列を返しません(マップできない場合は例外がスローされますが、マップできない場合は例外がスローされます 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 ヌルヌル**

*それは"間違っている"かもしれませんが、今は問題ではありません。

**コールの意味 ToString このコードの破壊要因です。

基本的に、コントロールには画像がcropに設定されておらず、これは独自のコードで明らかです:

//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に画像を保存しましたが、Grabをつくらせようとした場合は、imageuploadディレクトリに追加しました。

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

代わりに相対パスを試してください:

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

ソースの "〜/ images / sample1.jpg"を使うことを検討してください。"〜"はserver.mappath への呼び出しでアプリケーション名によって置き換えられます。

に従って: https://webcropimage.svn.codeplex.com/svn./trunk/cs.webcontrols/webcropimage/cropimage.cs

method crop()は、 save トリートメント画像を保存するパスを与えます。

どの画像をトリミングする必要があるかは WCI1.ImagePath に保存されます。

この値が null のかどうかを確認してください。これは問題を指摘する必要があります。

<Form runat="Server">がないため、ViewStateにアクセスできない:

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

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top