我在jsp页面上要显示一图片的缩略图,我用Javascript来控制,时而可以,时而不可以,请问,这是怎么回事,我用的Javascript如下:
function SetImgAutoSize(img)
{
var MaxWidth=120;//设置图片宽度界限
var MaxHeight=60;//设置图片高度界限
var HeightWidth=img.offsetHeight/img.offsetWidth;//设置高宽比
var WidthHeight=img.offsetWidth/img.offsetHeight;//设置宽高比 if(img.readyState!="complete"){
return false;//确保图片完全加载
}
//CheckImg(img);
if(img.offsetWidth>MaxWidth){
img.width=MaxWidth;
img.height=MaxWidth*HeightWidth;
}
if(img.offsetHeight>MaxHeight){
img.height=MaxHeight;
img.width=MaxHeight*WidthHeight;
}
}
在图片显示处用onload事件即onload="SetImgAutoSize(this);".
不知各位高手有没有好办法?谢谢!
function SetImgAutoSize(img)
{
var MaxWidth=120;//设置图片宽度界限
var MaxHeight=60;//设置图片高度界限
var HeightWidth=img.offsetHeight/img.offsetWidth;//设置高宽比
var WidthHeight=img.offsetWidth/img.offsetHeight;//设置宽高比 if(img.readyState!="complete"){
return false;//确保图片完全加载
}
//CheckImg(img);
if(img.offsetWidth>MaxWidth){
img.width=MaxWidth;
img.height=MaxWidth*HeightWidth;
}
if(img.offsetHeight>MaxHeight){
img.height=MaxHeight;
img.width=MaxHeight*WidthHeight;
}
}
在图片显示处用onload事件即onload="SetImgAutoSize(this);".
不知各位高手有没有好办法?谢谢!
File _file = new File(SpecialVector.PICTOREPATH+"/"+tempPicUrl); //读入文件
Image src = javax.imageio.ImageIO.read(_file); //构造Image对象
int wideth=src.getWidth(null); //得到源图宽
int height=src.getHeight(null); //得到源图长
BufferedImage tag = new BufferedImage((320*wideth/height),320,BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(src,0,0,(320*wideth/height),320,null); //绘制缩小后的图
FileOutputStream outFile=new FileOutputStream(SpecialVector.PICTOREPATH+"/"+picUrl); //输出到文件流
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(outFile);
encoder.encode(tag); //近JPEG编码
outFile.close();
var flag=false;
function DrawImage(ImgD){
var image=new Image();
var iwidth=100; //定义允许图片宽度
var iheight=100; //定义允许图片高度
image.src=ImgD.src;
if(image.width>0 && image.height>0){
flag=true;
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;
}
}
}
</script>
<img src="*.jpg" onload=DrawImage(this) border=0>这段js代码我用过,可以成功的。这个代码我也是从这个论坛找到的,在此感谢原作者
能读网络上的图片吗?