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);
}
}
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?
我想回答这个问题,我想回答的是我提到的所有方法都在起作用,但之所以不进行转换,是因为该值为null,所以如果有人遇到相同的问题并尝试了栈中提到的所有方法溢出或互联网,它将无法正常工作,原因是该值为null,这就是我在花了数小时尝试在线解决方案的情况下发生的情况,在@Steve说要仔细跟踪值之后,我发现我将null传递给了串。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.