ASP.NET上传图片并生产缩略图并显示!
代码如下:usingSystem;usingSystem.Collections;usingSystem.Configuration;usingSystem.Data;u...
代码如下:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Drawing;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click1(object sender, EventArgs e)
{
string mPath;
string imagePath, imageType, imageName;
if (""!=upImage.PostedFile.FileName)
{
imagePath = upImage.PostedFile.FileName;
//取得图片类型
imageType = imagePath.Substring(imagePath.LastIndexOf(".") + 1);
//取得图片名称
imageName = imagePath.Substring(imagePath.LastIndexOf("\\") + 1);
//判断图片格式
if ("jpg" != imageType && "gif" != imageType)
{
Response.Write("<script language='javascript'>alert('格式不对');</script>");
return;
}
else
{
try
{
System.Drawing.Image image,newimage;
System.Drawing.Image.GetThumbnailImageAbort callb=null;
//建立虚拟路径
mPath = Server.MapPath("upfile");
//保存到虚拟路径
upImage.PostedFile.SaveAs(mPath + "\\" + imageName);
//显示原图
Imagesource.ImageUrl = "upfile/" + imageName;
//为了上传的图片建立引用
image=System.Drawing.Image.FromFile(mPath + "\\" + imageName);
//生成缩略图
newimage = System.Drawing.Image.GetThumbnailImage(100, 100, callb,new System.IntPtr());
//把缩略图保存到指定的虚拟目录
newimage.Save(Server.MapPath("upfile") + "\\small" + imageName);
//释放
image.Dispose();
//释放
newimage.Dispose();
//显示缩略图
Imagesmall.ImageUrl = "upfile/" + "small" + imageName;
Response.Write("<script language='javascript'>alert('上传成功');</script>");
}
catch
{
Response.Write("上传失败");
}
}
}
}
}
运行起来报错,不能生成缩略图,求高手修改 展开
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Drawing;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click1(object sender, EventArgs e)
{
string mPath;
string imagePath, imageType, imageName;
if (""!=upImage.PostedFile.FileName)
{
imagePath = upImage.PostedFile.FileName;
//取得图片类型
imageType = imagePath.Substring(imagePath.LastIndexOf(".") + 1);
//取得图片名称
imageName = imagePath.Substring(imagePath.LastIndexOf("\\") + 1);
//判断图片格式
if ("jpg" != imageType && "gif" != imageType)
{
Response.Write("<script language='javascript'>alert('格式不对');</script>");
return;
}
else
{
try
{
System.Drawing.Image image,newimage;
System.Drawing.Image.GetThumbnailImageAbort callb=null;
//建立虚拟路径
mPath = Server.MapPath("upfile");
//保存到虚拟路径
upImage.PostedFile.SaveAs(mPath + "\\" + imageName);
//显示原图
Imagesource.ImageUrl = "upfile/" + imageName;
//为了上传的图片建立引用
image=System.Drawing.Image.FromFile(mPath + "\\" + imageName);
//生成缩略图
newimage = System.Drawing.Image.GetThumbnailImage(100, 100, callb,new System.IntPtr());
//把缩略图保存到指定的虚拟目录
newimage.Save(Server.MapPath("upfile") + "\\small" + imageName);
//释放
image.Dispose();
//释放
newimage.Dispose();
//显示缩略图
Imagesmall.ImageUrl = "upfile/" + "small" + imageName;
Response.Write("<script language='javascript'>alert('上传成功');</script>");
}
catch
{
Response.Write("上传失败");
}
}
}
}
}
运行起来报错,不能生成缩略图,求高手修改 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询