環(huán)境:VS.NET2005 + Windows2003
就是一個(gè)簡(jiǎn)單的將圖片上傳到服務(wù)器,并按照需求生成縮略圖。在上傳圖片較大的時(shí)候,可以生成一個(gè)較小的圖片放在網(wǎng)頁(yè)里,這樣可以加快瀏覽的速度。可根據(jù)需要鏈接到大圖片再仔細(xì)瀏覽。
不是很復(fù)雜,大概寫一下。目的只在于實(shí)現(xiàn),未仔細(xì)按照標(biāo)準(zhǔn)什么的來(lái)寫。其中參考了網(wǎng)上已經(jīng)存在的代碼。
using System.Drawing;
頁(yè)面,如圖:
點(diǎn)擊提交按鈕:
HttpPostedFile hpf = UploadImage.PostedFile;
//取得文件名(不含路徑)
char[] splitChar = { '\\' };
string[] FilenameArray = hpf.FileName.Split(splitChar);
string Filename = FilenameArray[FilenameArray.Length - 1].ToLower();
if (hpf.FileName.Length < 1)
{
Response.Write("請(qǐng)選擇您要上傳的圖片文件");
return;
}
if (hpf.ContentType != "image/pjpeg" && hpf.ContentType != "image/gif")
{
Response.Write("只允許上傳 GIF JPG類型的文件");
return;
}
else
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append(DateTime.Now.Year.ToString());
sb.Append(DateTime.Now.Month.ToString());
sb.Append(DateTime.Now.Day.ToString());
sb.Append(DateTime.Now.Hour.ToString());
sb.Append(DateTime.Now.Minute.ToString());
sb.Append(DateTime.Now.Second.ToString());
if (Filename.ToLower().EndsWith("gif"))
{
sb.Append(".gif");
}
else if (Filename.ToLower().EndsWith("jpg"))
{
sb.Append(".jpg");
}
else if (Filename.ToLower().EndsWith("jpeg"))
{
sb.Append(".jpeg");
}
Filename = sb.ToString();
}
// 保存圖片到服務(wù)器上
try
{
hpf.SaveAs(Server.MapPath("..") + "/Image/" + Filename);
}
catch (Exception ee)
{
Response.Write("上傳圖片失敗,原因" + ee.Message);
return;
}
// 生成縮略圖
//原始圖片名稱
string originalFilename = hpf.FileName;
//生成的高質(zhì)量圖片名稱
string strFile = Server.MapPath("..") + "/Image/Small_" + Filename;
//從文件取得圖片對(duì)象
System.Drawing.Image image = System.Drawing.Image.FromStream(hpf.InputStream, true);
Double Width = Double.Parse(TextBox1.Text.Trim());
Double Height = Double.Parse(TextBox2.Text.Trim());
System.Double NewWidth, NewHeight;
if (image.Width > image.Height)
{
NewWidth = Width;
NewHeight = image.Height * (NewWidth / image.Width);
}
else
{
NewHeight = Height;
NewWidth = (NewHeight / image.Height) * image.Width;
}
if (NewWidth > Width)
{
NewWidth = Width;
}
if (NewHeight > Height)
{
NewHeight = Height;
}
System.Drawing.Size size = new Size((int)NewWidth, (int)NewHeight); // 圖片大小
System.Drawing.Image bitmap = new System.Drawing.Bitmap(size.Width, size.Height); //新建bmp圖片
System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(bitmap); //新建畫板
graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //設(shè)置高質(zhì)量插值法
graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; //設(shè)置高質(zhì)量,低速度呈現(xiàn)平滑程度
graphics.Clear(Color.White); //清空畫布
//在指定位置畫圖
graphics.DrawImage(image, new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height),
new System.Drawing.Rectangle(0, 0, image.Width, image.Height),
System.Drawing.GraphicsUnit.Pixel);
//文字水印
System.Drawing.Graphics textGraphics = System.Drawing.Graphics.FromImage(bitmap);
System.Drawing.Font font = new Font("宋體", 10);
System.Drawing.Brush brush = new SolidBrush(Color.Black);
textGraphics.DrawString(TextBox3.Text.Trim(), font, brush, 10, 10);
textGraphics.Dispose();
///圖片水印
//System.Drawing.Image copyImage = System.Drawing.Image.FromFile(System.Web.HttpContext.Current.Server.MapPath("pic/1.gif"));
//Graphics a = Graphics.FromImage(bitmap);
//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);
//copyImage.Dispose();
//a.Dispose();
//copyImage.Dispose();
//保存縮略圖
try
{
bitmap.Save(strFile, System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch (Exception ex)
{
Response.Write("保存縮略圖失敗:" + ex.Message);
}
graphics.Dispose();
image.Dispose();
bitmap.Dispose();
整個(gè)實(shí)現(xiàn)的過(guò)程如下面的圖:
瀏覽頁(yè)面,選擇圖片:
點(diǎn)擊提交后,圖片以及縮略圖都已經(jīng)生成到了目標(biāo)文件夾里面:
可以看到,縮略圖里面文字水印已經(jīng)生成:
至此,整個(gè)功能已實(shí)現(xiàn)。
在生成縮略圖的時(shí)候,可以根據(jù)需要對(duì)圖片以及水印進(jìn)行處理。更多幫助,請(qǐng)參閱MSDN。