using System;
using System.Web;
using System.Web.UI;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
namespace {
/// <summary>
/// UploadFile 的摘要說明。
/// </summary>
public class UploadFile
{
public UploadFile()
{
//
// TODO: 在此處添加建構函式邏輯
//
}
/// <summary>
/// 指定路徑 圖片大小
/// </summary>
/// <param name="dir"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <returns></returns>
public string GetSqlFile(string dir, int width, int height)
{
string sqlfile="";
string path = HttpContext.Current.Request.PhysicalApplicationPath.ToString()+"upload/";
path +=dir;
if(Directory.Exists(path))
{
}
else
{
Directory.CreateDirectory(path);
}
if(Directory.Exists(path+"/small"))
{
}
else
{
Directory.CreateDirectory(path+"/small");
}
HttpFileCollection files = HttpContext.Current.Request.Files;
if(files[0].FileName.ToString().Length>0)
{
string filename = files[0].FileName.ToString();
string datestr = DateTime.Now.ToString("yyyyMMddHmmssfff");
string ext = filename.Substring(filename.LastIndexOf("."));
if(ext!=".bmp"&&ext!=".jpg"&&ext!=".gif"&&ext!=".jpeg")
{
HttpContext.Current.Response.Write("<script>alert('上傳的檔案不是.gif,jpg,jpeg,bmp格式')</script>");
return "";
}
files[0].SaveAs(path+"/"+datestr+ext);
#region 產生小圖
string originalFilename = path+"/"+datestr+ext;
//縮小的倍數
int iScale = 1;
//從檔案取得圖片對象
Image image = null;
try
{
image = Image.FromFile(originalFilename);
}
catch
{
//
try
{
File.Delete(originalFilename);
image.Dispose();
}
catch
{
}
HttpContext.Current.Response.Write("<script>alert('上傳的檔案不是.gif,jpg,jpeg,bmp圖片的標準格式格式')</script>");
return "";
}
int hi =0;
int wi =0;
wi=width;
hi=height;
Size size = new Size(wi,hi);
//建立一個bmp圖片
Image bitmap = new Bitmap(size.Width, size.Height);
//建立一個畫板
Graphics g = Graphics.FromImage(bitmap);
//設定高品質插值法
g.InterpolationMode = InterpolationMode.High;
//設定高品質,低速度呈現平滑程度
g.SmoothingMode = SmoothingMode.HighQuality;
//清空一下畫布
g.Clear(Color.Blue);
//在指定位置畫圖
g.DrawImage(image, new Rectangle(0, 0, bitmap.Width, bitmap.Height), new Rectangle(0, 0, image.Width, image.Height), GraphicsUnit.Pixel);
if (ext == ".jpg" || ext == ".jpeg")
bitmap.Save(path+"/small/"+datestr+ext, ImageFormat.Jpeg);
if (ext == ".gif")
bitmap.Save(path+"/small/"+datestr+ext, ImageFormat.Gif);
if(ext==".bmp")
bitmap.Save(path+"/small/"+datestr+ext,ImageFormat.Bmp);
image.Dispose();
bitmap.Dispose();
g.Dispose();
#endregion
sqlfile=datestr+ext;
try
{
image.Dispose();
bitmap.Dispose();
g.Dispose();
}
catch(Exception ex)
{
string exc = ex.Message.ToString();
HttpContext.Current.Response.Write("<script>alert('"+exc+"');</script>");
}
}
else
{
sqlfile = "";
}
return sqlfile;
}
}
}