.net上传图片生成大小缩略图
2009-04-01 18:02
417 查看
public string GetSqlFile(FileUpload fp, int width, int height)
{
string sqlfile = "";
string tempSqlPath = "";
string path = HttpContext.Current.Request.PhysicalApplicationPath.ToString() + "upload/";
path += "UploadFile/" + DateTime.Now.ToString("yyyyMM");
tempSqlPath = "\\upload\\UploadFile\\" + DateTime.Now.ToString("yyyyMM");
if (Directory.Exists(path))
{
}
else
{
Directory.CreateDirectory(path);
}
if (Directory.Exists(path + "/small"))
{
}
else
{
Directory.CreateDirectory(path + "/small");
}
if (Directory.Exists(path + "/temp"))
{
}
else
{
Directory.CreateDirectory(path + "/temp");
}
//HttpFileCollection files = HttpContext.Current.Request.Files;
if (fp.FileName.ToString().Length > 0)
{
string filename = fp.FileName.ToString();
string datestr = DateTime.Now.ToString("yyyyMMddHmmssfff");
string ext = filename.Substring(filename.LastIndexOf(".")).ToLower();
if (ext != ".bmp" && ext != ".jpg" && ext != ".gif" && ext != ".jpeg")
{
HttpContext.Current.Response.Write("<script>alert('上传的文件不是.gif,jpg,jpeg,bmp格式')</script>");
return "";
}
string tempFilePath = path + "/" + "temp/" + datestr + ext;
fp.SaveAs(tempFilePath);
#region 生成小图
string originalFilename = path + "/" + datestr + ext;
//fp.FileBytes
//缩小的倍数
int iScale = 1;
//从文件取得图片对象
System.Drawing.Image image = null;
try
{
image = System.Drawing.Image.FromFile(tempFilePath);
}
catch
{
//
try
{
File.Delete(tempFilePath);
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图片
System.Drawing.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);
///大图片
if (fp.FileBytes.Length > 300000){
//image.Width
// image.Height
wi = 800;
hi = Convert.ToInt32(image.Height * (Convert.ToDouble(wi) / Convert.ToDouble(image.Width)));
size = new Size(wi, hi);
//新建一个bmp图片
bitmap = new Bitmap(size.Width, size.Height);
//新建一个画板
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(originalFilename, ImageFormat.Jpeg);
if (ext == ".gif")
bitmap.Save(originalFilename, ImageFormat.Gif);
if (ext == ".bmp")
bitmap.Save(originalFilename, ImageFormat.Bmp);
}
else {
fp.SaveAs(originalFilename);
}
image.Dispose();
bitmap.Dispose();
g.Dispose();
#endregion
sqlfile = datestr + ext;
try
{
image.Dispose();
bitmap.Dispose();
g.Dispose();
File.Delete(tempFilePath);
}
catch (Exception ex)
{
string exc = ex.Message.ToString();
HttpContext.Current.Response.Write("<script>alert('" + exc + "');</script>");
}
}
else
{
sqlfile = "";
}
sqlfile = sqlfile.Length>0?(tempSqlPath + "\\" + sqlfile):("");
return sqlfile;
}
{
string sqlfile = "";
string tempSqlPath = "";
string path = HttpContext.Current.Request.PhysicalApplicationPath.ToString() + "upload/";
path += "UploadFile/" + DateTime.Now.ToString("yyyyMM");
tempSqlPath = "\\upload\\UploadFile\\" + DateTime.Now.ToString("yyyyMM");
if (Directory.Exists(path))
{
}
else
{
Directory.CreateDirectory(path);
}
if (Directory.Exists(path + "/small"))
{
}
else
{
Directory.CreateDirectory(path + "/small");
}
if (Directory.Exists(path + "/temp"))
{
}
else
{
Directory.CreateDirectory(path + "/temp");
}
//HttpFileCollection files = HttpContext.Current.Request.Files;
if (fp.FileName.ToString().Length > 0)
{
string filename = fp.FileName.ToString();
string datestr = DateTime.Now.ToString("yyyyMMddHmmssfff");
string ext = filename.Substring(filename.LastIndexOf(".")).ToLower();
if (ext != ".bmp" && ext != ".jpg" && ext != ".gif" && ext != ".jpeg")
{
HttpContext.Current.Response.Write("<script>alert('上传的文件不是.gif,jpg,jpeg,bmp格式')</script>");
return "";
}
string tempFilePath = path + "/" + "temp/" + datestr + ext;
fp.SaveAs(tempFilePath);
#region 生成小图
string originalFilename = path + "/" + datestr + ext;
//fp.FileBytes
//缩小的倍数
int iScale = 1;
//从文件取得图片对象
System.Drawing.Image image = null;
try
{
image = System.Drawing.Image.FromFile(tempFilePath);
}
catch
{
//
try
{
File.Delete(tempFilePath);
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图片
System.Drawing.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);
///大图片
if (fp.FileBytes.Length > 300000){
//image.Width
// image.Height
wi = 800;
hi = Convert.ToInt32(image.Height * (Convert.ToDouble(wi) / Convert.ToDouble(image.Width)));
size = new Size(wi, hi);
//新建一个bmp图片
bitmap = new Bitmap(size.Width, size.Height);
//新建一个画板
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(originalFilename, ImageFormat.Jpeg);
if (ext == ".gif")
bitmap.Save(originalFilename, ImageFormat.Gif);
if (ext == ".bmp")
bitmap.Save(originalFilename, ImageFormat.Bmp);
}
else {
fp.SaveAs(originalFilename);
}
image.Dispose();
bitmap.Dispose();
g.Dispose();
#endregion
sqlfile = datestr + ext;
try
{
image.Dispose();
bitmap.Dispose();
g.Dispose();
File.Delete(tempFilePath);
}
catch (Exception ex)
{
string exc = ex.Message.ToString();
HttpContext.Current.Response.Write("<script>alert('" + exc + "');</script>");
}
}
else
{
sqlfile = "";
}
sqlfile = sqlfile.Length>0?(tempSqlPath + "\\" + sqlfile):("");
return sqlfile;
}
相关文章推荐
- .net上传图片生成大小缩略图
- .net上传图片生成大小缩略图
- .net上传图片文件,生成缩略图,并写上版权信息
- .net图片操作类,生成缩略图,上传图片截取,上传图片打水印
- php实现按指定大小等比缩放生成上传图片缩略图的方法
- php按指定大小等比缩放生成上传图片缩略图
- php实现按指定大小等比缩放生成上传图片缩略图的方法
- .net上传图片文件,生成缩略图,并写上版权信息
- .Net 上传图片并生成高清晰缩略图
- 对给定的一个图片生成一个指定大小的缩略图(上传图片很大是可用)
- 上传图片 生成指定大小的缩略图
- php实现按指定大小等比缩放生成上传图片缩略图的方法
- net上传图片生成大小缩略图
- 上传图片,并生成缩略图(指定大小)
- ASP.NET 2.0上传图片生成缩略图类
- 生成规定大小的图片(缩略图生成)
- 在ASP.NET中上传图片并生成缩略图的C#源码
- asp.net图片上传生成缩略图的注意事项
- 用ASP.NET上传图片并生成可带版权信息的缩略图
- 图片上传自动生成缩略图VB组件!