4

画像をトリミングして保存しています。btnsave_click で、hiddenfield の値を 10 進数に変換しています。ローカル マシンではエラーは発生しませんが、サーバーに公開すると以下のエラーが発生します。

サーバーの詳細な例外:

入力文字列は、正しい形式ではありませんでした。

説明: 現在の Web 要求の実行中に未処理の例外が発生しました。エラーの詳細とコード内のどこでエラーが発生したかについては、スタック トレースを確認してください。

例外の詳細: System.FormatException: 入力文字列が正しい形式ではありませんでした。

ソース エラー:

現在の Web 要求の実行中に未処理の例外が生成されました。例外の発生元と場所に関する情報は、以下の例外スタック トレースを使用して特定できます。

スタックトレース:

[FormatException: 入力文字列が正しい形式ではありませんでした。]
System.Number.StringToNumber(文字列 str, NumberStyles オプション, NumberBuffer& 数値, NumberFormatInfo 情報, Boolean parseDecimal) +10726387 System.Number.ParseDecimal(文字列値, NumberStyles オプション, NumberFormatInfo numfmt ) +172
System.Convert.ToDecimal(文字列値) +68
IngredientMatcher.Pages.ImageCropPopup.btnsave_Click(オブジェクト送信者、EventArgs e) +104
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +9552602
System.Web .UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +103
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) + 1724年

コード::

protected void btnsave_Click(object sender, EventArgs e)
{
    string ImageName = ViewState["ImageName"].ToString();
    int www = Convert.ToInt32(Math.Round(Convert.ToDecimal(W.Value)));
    int hhh = Convert.ToInt32(Math.Round(Convert.ToDecimal(H.Value)));
    int xxx = Convert.ToInt32(Math.Round(Convert.ToDecimal(X.Value)));
    int yyy = Convert.ToInt32(Math.Round(Convert.ToDecimal(Y.Value)));
    int w = (www == 0) ? 0 : www;
    int h = (hhh == 0) ? 0 : hhh;
    int x = (xxx == 0) ? 0 : xxx;
    int y = (yyy == 0) ? 0 : yyy;

    byte[] CropImage = Crop(Server.MapPath(" ") + "\\" + upPath + ImageName, w, h, x, y);
    using (MemoryStream ms = new MemoryStream(CropImage, 0, CropImage.Length))
    {
        ms.Write(CropImage, 0, CropImage.Length);
        using (SD.Image CroppedImage = SD.Image.FromStream(ms, true))
        {
            string SaveTo = Server.MapPath("") + "\\" + CropPath + "crop" + ImageName;

            CroppedImage.Save(SaveTo, CroppedImage.RawFormat);
            imgCropped.BorderWidth = 1;
            imgCropped.ImageUrl = CropPath + "crop" + ImageName;
        }
    }
    string CroppedImg = "crop" + ViewState["ImageName"].ToString();

    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "OpenPopUp", "javascript:SaveAndClose('" + CroppedImg + "');", true);
}

前もって感謝します。

4

3 に答える 3

4

あなたの問題は、小数の変換に使用される文化にあります。0,01 を使用するカルチャもあれば、0.01 を使用するカルチャもあります。そこに問題があります。

たとえば、インバリアント カルチャ (入力として常に 0.01) を使用できます。

int www = Convert.ToInt32(Math.Round(Convert.ToDecimal(W.Value, System.Globalization.CultureInfo.InvariantCulture)));
int hhh = Convert.ToInt32(Math.Round(Convert.ToDecimal(H.Value, System.Globalization.CultureInfo.InvariantCulture)));
int xxx = Convert.ToInt32(Math.Round(Convert.ToDecimal(X.Value, System.Globalization.CultureInfo.InvariantCulture)));
int yyy = Convert.ToInt32(Math.Round(Convert.ToDecimal(Y.Value, System.Globalization.CultureInfo.InvariantCulture)));

または、自分の文化を使用することもできます。コンストラクター文字列が自分の文化に置き換えられた場所にSystem.Globalization.CultureInfo.InvariantCulture置き換えるだけです。CultureInfo.CreateSpecificCulture("nl-NL")

于 2013-06-29T10:21:07.567 に答える
1

変換操作を実行する前に、変換された値が実質的に数値であり、空の文字列ではないことを確認する必要があります。この結果を取得するには、Decimal.TryParse メソッドを使用して、カルチャに本当に有効な 10 進数があるかどうかを確認し、値に対して変換と数学演算を実行するのが最善の方法です。

decimal dw;
decimal dh;
decimal dx;
decimal dy;

int www = 0;
int hhh = 0;
int xxx = 0;
int yyy = 0;

CultureInfo ci = CultureInfo.CreateSpecificCulture("en-GB");  // Here your specific culture

if(decimal.TryParse(W.Value, NumberStyles.AllowDecimalPoint, ci, out dw))
    www = Convert.ToInt32(Math.Round(dw));
if(decimal.TryParse(W.Value, NumberStyles.AllowDecimalPoint, ci, out dh))
    hhh = Convert.ToInt32(Math.Round(dh));
if(decimal.TryParse(W.Value, NumberStyles.AllowDecimalPoint, ci, out dx))
    xxx = Convert.ToInt32(Math.Round(dx));
if(decimal.TryParse(W.Value, NumberStyles.AllowDecimalPoint, ci, out dy))
    yyy = Convert.ToInt32(Math.Round(dy));
于 2013-06-29T10:44:12.653 に答える