您的位置:首页 > 其它

Ajax异步无刷新显示图片

2012-03-11 12:50 936 查看
前台:

//图片
var path = '<%= ResolveUrl("~/Img/") %>';
var imgUrl = path + "/" + result.FileName.substr(result.FileName.lastIndexOf('/') + 1);
var img = new Image();
img.src = imgUrl;

if (!img.complete) {

//无图片
}
else //有图片
{
if (img.width < 500) { //小于最大宽度,按实际宽度显示
$("#<%=Image1.ClientID %>").css("width", img.width);
}
else { //大于等于最大宽度,按最大宽度显示
$("#<%=Image1.ClientID %>").css("width", 500);
}
}
后台初始化获取第一张图片:

System.Drawing.Image imgPhoto = System.Drawing.Image.FromFile(Server.MapPath(url));
int sourceWidth = imgPhoto.Width;
int sourceHeight = imgPhoto.Height;

if (sourceWidth < 500)
{
Unit width = new Unit(sourceWidth);
this.Image1.Width = width;
}
if (sourceHeight < 350)
{
Unit height = new Unit(sourceHeight);
this.Image1.Height = height;
}
ashx文件获取切换的图片数据(使用json返回):

JavaScriptSerializer serializer = new JavaScriptSerializer();
string jsonStr = serializer.Serialize(model);

context.Response.Write(jsonStr);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐