【问题标题】:ASp.net WebForms C# Convert String To Int32 All ways are not workingASp.net WebForms C# Convert String To Int32 所有方法都不起作用
【发布时间】:2017-05-08 19:10:29
【问题描述】:

我正在尝试将字符串值转换为 int32,我尝试了网络上的所有解决方案以及我自己的解决方案,但没有任何效果???

我正在使用 asp.net C# 从 Cropper.js 插件中读取 x、y、宽度和高度,我的值如下:19.999999999999999

我怎么能把它转换成整数 我试过了:

 (int)Convert.ToInt32(Math.Round(Decimal.Parse(W.Value.ToString(), new CultureInfo("en-US"))));

以及所有变体 int.parse、int32.parse 和 tryParse

没有任何效果,请注意我正在将值保存在 Asp:Hidden 字段中并在服务器端读取它没有任何效果请帮助!!!

 <script>
    function CallCropper() {
        var image = document.querySelector('#image');
        setInterval(function () {
            $('#image').attr("src", $("#ImagePathHidden").val());
        }, 1);
        setTimeout(function () {
            var cropper = new Cropper(image, {
                aspectRatio: 1 / 1,
                autoCrop: true,
                crop: function (e) {
                    $("#X").val(e.detail.x.toString());
                    $("#Y").val(e.detail.y.toString());
                    $("#W").val(e.detail.width.toString());
                    $("#H").val(e.detail.height.toString());
                    //alert(e.detail.x.toString().replace(/\,/g, '.'));
                },
                movable: false,
                zoomable: false,
                rotatable: false,
                scalable: false
            });
        }, 1000);
    }
</script>

ASpCode:

 protected void btnCrop_Click(object sender, EventArgs e)
    {
    string ImageName = Session["WorkingImage"].ToString();

    decimal MyW = Decimal.Parse(W.Value.ToString(), CultureInfo.CurrentCulture);
    int Myw2 = (int)Math.Round(MyW);

    int w = Myw2;
    int h = (int)Convert.ToInt32(Math.Round(Decimal.Parse(H.Value.ToString(), CultureInfo.CurrentCulture)));
    int x = (int)Convert.ToInt32(Math.Round(Decimal.Parse(X.Value.ToString(), CultureInfo.CurrentCulture)));
    int y = (int)Convert.ToInt32(Math.Round(Decimal.Parse(Y.Value.ToString(), CultureInfo.CurrentCulture)));

    byte[] CropImage = Crop(path + 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 = path + "crop" + ImageName;
            CroppedImage.Save(SaveTo, CroppedImage.RawFormat);
            ImageSrc = "upload/crop" + ImageName;
            ImagePathHidden.Value = ImageSrc;
        }
    }
}

static byte[] Crop(string Img, int Width, int Height, int X, int Y)
{
    try
    {
        using (SD.Image OriginalImage = SD.Image.FromFile(Img))
        {
            using (SD.Bitmap bmp = new SD.Bitmap(Width, Height))
            {
                bmp.SetResolution(OriginalImage.HorizontalResolution, OriginalImage.VerticalResolution);
                using (SD.Graphics Graphic = SD.Graphics.FromImage(bmp))
                {
                    Graphic.SmoothingMode = SmoothingMode.AntiAlias;
                    Graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
                    Graphic.PixelOffsetMode = PixelOffsetMode.HighQuality;
                    Graphic.DrawImage(OriginalImage, new SD.Rectangle(0, 0, Width, Height), X, Y, Width, Height, SD.GraphicsUnit.Pixel);
                    MemoryStream ms = new MemoryStream();
                    bmp.Save(ms, OriginalImage.RawFormat);
                    return ms.GetBuffer();
                }
            }
        }
    }
    catch (Exception Ex)
    {
        throw (Ex);
    }
}

【问题讨论】:

  • 不工作是什么意思?在尝试解析之前,您是否验证了该值是否存在?
  • 是的,它给出了字符串格式错误我读了一个关于将逗号转换为点的解决方案,我使用了 regx,但也没有工作
  • 此外,我将裁剪功能从加载时更改为单击按钮时出现相同的错误,我试图提醒该值,它是有效的,但服务器端代码在运行时崩溃
  • W.Value 的数据类型是什么?你的语言环境是什么?
  • 查了文档发现隐藏字段的值是字符串

标签: javascript c# jquery asp.net webforms


【解决方案1】:

对来自标签的文本进行小测试(使用 19.999999999999999):

decimal num;
if(decimal.TryParse(lblNumberToConvert.Text, out num))
{
    lblConvertNumberResult.Text = num.ToString();
}

见:how to resolve "Input string was not in a correct format." error?

【讨论】:

    【解决方案2】:

    我想回答我问的这个问题,答案是我提到的所有方法都在工作,但它不能转换的原因是因为值是空的,所以如果有人遇到同样的问题并尝试了所有方法在堆栈溢出或互联网上提到它不起作用,原因是值为空,这就是在我的情况下发生的情况,我花了几个小时尝试在线解决方案,并且在@Steve 说要仔细追踪我发现我通过的值之后字符串为空。

    【讨论】:

      猜你喜欢
      • 2019-06-15
      • 2018-05-13
      • 2013-09-09
      • 2022-12-16
      • 2013-10-10
      • 1970-01-01
      • 1970-01-01
      • 2012-08-30
      • 1970-01-01
      相关资源
      最近更新 更多