繁体   English   中英

ASp.net WebForms C#将字符串转换为Int32所有方式均不起作用

[英]ASp.net WebForms C# Convert String To Int32 All ways are not working

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

我正在使用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);
    }
}

使用标签中的文本进行一点测试(使用19.999999999999999):

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

请参阅: 如何解决“输入字符串的格式不正确”。 错误?

我想回答这个问题,我想回答的是我提到的所有方法都在起作用,但之所以不进行转换,是因为该值为null,所以如果有人遇到相同的问题并尝试了栈中提到的所有方法溢出或互联网,它将无法正常工作,原因是该值为null,这就是我在花了数小时尝试在线解决方案的情况下发生的情况,在@Steve说要仔细跟踪值之后,我发现我将null传递给了串。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM