0

I'm trying to convert a string value to int32 and I tried all the solutions on the web also my own solutions and nothing is working???

I'm reading x,y,width and height from Cropper.js Plugin using asp.net C# and I have a value like this:19.999999999999999

how on earth could I convert it to integer I tried :

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

and all variations also int.parse, int32.parse and tryParse

nothing is working, please note that i'm saving the values in Asp:Hidden field and read it on server side nothing is working please help!!!!

 <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);
    }
}

2 Answers2

0

a little test with text from a label (using 19.999999999999999):

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

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

Community
  • 1
  • 1
wazz
  • 4,953
  • 5
  • 20
  • 34
0

I want to answer this question which I asked the answer is all the ways is working that I mention but the reason why would it not convert is because the value was null, so if anyone face the same problem and tried all the ways mentioned on stack overflow or the internet and it wont work the reason is the value is null, this is what happened in my case I spent hours and hours on trying online solutions and after @Steve said to trace carefully the values I found that I pass null to the string.