gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 14:59:04 24 4
gpt4 key购买 nike

我正在裁剪图像并保存。在 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com