我这半个月的代码总结(小技巧,小代码)之一
实现功能:
一:检查用户输入的网站连接代码(如果以非http://开头则自动加上http头)
二:无论用户输入的路径如何都只显示主站点的URL(比如http://blog.sina.com.cn/myblog/u/3554345.htm 只显示http://blog.sina.com.cn/)
public string checkHttp(String http) //检查HTTP
{
if (!(http == "原创") && !(http == "投票") && !(http == "")) //如果非---为原创,投票,或者空
{
if (http.IndexOf("http://") > -1) //判断是否包括HTTP头
{
if (http.Substring(7, http.Length - 7).IndexOf("/") > -1) //包括头情况下删除URL住站后的“/”
{
http = http.Substring(0, http.Substring(7).IndexOf("/") + 8);
}
}
else
{
if (http.IndexOf("/") > -1) //没HTTP头
{
http = (http.Substring(0, http.IndexOf("/") + 1)).Insert(0, "http://");
}
}
}
else
{
return http;
}
return http;
}
二:图片伸缩方法
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script language="javascript">
function chang_img(img,maxwidth,maxheight){
var image=new Image();
image=img;
if(image.width/image.height>=maxwidth/maxheight)
{
if(image.width>maxwidth)
{
img.width=maxwidth;
img.height=maxwidth*image.height/maxheight;
}
else
{
img.width=image.width;
img.height=image.height;
}
}
else
{
if(image.height>maxheight)
{
img.width=maxheight*image.width/image.height;
img.height=image.height;
}
else
{
img.width=image.width;
img.height=image.height;
}
}
}</script>
</head>
<body>
<img src="1.jpg" onload="chang_img(this,50,50)" />
</body>
</html>
解决问题:正常我们在不确定图片大小的情况下,要求最后确定大小格式图片输出的时候要采取伸缩图的形式,
如果硬性规定图片显示大小的话会造成两种错误效果:一 图片被伸缩后 严重变形失真,二:图片只看到了一部分小区域
上面的代码经过计算 长和高 进行判断伸缩比例和结构
img变量为被引用的图片“对象”,可以通过this传递给本变量
maxwidth 希望伸缩后的实际图片显示宽度
maxheight 希望伸缩后的实际图片显示高度
哈哈 绝对不失真

浙公网安备 33010602011771号