当前位置:首页 > 使用ASP.NET上传图片并生成缩略图

使用ASP.NET上传图片并生成缩略图

点击次数:1207  更新日期:2010-12-30
\n

环境:VS.NET2005 + Windows2003


\n

就是一个简单的将图片上传到服务器,并按照需求生成缩略图。在上传图片较大的时候,可以生成一个较小的图片放在网页里,这样可以加快浏览的速度。可根据需要链接到大图片再仔细浏览。


\n


\n

不是很复杂,大概写一下。目的只在于实现,未仔细按照标准什么的来写。其中参考了网上已经存在的代码。


\n

using System.Drawing;


\n

页面,如图:


\n


\n

点击提交按钮:


\n

HttpPostedFile hpf = UploadImage.PostedFile;


\n


\n

//取得文件名(不含路径)


\n

char[] splitChar = { ‘\\\\’ };


\n

string[] FilenameArray = hpf.FileName.Split(splitChar);


\n

string Filename = FilenameArray[FilenameArray.Length - 1].ToLower();


\n


\n

if (hpf.FileName.Length < 1)


\n

{


\n

Response.Write(“请选择您要上传的图片文件“);


\n

return;


\n

}


\n

if (hpf.ContentType != “image/pjpeg” && hpf.ContentType != “image/gif”)


\n

{


\n

Response.Write(“只允许上传 GIF JPG类型的文件“);


\n

return;


\n

}


\n

else


\n

{


\n

System.Text.StringBuilder sb = new System.Text.StringBuilder();


\n

sb.Append(DateTime.Now.Year.ToString());


\n

sb.Append(DateTime.Now.Month.ToString());


\n

sb.Append(DateTime.Now.Day.ToString());


\n

sb.Append(DateTime.Now.Hour.ToString());


\n

sb.Append(DateTime.Now.Minute.ToString());


\n

sb.Append(DateTime.Now.Second.ToString());


\n

if (Filename.ToLower().EndsWith(“gif”))


\n

{


\n

sb.Append(“.gif”);


\n

}


\n

else if (Filename.ToLower().EndsWith(“jpg”))


\n

{


\n

sb.Append(“.jpg”);


\n

}


\n

else if (Filename.ToLower().EndsWith(“jpeg”))


\n

{


\n

sb.Append(“.jpeg”);


\n

}


\n

Filename = sb.ToString();


\n

}


\n


\n

// 保存图片到服务器上


\n

try


\n

{


\n

hpf.SaveAs(Server.MapPath(“..”) + “/Image/” + Filename);


\n

}


\n

catch (Exception ee)


\n

{


\n

Response.Write(“上传图片失败,原因” + ee.Message);


\n

return;


\n

}


\n


\n


\n

// 生成缩略图


\n

//原始图片名称


\n

string originalFilename = hpf.FileName;


\n

//生成的高质量图片名称


\n

string strFile = Server.MapPath(“..”) + “/Image/Small_” + Filename;


\n


\n


\n

//从文件取得图片对象


\n

System.Drawing.Image image = System.Drawing.Image.FromStream(hpf.InputStream, true);


\n


\n

Double Width = Double.Parse(TextBox1.Text.Trim());


\n

Double Height = Double.Parse(TextBox2.Text.Trim());


\n

System.Double NewWidth, NewHeight;


\n


\n

if (image.Width > image.Height)


\n

{


\n

NewWidth = Width;


\n

NewHeight = image.Height * (NewWidth / image.Width);


\n

}


\n

else


\n

{


\n

NewHeight = Height;


\n

NewWidth = (NewHeight / image.Height) * image.Width;


\n

}


\n


\n

if (NewWidth > Width)


\n

{


\n

NewWidth = Width;


\n

}


\n

if (NewHeight > Height)


\n

{


\n

NewHeight = Height;


\n

}


\n


\n

System.Drawing.Size size = new Size((int)NewWidth, (int)NewHeight); // 图片大小


\n

System.Drawing.Image bitmap = new System.Drawing.Bitmap(size.Width, size.Height); //新建bmp图片


\n

System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(bitmap); //新建画板


\n

graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //设置高质量插值法


\n

graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; //设置高质量,低速度呈现平滑程度


\n

graphics.Clear(Color.White); //清空画布


\n

//在指定位置画图


\n

graphics.DrawImage(image, new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height),


\n

new System.Drawing.Rectangle(0, 0, image.Width, image.Height),


\n

System.Drawing.GraphicsUnit.Pixel);


\n


\n

//文字水印


\n

System.Drawing.Graphics textGraphics = System.Drawing.Graphics.FromImage(bitmap);


\n

System.Drawing.Font font = new Font(“宋体“, 10);


\n

System.Drawing.Brush brush = new SolidBrush(Color.Black);


\n

textGraphics.DrawString(TextBox3.Text.Trim(), font, brush, 10, 10);


\n

textGraphics.Dispose();


\n


\n

///图片水印


\n

//System.Drawing.Image copyImage = System.Drawing.Image.FromFile(System.Web.HttpContext.Current.Server.MapPath(“pic/1.gif”));


\n

//Graphics a = Graphics.FromImage(bitmap);


\n

//a.DrawImage(copyImage, new Rectangle(bitmap.Width-copyImage.Width,bitmap.Height-copyImage.Height,copyImage.Width, copyImage.Height),0,0, copyImage.Width, copyImage.Height, GraphicsUnit.Pixel);


\n


\n

//copyImage.Dispose();


\n

//a.Dispose();


\n

//copyImage.Dispose();


\n


\n


\n

//保存缩略图


\n

try


\n

{


\n

bitmap.Save(strFile, System.Drawing.Imaging.ImageFormat.Jpeg);


\n

}


\n

catch (Exception ex)


\n

{


\n

Response.Write(“保存缩略图失败:” + ex.Message);


\n

}


\n


\n


\n

graphics.Dispose();


\n

image.Dispose();


\n

bitmap.Dispose();


\n


\n

整个实现的过程如下面的图:


\n

浏览页面,选择图片:


\n


\n

点击提交后,图片以及缩略图都已经生成到了目标文件夹里面:


\n


\n

可以看到,缩略图里面文字水印已经生成:


\n


\n


\n

至此,整个功能已实现。


\n

在生成缩略图的时候,可以根据需要对图片以及水印进行处理。更多帮助,请参阅MSDN


\n


\n

来源:http://blog.csdn.net/cqfeng

\n