【发布时间】: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