前台:
//
圖片
var path = '<%= ResolveUrl("~/Img/") %>';
var imgUrl = path + "/" + result.FileName.substr(result.FileName.lastIndexOf('/') + 1);
var img = new Image();
img.src = imgUrl;
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);
}
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;
}
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);
string jsonStr = serializer.Serialize(model);
context.Response.Write(jsonStr);