.net 中如何得到图片的高度(height)和宽度(width) ,生成图片的缩略图
2008-03-27 17:31
519 查看
.net 开发中,需要得到图片的高和宽。代码如下所示:
protected string imgPath;
protected string fileExtName;
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("请选择图片!");
}
}
生成图片的缩略图的方法
//定义image类的对象
System.Drawing.Image image,newimage;
//图片路径
protected string imagePath;
//图片类型
protected string imageType;
//图片名称
protected string imageName;
//提供一个回调方法,用于确定Image对象在执行生成缩略图操作时何时提前取消执行
//如果此方法确定 GetThumbnailImage 方法应提前停止执行,则返回 true;否则返回 false
System.Drawing.Image.GetThumbnailImageAbort callb = null;
private void btnUp_Click(object sender, System.EventArgs e)
...{
string mPath;
if("" != upImage.PostedFile.FileName)
...{
imagePath= upImage.PostedFile.FileName;
//取得图片类型
imageType= imagePath.Substring(imagePath.LastIndexOf(".")+1);
//取得图片名称
imageName = imagePath.Substring(imagePath.LastIndexOf("/")+1);
//判断是否是JPG或者GIF图片,这里只是举个例子,并不一定必须是这两种图片
if("jpg" != imageType && "gif" != imageType)
...{
Response.Write("<script language='javascript'> alert('对不起!请您选择jpg或者gif格式的图片!');</script>");
return;
}
else
...{
try
...{
//建立虚拟路径
mPath=Server.MapPath("upFile");
//保存到虚拟路径
upImage.PostedFile.SaveAs(mPath+"/"+imageName);
//显示原图
imageSource.ImageUrl = "upFile/"+imageName;
//为上传的图片建立引用
image=System.Drawing.Image.FromFile(mPath+"/"+imageName);
//生成缩略图
newimage=image.GetThumbnailImage(100,100,callb,new System.IntPtr());
//把缩略图保存到指定的虚拟路径
newimage.Save(Server.MapPath("upFile")+"/small"+imageName);
//释放image对象占用的资源
image.Dispose();
//释放newimage对象的资源
newimage.Dispose();
//显示缩略图
imageSmall.ImageUrl = "upFile/"+"small"+imageName;
Response.Write("上传成功!");
}
catch
...{
Response.Write("上传成功!");
}
}
}
protected string imgPath;
protected string fileExtName;
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("请选择图片!");
}
}
生成图片的缩略图的方法
//定义image类的对象
System.Drawing.Image image,newimage;
//图片路径
protected string imagePath;
//图片类型
protected string imageType;
//图片名称
protected string imageName;
//提供一个回调方法,用于确定Image对象在执行生成缩略图操作时何时提前取消执行
//如果此方法确定 GetThumbnailImage 方法应提前停止执行,则返回 true;否则返回 false
System.Drawing.Image.GetThumbnailImageAbort callb = null;
private void btnUp_Click(object sender, System.EventArgs e)
...{
string mPath;
if("" != upImage.PostedFile.FileName)
...{
imagePath= upImage.PostedFile.FileName;
//取得图片类型
imageType= imagePath.Substring(imagePath.LastIndexOf(".")+1);
//取得图片名称
imageName = imagePath.Substring(imagePath.LastIndexOf("/")+1);
//判断是否是JPG或者GIF图片,这里只是举个例子,并不一定必须是这两种图片
if("jpg" != imageType && "gif" != imageType)
...{
Response.Write("<script language='javascript'> alert('对不起!请您选择jpg或者gif格式的图片!');</script>");
return;
}
else
...{
try
...{
//建立虚拟路径
mPath=Server.MapPath("upFile");
//保存到虚拟路径
upImage.PostedFile.SaveAs(mPath+"/"+imageName);
//显示原图
imageSource.ImageUrl = "upFile/"+imageName;
//为上传的图片建立引用
image=System.Drawing.Image.FromFile(mPath+"/"+imageName);
//生成缩略图
newimage=image.GetThumbnailImage(100,100,callb,new System.IntPtr());
//把缩略图保存到指定的虚拟路径
newimage.Save(Server.MapPath("upFile")+"/small"+imageName);
//释放image对象占用的资源
image.Dispose();
//释放newimage对象的资源
newimage.Dispose();
//显示缩略图
imageSmall.ImageUrl = "upFile/"+"small"+imageName;
Response.Write("上传成功!");
}
catch
...{
Response.Write("上传成功!");
}
}
}
相关文章推荐
- asp.net 中如何得到图片的高度(height)和宽度(width)以及生成图片的缩略图 C#
- asp.net 中如何得到图片的高度(height)和宽度(width)以及生成图片的缩略图 C#
- [js]如何更快的得到图片的高度和宽度
- 如何用ASP.NET生成图片的缩略图
- asp.net上传图片并同时生成缩略图
- asp.net 获取图片的宽度和高度 (两方法)
- asp.net生成缩略图、文字图片水印
- 如何通过js和jquery获取图片真实的宽度和高度
- textarea文本域宽度和高度(width、height)自己主动适应变化处理
- 关于asp.net上传图片自动生成缩略图
- ASP.NET C# 按原图片大小等比例缩放生成缩略图
- HTML表格标记教程(31):单元格的宽度和高度属性WIDTH、HEIGHT
- [网络收集]使用ASP.net(C#)批量上传图片并自动生成缩略图,文字水印图,图片水印图
- NPOI 1.2.3教程 -7 单元格高度和宽度HeightAndWidth
- 使用CSS修改TD或TR的高度(height)或宽度(width)
- 如何在页面加载完毕后获取图片高度或宽度问题
- 在ASP.NET中实现上传图片,并按比例生成缩略图
- 用ASP.NET上传图片并生成带版权信息的缩略图(转载)
- ASP.Net 上传图片并生成高清晰缩略图
- VB长度宽度高度单位,width,scaleWidth,Height,scaleHeight,scaleMode