Private Sub DataList1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) Handles DataList1.ItemDataBound End Sub 我是在DataList1.ItemDataBound中去判断来设写宽度的, 我通过 dim image as image =ctype( e.Item.FindControl("我的图片的ID"),image) 但是得到的宽度是0 我通过 if image.width > 我设定的大小 不行,这个方法
上面这段代码会有一下闪动, 所以我再改动了一下, 将图片放在一个隐藏的iframe里就不会有这种问题了.<html> <head> <meta http-equiv='Content-Type' content='text/html; charset=gb2312'> <meta name='author' content='F.R.Huang(meizz梅花雨)//www.meizz.com'> <script language="JavaScript"><!-- function getImageWidthHeight(e) { var w = e.clientWidth; var h = e.clientHeight; document.form1.txt.value = "高 : "+ h +" 宽 : "+ w; } function setImageSrcByValue(e) { var s = e.value; //file URL author: meizz if (s != "") { var filename = s.substr(s.lastIndexOf("\\")+1); var a = filename.split("."); if (a.length < 2) return; var ft = a[1].toLowerCase(); //file type if (ft=="gif" || ft=="jpg" || ft=="bmp" || ft=="png") document.frames["iframeMeizz"].document.images["imgHidden"].src = s; } } function writeIframe() { var e = document.frames["iframeMeizz"].document; e.write("<img id=imgHidden onload='parent.getImageWidthHeight(this)'>"); e.close(); } //--></script> </head><body onload="writeIframe()"> <form name=form1> <input type=file onchange="setImageSrcByValue(this)"><br> <input name=txt> </form> <iframe frameborder=0 width=0 height=0 name=iframeMeizz></iframe> </body> </html>
<form name=form1> <input type=file onchange="setImageSrcByValue(this)"><br> <input name=txt> <img border=0 id=imgHidden style="display: none" onload="getImageWidthHeight(this)"> </form><script language="JavaScript"><!-- function getImageWidthHeight(e) { var w = e.clientWidth; var h = e.clientHeight; document.form1.txt.value = "高 : "+ h +" 宽 : "+ w; e.style.display = "none"; } function setImageSrcByValue(e) { document.images["imgHidden"].src = ""; document.images["imgHidden"].style.display = ""; var s = e.value; if (s != "") { var filename = s.substr(s.lastIndexOf("\\")+1); var a = filename.split("."); if (a.length < 2) return; var filetype = a[1].toLowerCase(); if (filetype=="gif"||filetype=="jpg"||filetype=="bmp"||filetype=="png") document.images["imgHidden"].src = s; } } //--></script>
如果你非要在后台获得 Dim img As System.Drawing.Image = System.Drawing.Image.FromFile(Me.Server.MapPath("Images/photo.gif")) Dim w As Integer = img.Width Me.Response.Write(w)可以测试一下,我测试成功了
Tbmp.Width 是宽
Tbmp.Height 是高
http://dev.csdn.net/develop/article/14/14980.shtm
我是在DataList1.ItemDataBound中去判断来设写宽度的,
我通过
dim image as image =ctype( e.Item.FindControl("我的图片的ID"),image)
但是得到的宽度是0
我通过
if image.width > 我设定的大小
不行,这个方法
<head>
<meta http-equiv='Content-Type' content='text/html; charset=gb2312'>
<meta name='author' content='F.R.Huang(meizz梅花雨)//www.meizz.com'>
<script language="JavaScript"><!--
function getImageWidthHeight(e)
{
var w = e.clientWidth;
var h = e.clientHeight;
document.form1.txt.value = "高 : "+ h +" 宽 : "+ w;
}
function setImageSrcByValue(e)
{
var s = e.value; //file URL author: meizz
if (s != "")
{
var filename = s.substr(s.lastIndexOf("\\")+1);
var a = filename.split(".");
if (a.length < 2) return;
var ft = a[1].toLowerCase(); //file type
if (ft=="gif" || ft=="jpg" || ft=="bmp" || ft=="png")
document.frames["iframeMeizz"].document.images["imgHidden"].src = s;
}
}
function writeIframe()
{
var e = document.frames["iframeMeizz"].document;
e.write("<img id=imgHidden onload='parent.getImageWidthHeight(this)'>");
e.close();
}
//--></script>
</head><body onload="writeIframe()">
<form name=form1>
<input type=file onchange="setImageSrcByValue(this)"><br>
<input name=txt>
</form> <iframe frameborder=0 width=0 height=0 name=iframeMeizz></iframe>
</body>
</html>
<input type=file onchange="setImageSrcByValue(this)"><br>
<input name=txt>
<img border=0 id=imgHidden style="display: none" onload="getImageWidthHeight(this)">
</form><script language="JavaScript"><!--
function getImageWidthHeight(e)
{
var w = e.clientWidth;
var h = e.clientHeight;
document.form1.txt.value = "高 : "+ h +" 宽 : "+ w;
e.style.display = "none";
}
function setImageSrcByValue(e)
{
document.images["imgHidden"].src = "";
document.images["imgHidden"].style.display = "";
var s = e.value;
if (s != "")
{
var filename = s.substr(s.lastIndexOf("\\")+1);
var a = filename.split(".");
if (a.length < 2) return;
var filetype = a[1].toLowerCase();
if (filetype=="gif"||filetype=="jpg"||filetype=="bmp"||filetype=="png")
document.images["imgHidden"].src = s;
}
}
//--></script>
<PUBLIC:COMPONENT>
<PUBLIC:ATTACH EVENT="onpropertychange" ONEVENT="CheckImage()" />
<PUBLIC:ATTACH EVENT="oncontentready" ONEVENT="Init()"/>
<PUBLIC:ATTACH EVENT="ondetach" ONEVENT="UnInit()"/>
<PUBLIC:PROPERTY GET="GetFileValid" NAME="FileValid" />
<PUBLIC:PROPERTY GET="GetWidthValid" NAME="WidthValid" />
<PUBLIC:PROPERTY GET="GetHeightValid" NAME="HeightValid" />
<PUBLIC:PROPERTY PUT="PutFileSize" NAME="LimFileSize" />
<PUBLIC:PROPERTY PUT="PutWidth" NAME="LimWidth" />
<PUBLIC:PROPERTY PUT="PutHeight" NAME="LimHeight" />
<SCRIPT LANGUAGE="JScript">
var bFileValid=false;
var bWidthValid=false;
var bHeightValid=false;
var nFileSize=0;
var nWidth=0;
var nHeight=0;
var tempImg=null;
var tempIFrame=null;
function Init()
{
tempIFrame=element.document.createElement("IFRAME");
tempIFrame.src="about:<img>";
tempIFrame.height=0;
tempIFrame=element.document.body.appendChild(tempIFrame);
}
function UnInit()
{
}
function CheckImage()
{
tempImg=tempIFrame.contentWindow.document.images[0];
tempImg.src=element.value;
if(tempImg.readyState!="complete")
{
setTimeout(LoadImage,1)
}
else
{
bFileValid=(parseInt(tempImg.fileSize)<=nFileSize)
bFileValid=bFileValid && (parseInt(tempImg.fileSize)!=-1);
bWidthValid=(tempImg.width<=nWidth);
bHeightValid=(tempImg.height<=nHeight);
}
}
function LoadImage()
{
if(tempImg.readyState!="complete")
{
setTimeout(LoadImage,1)
}
bFileValid=(parseInt(tempImg.fileSize)<=nFileSize)
bFileValid=bFileValid && (parseInt(tempImg.fileSize)!=-1);
bWidthValid=(tempImg.width<=nWidth);
bHeightValid=(tempImg.height<=nHeight);
//alert(tempImg.width);
//alert(tempImg.height);
}
function GetFileValid()
{
return bFileValid;
}
function GetWidthValid()
{
return bWidthValid;
}
function GetHeightValid()
{
return bHeightValid;
}
function PutFileSize(vValue)
{
nFileSize=vValue;
}
function PutWidth(vValue)
{
nWidth=vValue;
}
function PutHeight(vValue)
{
nHeight=vValue;
}</SCRIPT>
</PUBLIC:COMPONENT>
测试页
<STYLE>
.Image {behavior:url(IMAGE.HTC)}
</STYLE>
<table border=1>
<tr>
<td>
<INPUT class="Image" type=file LimWidth="282" LimHeight="212" LimFileSize="1000"
ondblclick="alert(this.WidthValid);">
</td>
</tr>
<tr>
<td>
<INPUT class="Image" type=file LimWidth="282" LimHeight="212" LimFileSize="1000"
ondblclick="alert(this.WidthValid);">
</td>
</tr>
<tr>
<td>
<INPUT class="Image" type=file LimWidth="282" LimHeight="212" LimFileSize="1000"
ondblclick="alert(this.WidthValid);">
</td>
</tr>
<tr>
<td onclick="this.innerHTML='<iframe src=about:blank height=400>'">
<B>注意</B>
</td>
</tr>
<tr>
<td>
<INPUT class="Image" type=file LimWidth="282" LimHeight="212" LimFileSize="1000"
ondblclick="alert(this.WidthValid);">
</td>
</tr>
</table>
<HEAD>
<title>Upload Image</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</HEAD>
<body bgcolor="#ffffff" text="#000000">
<form name="IUpload" action="upfile.asp" method="post">
<p><input type="file" name="picsrc" size="40" onchange="picshow.src=document.IUpload.picsrc.value">
<input type="button" value="上载" onclick="detect()"></p>
</form>
<p></p>
<asp:Image ID="picshow" Runat="server" name="picshow" ></asp:Image>
<SCRIPT LANGUAGE="JavaScript">
<!--
function imgExceedSize(w,h){
if(!document.IUpload.picsrc.value==""){
if(picshow.width>w||picshow.height>h){
alert("图像尺寸:"+picshow.width+"X"+picshow.height+"。\\n图像尺寸过大!你只能上传尺寸为 "+w+"×"+h+"的图像,请重新浏览图片!");
return true;
}else{
return false;
}
}else{
return true;
}
}function detect(){
var ok=imgExceedSize(50,50);
if(ok){
document.IUpload.reset();
}else{
document.IUpload.submit();
}
}//-->
</SCRIPT>
</body>
</HTML>
使用JS控制
Dim img As System.Drawing.Image = System.Drawing.Image.FromFile(Me.Server.MapPath("Images/photo.gif"))
Dim w As Integer = img.Width
Me.Response.Write(w)可以测试一下,我测试成功了