参考下这个吧:
http://expert.csdn.net/Expert/topic/1553/1553351.xml?temp=.6691553

解决方案 »

  1.   

    片上传的功能简介及web.config设置(自动生成所略图)
    功能:
    1。把图片文件(JPG GIF PNG)上传,
    2。保存到指定的路径(在web.config中设置路径,以文件的原有格式保存),
    3。并自动生成指定宽度的(在web.config中设置宽度)
    4。和指定格式的(在web.config中指定缩略图的格式,支持GIF,JPG,PNG)
    5。和原图比例相同的缩略图(根据宽度和原图的宽和高计算所略图的高度)
    6。可以判断是否已经存在文件
    7。如果不覆盖,则给出错误
    8。如果选中"覆盖原图"checkbox,则覆盖原图。
    9。可以根据要求,在webform上设置1个以上的file input和相应的checkbox
    10。并在文件上传完毕后,显示原图的文件名,尺寸,字节,和
    11。缩略图的文件名尺寸,以及
    12。显示原图和缩略图。
    13。缩略图的文件名格式,以便与管理:
    大图(原图): 图片分类代号_图片代表的日期_图片原有文件名.原有格式 (如:28_2002-1-28_test.jpg)
    缩略图: 图片分类代号_图片代表的日期_图片原有文件名.原有格式_thumb.指定的缩略图格式 (如:28_2002-1-28_test.jpg_thumb.gif)----------------------
    web.config文件中的相应设置:<?xml version="1.0" encoding="utf-8" ?>
    <configuration>
    <system.web> 
    ...
    ...
    </system.web>
    <appSettings>
    ...
    ...
    ...
    ...
    //FePicSavePath 图片保存在服务器上的实际路径
    <add key="FePicSavePath" value="d:/myroot/myapp/content/fepics/" />
    //FePicWebPath 图片的网络路径,用于显示图片
    <add key="FePicWebPath" value="http://www.mysite.com/fepics/" />
    //FePicThumbWidth 缩略图的宽度
    <add key="FePicThumbWidth" value="115" />
    //所略图的格式
    <add key="FePicThumbFormat" value="gif" /> 
    </appSettings>
    </configuration> 
      

  2.   

    图片上传的数据库部分(自动生成所略图)
    public int FePicDataSet(string strPicTitle, string strPicDate, string strPicName, int intPicType, string strPicIntro, string strThumbnail, int opID) 
    {//string strPicTitle, 
    //string strPicDate, 
    //string strPicName, 
    //int intPicType, 
    //string strPicIntro, 
    //string strPicName, 
    //string strThumbnail, 
    //int opID
    SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
    SqlCommand myCommand = new SqlCommand("mag_FePicDataSet", myConnection);
    // Mark the Command as a SPROC
    myCommand.CommandType = CommandType.StoredProcedure;// Add Parameters to SPROC
    SqlParameter parameterUserId = new SqlParameter("@opID", SqlDbType.Int);
    parameterUserId.Value = opID;
    myCommand.Parameters.Add(parameterUserId);SqlParameter parameterPicTitle = new SqlParameter("@picTitle", SqlDbType.Char, 30);
    parameterPicTitle.Value = strPicTitle; 
    myCommand.Parameters.Add(parameterPicTitle);SqlParameter parameterPicDate = new SqlParameter("@picDate", SqlDbType.Char, 10);
    parameterPicDate.Value = strPicDate;
    myCommand.Parameters.Add(parameterPicDate); SqlParameter parameterPicName = new SqlParameter("@picName", SqlDbType.Char, 50);
    parameterPicName.Value = strPicName;
    myCommand.Parameters.Add(parameterPicName); SqlParameter parameterPicType = new SqlParameter("@picType", SqlDbType.Int);
    parameterPicType.Value = intPicType;
    myCommand.Parameters.Add(parameterPicType); SqlParameter parameterPicIntro = new SqlParameter("@picIntro", SqlDbType.Char, 255);
    parameterPicIntro.Value = strPicIntro;
    myCommand.Parameters.Add(parameterPicIntro);SqlParameter parameterThumbnail = new SqlParameter("@thumbnail", SqlDbType.Char, 50);
    parameterThumbnail.Value = strThumbnail;
    myCommand.Parameters.Add(parameterThumbnail); SqlParameter parameterHostAddress = new SqlParameter("@opIP", SqlDbType.Char, 15);
    parameterHostAddress.Value = Context.Request.UserHostAddress; 
    myCommand.Parameters.Add(parameterHostAddress); //SqlParameter parameterRID = new SqlParameter("@returnID", SqlDbType.Int);
    //parameterReturnUserID.Value = -1;
    //parameterRID.Direction = ParameterDirection.Output;
    //myCommand.Parameters.Add(parameterRID);// Execute the command in a try/catch to catch duplicate username errors
    try 
    {
    // Open the connection and execute the Command
    myConnection.Open();
    myCommand.ExecuteNonQuery();
    }
    catch 
    {// failed to create a new user
    return -1;
    }
    finally 
    {// Close the Connection
    if (myConnection.State == ConnectionState.Open)
    myConnection.Close();
    }return 1;} 
      

  3.   

    图片上传的Codebehind(自动生成所略图)
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Configuration;
    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.Text;
    using cj168.util;
    namespace cj168.Web.Mag.Admins.FeData
    {
    /// <summary>
    /// Summary description for fedata.
    /// </summary>
    public class feUploadPic : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.TextBox txtboxPicTitle;
    protected System.Web.UI.WebControls.Calendar calPicDate;
    protected System.Web.UI.WebControls.DropDownList ddlPicType;
    protected System.Web.UI.WebControls.TextBox txtboxPicIntro;
    protected System.Web.UI.HtmlControls.HtmlInputFile filePicName;
    protected System.Web.UI.WebControls.Button btnSubmit;
    protected System.Web.UI.WebControls.Label lblPicInfo;
    protected System.Web.UI.WebControls.TextBox txtboxPicDate;
    protected System.Web.UI.WebControls.RequiredFieldValidator Requiredfieldvalidator1;
    protected System.Web.UI.WebControls.RegularExpressionValidator vldCatName;
    protected System.Web.UI.WebControls.RequiredFieldValidator Requiredfieldvalidator2;
    protected System.Web.UI.WebControls.CustomValidator CustomValidator1;
    protected System.Web.UI.WebControls.RegularExpressionValidator RegularExpressionValidator1;
    protected System.Web.UI.WebControls.HyperLink hlkOriPic;
    protected System.Web.UI.WebControls.HyperLink hlkNewPic;
    protected System.Web.UI.WebControls.RegularExpressionValidator Regularexpressionvalidator2;
    protected System.Web.UI.WebControls.RegularExpressionValidator Regularexpressionvalidator3;
    protected System.Web.UI.WebControls.CheckBoxList checkboxlistRewrite;private void Page_Load(object sender, System.EventArgs e)
    {
    // Put user code to initialize the page here
    //if(Page.IsPostBack)
    //txtboxPicDate.Text = calPicDate.SelectedDate.ToString(); if(!Page.IsPostBack)
    {
    Bind2DropDownListPicType();//for(int i = 0;i < Request.Files.Count; i++)
    //{ 
    // checkboxlistRewrite.Items[i].Selected = false;
    //}
    }}public void Bind2DropDownListPicType()
    {
    cj168.DataAccess.Admins admins = new cj168.DataAccess.Admins();
    DataSet ds = admins.FePicTypeGet();
    //ddlPicType.DataSource = ;
    DataView dv = new DataView(ds.Tables["PicType"]);DataRowView drv = dv.AddNew();
    //DataColumnView dcv = dv.AddNew();
    drv["Title"] = "请选择";
    drv["fePicTypeID"] = "0";
    drv.EndEdit(); 
    dv.Sort = "fePicTypeID"; ddlPicType.DataSource = dv;ddlPicType.DataBind();
    }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;
    string strPicTitle = txtboxPicTitle.Text;
    string strPicIntro = txtboxPicIntro.Text;
    string strPicDate = txtboxPicDate.Text;int intPicType = Int32.Parse(ddlPicType.SelectedItem.Value);
    string strPicType = intPicType.ToString() + "_" + DateTime.Now.Date.ToShortDateString() + "_";
    string strFePicWebPath = ConfigurationSettings.AppSettings["FePicWebPath"];cj168.DataAccess.Admins admins = new cj168.DataAccess.Admins();//if(admins.FePicTypeSet(textboxTitle.Text,0) < 0)
    // lblAddPicInfo.Text = "操作失败:已经存在相同名称类型,请修改";
    //else
    // lblAddPicInfo.Text = "操作成功";
    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();
    //5-test.jpg
    imgNameOnly = strPicType + 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;
    if(fileExt == "")
    picInfo.Append("<b>错误:</b>请选择文件<br>" );
    else
    picInfo.Append("<b>错误:</b>文件("+ (i+1) +") " + imgNameOnly + " 扩展名 " + fileExt + " 不被许可<br>" );
    } }if(erroNumber > 0) 
    {
    picInfo.Append("<font color=red>全部操作均未完成,请修改错误,再进行操作</font><br>");hlkOriPic.ImageUrl = "";
    hlkOriPic.ToolTip = "";
    hlkNewPic.ImageUrl = "";
    hlkNewPic.ToolTip = "";
    }
    else 
    {
    for(int i = 0;i < Request.Files.Count; i++)
    {HttpPostedFile PostedFile = Request.Files[i];
    imgNameOnly = strPicType + 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;