关于图片上传及管理问题,最好有注释,急,知道放假了,但等用.
200分急求
200分急求
解决方案 »
- WPF DataGrid 的数据源问题
- 一个条码的问题,请问各位有没有试过?
- C#中的数据库连接问题求解
- 问一下C#如何才能实现邻接多重表
- 问下接口IComparer
- c# treeview中treenode能用ctrl和shift复选
- C#里如何实现哈希表里的数据的输出(在线等,及时结帖)
- 如何通过 BackGroundWorker(或是自己写多线程代码)实现在备份数据库时在进度条里显示备份进度(如果代码写起来麻烦指点一下方法就行了)谢
- 请教一个关于BitBlt函数的问题
- C#中怎样Label的那个属性使它变为透明?
- 兄弟们,如何用PropertyGrid编辑INI文件中的配置信息,我的INI文件中的配置项是动态的。
- 急!!即时消息通讯协议
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Drawing.Imaging;namespace eMeng.Exam
{
/// <summary>
/// Thumbnail 的摘要说明。
/// </summary>
public class Thumbnail : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Button Button1; private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
Label1.Text = "<h3>在ASP.NET里轻松实现缩略图</h3>";
Button1.Text = "上载并显示缩略图"; } #region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion private void Button1_Click(object sender, System.EventArgs e)
{
HttpFileCollection MyFileColl = HttpContext.Current.Request.Files;
HttpPostedFile MyPostedFile = MyFileColl[0]; if (MyPostedFile.ContentType.ToString().ToLower().IndexOf("image") < 0)
{
Response.Write("无效的图形格式。");
return;
}
GetThumbNail(MyPostedFile.FileName, 100, 100,
MyPostedFile.ContentType.ToString(), false, MyPostedFile.InputStream);
}
private System.Drawing.Imaging.ImageFormat GetImageType(object strContentType)
{
if ((strContentType.ToString().ToLower()) == "image/pjpeg")
{
return System.Drawing.Imaging.ImageFormat.Jpeg;
}
else if ((strContentType.ToString().ToLower()) == "image/gif")
{
return System.Drawing.Imaging.ImageFormat.Gif;
}
else if ((strContentType.ToString().ToLower()) == "image/bmp")
{
return System.Drawing.Imaging.ImageFormat.Bmp;
}
else if ((strContentType.ToString().ToLower()) == "image/tiff")
{
return System.Drawing.Imaging.ImageFormat.Tiff;
}
else if ((strContentType.ToString().ToLower()) == "image/x-icon")
{
return System.Drawing.Imaging.ImageFormat.Icon;
}
else if ((strContentType.ToString().ToLower()) == "image/x-png")
{
return System.Drawing.Imaging.ImageFormat.Png;
}
else if ((strContentType.ToString().ToLower()) == "image/x-emf")
{
return System.Drawing.Imaging.ImageFormat.Emf;
}
else if ((strContentType.ToString().ToLower()) == "image/x-exif")
{
return System.Drawing.Imaging.ImageFormat.Exif;
}
else if ((strContentType.ToString().ToLower()) == "image/x-wmf")
{
return System.Drawing.Imaging.ImageFormat.Wmf;
}
else
{
return System.Drawing.Imaging.ImageFormat.MemoryBmp;
}
} private void GetThumbNail(string strFileName, int iWidth, int iheight,
string strContentType, bool blnGetFromFile, System.IO.Stream ImgStream)
{
System.Drawing.Image oImg;
if (blnGetFromFile)
{
oImg = System.Drawing.Image.FromFile(strFileName);
}
else
{
oImg = System.Drawing.Image.FromStream(ImgStream);
}
oImg = oImg.GetThumbnailImage(iWidth, iheight, null, IntPtr.Zero);
string strGuid = System.Guid.NewGuid().ToString().ToUpper();
string strFileExt = strFileName.Substring(strFileName.LastIndexOf("."));
Response.ContentType = strContentType;
MemoryStream MemStream = new MemoryStream();
oImg.Save(MemStream, GetImageType(strContentType));
MemStream.WriteTo(Response.OutputStream);
} }
}
</FONT></FONT></FONT>
<FONT size=4>功能:
1。把图片文件(JPG GIF PNG)上传,
2。保存到指定的路径(在web.config中设置路径,以文件的原有格式保存),
3。并自动生成指定宽度的(在web.config中设置宽度)
4。和指定格式的(在web.config中指定缩略图的格式)
5。和原图比例相同的缩略图(根据宽度和原图的宽和高计算所略图的高度)
6。可以判断是否已经存在文件
7。如果不覆盖,则给出错误
8。如果选中"覆盖原图"checkbox,则覆盖原图。
9。可以根据要求,在webform上设置1个以上的file input和相应的checkbox
10。并在文件上传完毕后,显示原图的文件名,尺寸,字节,和
11。缩略图的文件名尺寸。
12。缩略图的文件名格式:原图+"_thumb."+指定格式,如:test.jpg_thumb.gif,以便于管理。--------------------
public void UploadFile(object sender, System.EventArgs e)
{string imgNameOnly, imgNameNoExt, imgExt;
string imgThumbnail;
int erroNumber = 0;
System.Drawing.Image oriImg, newImg;
string strFePicSavePath = ConfigurationSettings.AppSettings["FePicSavePath"].ToString();
string strFePicThumbFormat = ConfigurationSettings.AppSettings["FePicThumbFormat"].ToString().ToLower();
int intFeThumbWidth = Int32.Parse(ConfigurationSettings.AppSettings["FePicThumbWidth"]);
string fileExt;StringBuilder picInfo = new StringBuilder();if(Page.IsValid)
{
for(int i = 0;i < Request.Files.Count; i++)
{
HttpPostedFile PostedFile = Request.Files[i];
fileExt = (System.IO.Path.GetExtension(PostedFile.FileName)).ToString().ToLower();imgNameOnly = System.IO.Path.GetFileName(PostedFile.FileName);
if(fileExt == ".jpg" || fileExt == ".gif" || fileExt == ".png")
{if(System.IO.File.Exists(strFePicSavePath + imgNameOnly) && (checkboxlistRewrite.Items[i].Selected == false))
{
erroNumber = erroNumber + 1;
picInfo.Append("<b>错误:</b>文件("+ (i+1) +") " + imgNameOnly + " 已经存在,请修改文件名<br/>" );
}
}
else
{
erroNumber = erroNumber + 1;
picInfo.Append("<b>错误:</b>文件("+ (i+1) +") " + imgNameOnly + " 扩展名 " + fileExt + " 不被许可<br/>" );
}}if(erroNumber > 0)
{
picInfo.Append("<font color=red>全部操作均未完成,请修改错误,再进行操作</font><br/>");
}
else
{
for(int i = 0;i < Request.Files.Count; i++)
{HttpPostedFile PostedFile = Request.Files[i];
imgNameOnly = System.IO.Path.GetFileName(PostedFile.FileName);
imgNameNoExt = System.IO.Path.GetFileNameWithoutExtension(PostedFile.FileName);
imgExt = System.IO.Path.GetExtension(PostedFile.FileName).ToString().ToLower();
oriImg = System.Drawing.Image.FromStream(PostedFile.InputStream);
newImg = oriImg.GetThumbnailImage(intFeThumbWidth, intFeThumbWidth * oriImg.Height/oriImg.Width,null,new System.IntPtr(0));
switch(imgExt)
{
//case ".jpeg":
case ".jpg":
oriImg.Save(strFePicSavePath + imgNameOnly , System.Drawing.Imaging.ImageFormat.Jpeg);
break;
case ".gif":
oriImg.Save(strFePicSavePath + imgNameOnly , System.Drawing.Imaging.ImageFormat.Gif);
break;
case ".png":
oriImg.Save(strFePicSavePath + imgNameOnly , System.Drawing.Imaging.ImageFormat.Png);
break;
}//oriImg.Save(ConfigurationSettings.AppSettings["FePicSavePath"] + imgNameNoExt + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);switch(strFePicThumbFormat)
{
//jpeg format can get the smallest file size, and the png is the largest size
//case "jpeg":
case "jpg":
newImg.Save(strFePicSavePath + imgNameOnly + "_thumb.jpg",System.Drawing.Imaging.ImageFormat.Jpeg);
imgThumbnail = imgNameOnly + "_thumb.jpg";
break;
case "gif":
newImg.Save(strFePicSavePath + imgNameOnly + "_thumb.gif",System.Drawing.Imaging.ImageFormat.Gif);
imgThumbnail = imgNameOnly + "_thumb.gif";
break;
case "png":
newImg.Save(strFePicSavePath + imgNameOnly + "_thumb.png",System.Drawing.Imaging.ImageFormat.Png);
imgThumbnail = imgNameOnly + "_thumb.png";
break;
default:
newImg.Save(strFePicSavePath + imgNameOnly + "_thumb.jpg",System.Drawing.Imaging.ImageFormat.Jpeg);
imgThumbnail = imgNameOnly + "_thumb.jpg";
break;}//switchpicInfo.Append("<b>文件 名:</b>" + imgNameOnly + " ( " + oriImg.Width + " x " + oriImg.Height + " ) " + PostedFile.ContentLength/1024 + "KB<br/>");
picInfo.Append("<b>缩略图名:</b>" + imgThumbnail + " ( " + newImg.Width + " x " + newImg.Height + " )<br/><br/>");oriImg.Dispose();
newImg.Dispose();}//for
picInfo.Append("<font color=red>所有操作成功</font><br/>");}// if erronumber = 0}
else
{
picInfo.Append("<font color=red>有错误,请检查。操作未成功</font><br/>");}lblPicInfo.Text = picInfo.ToString();}
</FONT>
public class UploadFile : System.Web.UI.Page
{
/// <summary>
/// 保存上传的文件。
/// </summary>
public static void Save(
HtmlInputFile e,
out string FilePath,//返回文件保存的虚拟路径
out string FileName,//返回上传文件的文件名
int FileSize,
string FileType,
string UploadFolder
)
{
if(!CheckSize(e,FileSize))
{
throw new Exception("上传的文件不能大于" + FileSize + "KB!");
} string ExtensionName = Path.GetExtension(e.PostedFile.FileName).ToLower();
if(!CheckType(FileType,ExtensionName))
{
throw new Exception("上传的文件类型不正确!");
}
string SavePath = HttpContext.Current.Request.PhysicalApplicationPath + UploadFolder.Replace("/","\\");
string SaveName = GetFileName(ExtensionName,SavePath);
SaveFile(e,SavePath, SaveName);
FilePath = SaveName;
FileName = Path.GetFileName(e.PostedFile.FileName);
} /// <summary>
/// 删除上传的文件。
/// </summary>
public static void Delete(string FilePath)
{
string DeletePath = HttpContext.Current.Request.PhysicalApplicationPath + "\\" + FilePath.Replace("/","\\");
if (File.Exists(DeletePath))
{
File.Delete(DeletePath);
}
} /// <summary>
/// 检查上传文件的大小。
/// </summary>
private static bool CheckSize(HtmlInputFile e,int FileSize)
{
if(e.PostedFile.ContentLength > 0 && e.PostedFile.ContentLength <= FileSize*1024)
{
return true;
}
else
{
return false;
}
}
/// <summary>
/// 检查上传文件的类型(扩展名)。
/// </summary>
private static bool CheckType(string FileType,string ExtensionName)
{
string[] fileType = FileType.ToLower().Split(new char[]{';'});
for(int i = 0;i <= fileType.GetUpperBound(0);i++)
{
if(ExtensionName == ("." + fileType[i]))
{
return true;
}
}
return false;
} /// <summary>
/// 用当前时间作为文件名。
/// </summary>
private static string GetFileName(string ExtensionName,string SavePath)
{
StringBuilder sb = new 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());
sb.Append(DateTime.Now.Millisecond.ToString());
sb.Append(ExtensionName); if (File.Exists(SavePath + "\\" + sb.ToString()))
{
return GetFileName(ExtensionName,SavePath);
} return sb.ToString();
} /// <summary>
/// 保存文件到工作文件夹。
/// </summary>
private static void SaveFile(HtmlInputFile e,string SavePath,string SaveName)
{
try
{
e.PostedFile.SaveAs(SavePath + "\\" + SaveName);
}
catch(Exception ex)
{
throw ex;
}
}
}