DataList显示图片变形要怎么解决,如何缩放DataList里面的图片
解决方案 »
- 求助!新手,想找个简单的例子理解asp.net的三层架构,要c#的,vb不懂!
- htm页中嵌套动态页面,如何刷新该动态页面(有分页)?急!!!
- 使用asp.net ajax后,是不是不能用自定义的HttpModule?
- 怎样控制柱状图在网页上的显示位置?
- DropDownList 不能绑定数据怎么办啊!!
- asp.net页面的后台代码是归在业务逻辑层还是UI层
- crystal report 中怎樣顯示圖片字段 ﹖﹖
- 自定义控件怎么样才能像标准自定义控件那样,在WEB FORM上随便摆放?
- 请问各位你们是如何编写ASP.net的网页
- 有点难度!asp:checkbox在客户端控制<asp:textbox>是否可操作,怎么做到?
- 急求一sql语句!来者有分
- 关于gridview的问题
通过JS来实现等比缩放,你可以参考
希望可以帮到楼主
function resizeimg(ImgD,iwidth,iheight) {
var image=new Image();
image.src=ImgD.src;
if(image.width>0 && image.height>0){
if(image.width/image.height>= iwidth/iheight){
if(image.width>iwidth){
ImgD.width=iwidth;
ImgD.height=(image.height*iwidth)/image.width;
}else{
ImgD.width=image.width;
ImgD.height=image.height;
}
ImgD.alt=image.width+"×"+image.height;
}
else{
if(image.height>iheight){
ImgD.height=iheight;
ImgD.width=(image.width*iheight)/image.height;
}else{
ImgD.width=image.width;
ImgD.height=image.height;
}
ImgD.alt=image.width+"×"+image.height;
}
ImgD.style.cursor= "pointer"; //改变鼠标指针
ImgD.onclick = function() { window.open(this.src);} //点击打开大图片
if (navigator.userAgent.toLowerCase().indexOf("ie") > -1) { //判断浏览器,如果是IE
ImgD.title = "请使用鼠标滚轮缩放图片,点击图片可在新窗口打开";
ImgD.onmousewheel = function img_zoom() //滚轮缩放
{
var zoom = parseInt(this.style.zoom, 10) || 100;
zoom += event.wheelDelta / 12;
if (zoom> 0) this.style.zoom = zoom + "%";
return false;
}
} else { //如果不是IE
ImgD.title = "点击图片可在新窗口打开";
}
}
}
<img name="" src="" onload="javascript:resizeimg(this,100,200)">
<table border="0" cellpadding="0" cellspacing="0" style="position: relative" width="230">
<tr>
<td align="center" height="188" nowrap="" style="width: 230px" valign="middle">
<table border="0" cellpadding="0" cellspacing="0" width="168">
<tr>
<td align="center" height="188" nowrap="" style="background-image: url(images/photo_0.jpg)"
valign="middle">
<table border="0" cellpadding="0" cellspacing="0" height="149" width="140">
<tr>
<td style="height: 149px">
<asp:ImageButton ID="ImageButton1" runat="server" Height="142px" ImageUrl='<%#DataBinder.Eval(Container.DataItem,"TypeUrl") %>' PostBackUrl='<%#"~/photos/meb_m2.aspx?TypeID="+DataBinder.Eval(Container.DataItem,"TypeID") %>'
Style="left: 4px; position: relative" Width="102px" /> <br />
<%#DataBinder.Eval(Container.DataItem,"TypeName") %>
(<%#DataBinder.Eval(Container.DataItem, "TypeCount")%>)张</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td height="12" nowrap="" style="width: 230px">
</td>
</tr>
<tr>
<td align="center" nowrap="" style="width: 230px; height: 24px">
<asp:ImageButton ID="imgbtnUpdate" runat="server" ImageUrl="~/photos/images/ph_re_r1_c1.gif"
PostBackUrl='<%#"~/photos/news_ph_ch.aspx?TypeID="+DataBinder.Eval(Container.DataItem,"TypeID") %>'
Style="position: relative" />
<asp:ImageButton ID="imgbtnDel" runat="server" ImageUrl="~/photos/images/ph_re_r1_c4.gif"
PostBackUrl='<%#"~/photos/del.aspx?TypeID="+DataBinder.Eval(Container.DataItem,"TypeID") %>'
Style="position: relative" /></td>
</tr>
<tr>
<td align="center" style="width: 230px; height: 24px" valign="middle">
日期: <%#DataBinder.Eval(Container.DataItem, "TypeDate")%></td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
Style="left: 4px; position: relative" Width="102px" />
------------------------
修改为<a href='<%#"~/photos/meb_m2.aspx?TypeID="+DataBinder.Eval(Container.DataItem,"TypeID")%>'><img src=' <%#DataBinder.Eval(Container.DataItem,"TypeUrl")%>' alt="图片" onload="javascript:resizeimg(this,100,200)" ></img></a>
把我的JS函数放在<head></head>之间
string pic = context.Request["pic"];
int W = 100;
int H = 100;
try
{
W = int.Parse(context.Request["W"]);
H = int.Parse(context.Request["H"]);
}
catch
{
context.Response.End();
}
if(pic==null || pic=="" || W<=0 || H<=0 || pic.IndexOf(".")<1)
{
context.Response.End();
}
Bitmap bit = new Bitmap(System.Configuration.ConfigurationManager.AppSettings["UpLoadFile"] + pic.Replace("/upload/", "").Replace("/images/",""));
MemoryStream ms = new MemoryStream();
int newh = 0;
int neww = 0;
//
if (bit.Width < W && bit.Height < H)
{
newh = bit.Height;
neww = bit.Width;
}
else
{
double pwidth = (double)bit.Width / W;
double pheight = (double)bit.Height / H;
if (pwidth > pheight)
{
neww = W;
newh = Convert.ToInt32(bit.Height / pwidth);
}
else
{
newh = H;
neww = Convert.ToInt32(bit.Width / pheight);
}
}
//生成小图
Bitmap newbit = new Bitmap(bit, neww, newh);
newbit.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
context.Response.ContentType = "image/jpeg";
context.Response.StatusCode = 200;
context.Response.BinaryWrite(ms.ToArray());
这个是什么意思?
创建一个新bit
System.Configuration.ConfigurationManager.AppSettings["UpLoadFile"] + pic.Replace("/upload/", "").Replace("/images/",""));
图片路径
System.Configuration.ConfigurationManager.AppSettings["UpLoadFile"]这个在webconfig设置的路径key
<appSettings>
<add key="WebSite" value="http://192.168.1.207:8011" />
<add key="UpLoadFile" value="E:\dingfang8ver2\WebSite\upload\" />
</appSettings>