今天做的一个小模块中,跟图片有关:
首先:生成缩略图,再上传到服务器.
以前做了一个图片上传的,但是他不是按比例缩放的.只需要在中间加一段判断长和宽.就可以了.还是看看代码吧.
//
定义image类的对象
System.Drawing.Image image;
System.Drawing.Image newimage;
// 图片路径
protected string imagePath;
// 图片类型
protected string imageType;
// 图片名称
protected string imageName;
// 提供一个回调方法,用于确定Image对象在执行生成缩略图操作时提前取消执行
// 如果此方法确定GetThumbnailImage方法应提前停止执行,则返回true;否则返回false
System.Drawing.Image.GetThumbnailImageAbort callb = null ;
protected void Button1_Click( object sender, EventArgs e)
... {
string mPath;
//如果文件名称不为空
if (FileUpload1.PostedFile.FileName != "")
...{
imagePath = FileUpload1.PostedFile.FileName;
//取得图片类型
imageType = imagePath.Substring(imagePath.LastIndexOf(".") + 1).ToLower().ToString();
//取得图片名称
imageName = imagePath.Substring(imagePath.LastIndexOf("/") + 1);
//判断是否是jpg/gif/jpeg/jpe类型图片
if (imageType != "jpg" && imageType != "gif" && imageType != "jpeg" && imageType != "jpe")
...{
ConfirmMessageBox("对不起,请选择:jpg|jpeg|jpe|gif中的任意一种格式!");
}
else
...{
//判断图片是否大于120K[120 * 1024]
if (FileUpload1.PostedFile.ContentLength > 122880)
...{
Label2.Text = "对不起,文件不得大于120K(122880个字节)!";
}
else
...{
try
...{
//建立虚拟路径
mPath = Server.MapPath("productImages");
//保存图片到虚拟路径
imageName = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + "." + imageType;
HiddenField1.Value = imageName;
FileUpload1.PostedFile.SaveAs(mPath + "/" + imageName);
//显示原图
//Image1.ImageUrl = "productImages/" + imageName;
//为上传图片建立引用
image = System.Drawing.Image.FromFile(mPath + "/" + imageName);
//==============================================================
//加入判断长宽的比例代码 >> Begin
//==============================================================
//生成原图
Byte[] oFileByte = new Byte[FileUpload1.PostedFile.ContentLength];
Stream oStream = FileUpload1.PostedFile.InputStream;
System.Drawing.Image oImage = System.Drawing.Image.FromStream(oStream);
//原图宽度和高度
int oWidth = oImage.Width;
int oHeight = oImage.Height;
//设置缩略图的初始宽度和高度
int tWidth = 100;
int tHeight = 100;
//按比例计算出缩略图的宽度和高度
if (oWidth >= oHeight)
...{
tWidth = (int)Math.Floor(Convert.ToDouble(oHeight) * (Convert.ToDouble(tWidth) / Convert.ToDouble(oWidth)));
}
else
...{
tHeight = (int)Math.Floor(Convert.ToDouble(oWidth) * (Convert.ToDouble(tHeight) / Convert.ToDouble(oHeight)));
}
//==============================================================
//判断长宽的比例代码 >> Over
//==============================================================
/**////生成缩略图
newimage = image.GetThumbnailImage(tHeight, tWidth, callb, System.IntPtr.Zero);
//把缩略图保存到指定的虚拟路径
newimage.Save(Server.MapPath("productImages") + "/small" + imageName);
//释放image对象占用的资源
image.Dispose();
//释放newimage占用的资源
newimage.Dispose();
//显示缩略图
Image1.ImageUrl = "productImages/small" + imageName;
Label2.Text = "上传成功!";
}
catch
...{
Label2.Text = "上传失败!";
}
}
}
}
else
...{
Label2.Text = "不能为空,请选择图片!";
}
}
System.Drawing.Image image;
System.Drawing.Image newimage;
// 图片路径
protected string imagePath;
// 图片类型
protected string imageType;
// 图片名称
protected string imageName;
// 提供一个回调方法,用于确定Image对象在执行生成缩略图操作时提前取消执行
// 如果此方法确定GetThumbnailImage方法应提前停止执行,则返回true;否则返回false
System.Drawing.Image.GetThumbnailImageAbort callb = null ;
protected void Button1_Click( object sender, EventArgs e)
... {
string mPath;
//如果文件名称不为空
if (FileUpload1.PostedFile.FileName != "")
...{
imagePath = FileUpload1.PostedFile.FileName;
//取得图片类型
imageType = imagePath.Substring(imagePath.LastIndexOf(".") + 1).ToLower().ToString();
//取得图片名称
imageName = imagePath.Substring(imagePath.LastIndexOf("/") + 1);
//判断是否是jpg/gif/jpeg/jpe类型图片
if (imageType != "jpg" && imageType != "gif" && imageType != "jpeg" && imageType != "jpe")
...{
ConfirmMessageBox("对不起,请选择:jpg|jpeg|jpe|gif中的任意一种格式!");
}
else
...{
//判断图片是否大于120K[120 * 1024]
if (FileUpload1.PostedFile.ContentLength > 122880)
...{
Label2.Text = "对不起,文件不得大于120K(122880个字节)!";
}
else
...{
try
...{
//建立虚拟路径
mPath = Server.MapPath("productImages");
//保存图片到虚拟路径
imageName = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + "." + imageType;
HiddenField1.Value = imageName;
FileUpload1.PostedFile.SaveAs(mPath + "/" + imageName);
//显示原图
//Image1.ImageUrl = "productImages/" + imageName;
//为上传图片建立引用
image = System.Drawing.Image.FromFile(mPath + "/" + imageName);
//==============================================================
//加入判断长宽的比例代码 >> Begin
//==============================================================
//生成原图
Byte[] oFileByte = new Byte[FileUpload1.PostedFile.ContentLength];
Stream oStream = FileUpload1.PostedFile.InputStream;
System.Drawing.Image oImage = System.Drawing.Image.FromStream(oStream);
//原图宽度和高度
int oWidth = oImage.Width;
int oHeight = oImage.Height;
//设置缩略图的初始宽度和高度
int tWidth = 100;
int tHeight = 100;
//按比例计算出缩略图的宽度和高度
if (oWidth >= oHeight)
...{
tWidth = (int)Math.Floor(Convert.ToDouble(oHeight) * (Convert.ToDouble(tWidth) / Convert.ToDouble(oWidth)));
}
else
...{
tHeight = (int)Math.Floor(Convert.ToDouble(oWidth) * (Convert.ToDouble(tHeight) / Convert.ToDouble(oHeight)));
}
//==============================================================
//判断长宽的比例代码 >> Over
//==============================================================
/**////生成缩略图
newimage = image.GetThumbnailImage(tHeight, tWidth, callb, System.IntPtr.Zero);
//把缩略图保存到指定的虚拟路径
newimage.Save(Server.MapPath("productImages") + "/small" + imageName);
//释放image对象占用的资源
image.Dispose();
//释放newimage占用的资源
newimage.Dispose();
//显示缩略图
Image1.ImageUrl = "productImages/small" + imageName;
Label2.Text = "上传成功!";
}
catch
...{
Label2.Text = "上传失败!";
}
}
}
}
else
...{
Label2.Text = "不能为空,请选择图片!";
}
}
那么显示的时候长宽最大为100.
如果在一个页面需要显示长宽最大为150,怎么办呢?刚刚开始我想从服务器端得到图片的大小.但是他需要的是绝对中路径.
用那种方法只能得到客户端的图片大小.代码如下:
private
void
btnGet_Click(
object
sender, System.EventArgs e)
... {
if(upImage.PostedFile.FileName != "")
...{
imgPath =upImage.PostedFile.FileName;
fileExtName = imgPath.Substring(imgPath.LastIndexOf(".")+1,3);
if(fileExtName !="gif" && fileExtName != "jpg")
...{
Response.Write("请选择GIF和JPG格式的图片");
}
else
...{
//获取图片大小
System.Drawing.Image image = System.Drawing.Image.FromFile(imgPath);
txtHeight.Text = image.Height.ToString();
txtWidth.Text = image.Width.ToString();
}
}
else
...{
Response.Write("请选择图片!");
}
}
... {
if(upImage.PostedFile.FileName != "")
...{
imgPath =upImage.PostedFile.FileName;
fileExtName = imgPath.Substring(imgPath.LastIndexOf(".")+1,3);
if(fileExtName !="gif" && fileExtName != "jpg")
...{
Response.Write("请选择GIF和JPG格式的图片");
}
else
...{
//获取图片大小
System.Drawing.Image image = System.Drawing.Image.FromFile(imgPath);
txtHeight.Text = image.Height.ToString();
txtWidth.Text = image.Width.ToString();
}
}
else
...{
Response.Write("请选择图片!");
}
}
没有办法,只好在显示到客户端时用onLoad事件来完成对图片大小的改造了.
此方法参考Winner.Net@2007的图片超过规定的大小就按原图片大小缩小
1,不按照比例的缩放
<
script language
=
"
javascript
"
>
function changeImg(mypic) ... {
alert("OK");
var xw=130;
var xl=160;
var width = mypic.width;
var height = mypic.height;
if (width > xw ) mypic.width = xw;
if (height > xl ) mypic.height = xl;
}
</ script >
< img src = " sh180.jpg " onload = " changeImg(this) " >
function changeImg(mypic) ... {
alert("OK");
var xw=130;
var xl=160;
var width = mypic.width;
var height = mypic.height;
if (width > xw ) mypic.width = xw;
if (height > xl ) mypic.height = xl;
}
</ script >
< img src = " sh180.jpg " onload = " changeImg(this) " >
2,按照比例的缩放
<
script language
=
"
javascript
"
>
function changeImg1(mypic) ... {
alert("OK");
var xw=160;
var xl=180;
var width = mypic.width;
var height = mypic.height;
var bili = width/height;
var A=xw/width;
var B=xl/height;
if(A>1||B>1)
...{
if(A<B)
...{
mypic.width=xw;
mypic.height=xw/bili;
}
if(A>B)
...{
mypic.width=xl*bili;
mypic.height=xl;
}
}
}
</ script >
function changeImg1(mypic) ... {
alert("OK");
var xw=160;
var xl=180;
var width = mypic.width;
var height = mypic.height;
var bili = width/height;
var A=xw/width;
var B=xl/height;
if(A>1||B>1)
...{
if(A<B)
...{
mypic.width=xw;
mypic.height=xw/bili;
}
if(A>B)
...{
mypic.width=xl*bili;
mypic.height=xl;
}
}
}
</ script >
哪位高手如果知道怎么得到服务器端图片的大小,请不吝赐教.