C# 输入字符串的格式不正确。服务器上的转换问题不在本地机器上

标签 c# asp.net jcrop

我正在裁剪图像并保存。在 btnsave_click 上,我将 hiddenfield 值转换为十进制。在本地机器上没有错误,但是当发布到服务器时出现以下错误。

服务器上的详细异常:

输入的字符串格式不正确。

描述:当前网络请求执行过程中出现未处理的异常。请查看堆栈跟踪以获取有关错误及其在代码中的来源的更多信息。

异常详细信息:System.FormatException:输入字符串的格式不正确。

来源错误:

在执行当前 Web 请求期间生成了未处理的异常。可以使用下面的异常堆栈跟踪来识别有关异常起源和位置的信息。

堆栈跟踪:

[FormatException: Input string was not in a correct format.]
System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal) +10726387 System.Number.ParseDecimal(String value, NumberStyles options, NumberFormatInfo numfmt) +172
System.Convert.ToDecimal(String value) +68
IngredientMatcher.Pages.ImageCropPopup.btnsave_Click(Object sender, 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);
}

提前致谢。

最佳答案

您的问题在于用于转换小数的区域性。有些文化使用 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"),其中构造函数字符串被替换与您的文化。

关于C# 输入字符串的格式不正确。服务器上的转换问题不在本地机器上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17379083/

相关文章:

javascript - 如何使用jquery或javascript获取gridview中的DataKey值?请在回答之前先阅读代码

javascript - 从 .css 移动到 jQuery css(),在动态添加元素时不起作用

c# - 从字符串数组中删除重复字母的字符串

c# - 在 .NET MVC 应用程序中验证 Azure AD B2C JWT

asp.net - 如何在 OWIN asp.net Web API 中启动时获取根 url

javascript - 裁剪时图像居中问题

jquery - jcrop 的回调未被调用

c# - 命名管道 : how server side can know that client has disconnected?

c# - 我从 C# 调用带有 2 个参数(输入和输出)的存储过程,但输出参数的值不显示

c# - 如何通过逗号拆分字符串填充 gridview?