﻿/*按比例生成缩略图*/
function DrawImage1(MyPic,W,H){
  var flag=false;
  var image=new Image();
  image.src=MyPic.src;
  if(image.width>0 && image.height>0){
    flag=true;
    if(image.width/image.height>= W/H){
      if(image.width>W){  
        MyPic.width=W;
        MyPic.height=(image.height*W)/image.width;
      }
	  else{
        MyPic.width=image.width;  
        MyPic.height=image.height;
      }
    }
    else{
      if(image.height>H){  
        MyPic.height=H;
        MyPic.width=(image.width*H)/image.height;     
      }
	  else{
        MyPic.width=image.width;  
        MyPic.height=image.height;
      }
    }
  }
} 

function DrawImage(imgObj,_width,_height)
{
	//alert(imgObj.width)
	if (imgObj == null) return;
	try{	
		//imgObj.onclick = function (){window.open(imgObj.src,"","")}
		imgObj.style.cursor = "pointer";
		imgObj.style.display = "inline";
		
		var thumbnailSize = new ImgSize();
		thumbnailSize.width = _width;
		thumbnailSize.height = _height;
	
		var sourceSize = new ImgSize();
		sourceSize.width = imgObj.width
		sourceSize.height = imgObj.height;
		
		var lastSize = new ImgSize();
		lastSize = GetLastImageSize(sourceSize,thumbnailSize)
	
		imgObj.width = lastSize.width;
		imgObj.height = lastSize.height;
		imgObj.title = ""
	}catch(e){}
}


function ImgSize()
{
	this.width = 0;
	this.height = 0;
}

/// <summary>
/// 设定最终输出图像的宽和高
/// </summary>
/// <param name='sourceSize'>原图像尺寸</param>
/// <param name='thumbnailSize'>目标图像尺寸</param>
function GetLastImageSize(sourceSize,thumbnailSize)
{
	
		var size = new ImgSize();
		//原图宽和高都比目标图的宽和高小
		if (sourceSize.width <= thumbnailSize.width && sourceSize.height <= thumbnailSize.height)
		{
		
			size.width = sourceSize.width;
			size.height = sourceSize.height;
			return;
		}

		//原图宽度大于目标宽度
		if (sourceSize.width > thumbnailSize.width)
		{
			size.width = thumbnailSize.width;
			size.height = thumbnailSize.width*sourceSize.height/sourceSize.width;
		}

		//原图高度大于目标高度
		if (sourceSize.height > thumbnailSize.height)
		{
			size.height = thumbnailSize.height;
			size.width = thumbnailSize.height*sourceSize.width/sourceSize.height;
		}

		var tmpWidth = size.width;
		var tmpHeight = size.height;

		//最终图的宽大于目标图的宽
		if (size.width > thumbnailSize.width)
		{
			size.width = thumbnailSize.width;
			size.height = thumbnailSize.width*tmpHeight/tmpWidth;
		}

		//最终图的高大于目标图的高
		if (size.height > thumbnailSize.height)
		{
			size.height = thumbnailSize.height;
			size.width = thumbnailSize.height*tmpWidth/tmpHeight;
		}
				/*//*/
	return size;
}
