﻿//var flag=false;
//function DrawImage(ImgD,iwidth,iheight)
//{
//   var images=new Image();
////   var iwidth = 160; //定义允许图片宽度
////   var iheight = 120; //定义允许图片高度
//   images.src=ImgD.src;
//   if(images.width>0 && images.height>0)
//   {
//        flag=true;
//        if(images.width/images.height>= iwidth/iheight)
//        {
//             if(images.width>iwidth)
//             { 
//              ImgD.width=iwidth;
//              ImgD.height=(images.height*iwidth)/images.width;
//             }
//             else
//             {
//              ImgD.width=images.width; 
//              ImgD.height=images.height;

//             }
//        }
//        else
//        {
//             if(images.height>iheight)
//             { 
//              ImgD.height=iheight;
//              ImgD.width=(images.width*iheight)/images.height; 
//            
//             }
//             else
//             {
//              ImgD.width=images.width; 
//              ImgD.height=images.height;
//            
//             }
//         }
//    }
//}

function DrawImage(objImg,maxWidth,maxHeight){ 
var img = new Image(); 
img.src = objImg.src; 
var hRatio; 
var wRatio; 
var Ratio = 1; 
var w = img.width; 
var h = img.height; 
wRatio = maxWidth / w; 
hRatio = maxHeight / h; 
if (maxWidth ==0 && maxHeight==0){ 
Ratio = 1; 
}else if (maxWidth==0){ 
if (hRatio<1) Ratio = hRatio; 
}else if (maxHeight==0){ 
if (wRatio<1) Ratio = wRatio; 
}else if (wRatio<1 || hRatio<1){ 
Ratio = (wRatio<=hRatio?wRatio:hRatio); 
} 
if (Ratio<1){ 
w = w * Ratio; 
h = h * Ratio; 
} 
objImg.height = h; 
objImg.width = w; 

if(h<=maxHeight)
{
  objImg.style.paddingTop=maxHeight-h;
}
if(w<=maxWidth)
{
objImg.style.paddingLeft=(maxWidth-w)/2;
objImg.style.paddingRight=(maxWidth-w)/2;
}
} 




