利用VS2005的FileUpload控件做一个图片上传类

该文原创,转载请注名出处。谢谢
配合VS2005里的文件上传控件,做了一个图片上传的例子,并把图片另存为70*70大小,如要判断图片大小和类型可做在页面代码之中。有兴趣的朋友可以去实现一下。很容易的。

/*
 * 类描述:该类主要作用于上传图片.
 * 
 * 作者:刘荣华
 * 创建日期:2006/11/21
 * 
 
*/

using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Configuration;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI;
using System.Collections;
using System.Web.UI.HtmlControls;
using System.Data;

namespace UploadPicture
{
    
public class CUploadPic
    
{
        
        
public bool Upload(FileUpload upLoadName,HttpServerUtility Server,out string ImgUrl)
        
{
            
bool flag = true;
            
try
            
{   
                
/*读取保存原图的虚拟目录名称*/
                
string DuumyActuallyPic = ConfigurationManager.AppSettings["ActuallyPic"];
                
/*保存原图的路径*/
                
string pathActuallyPic = Server.MapPath("../" + DuumyActuallyPic + "/");
                
string fileName = upLoadName.FileName.Trim();
                
string NewFileName = CreatFileName(fileName);
                
string newPathActuallyPic = pathActuallyPic + NewFileName;

                
/*保存原图*/
                upLoadName.SaveAs(newPathActuallyPic);
                CreateBreviaryPic(newPathActuallyPic,upLoadName.FileName,Server, 
out ImgUrl);
            }

            
catch (Exception e)
            
{
                flag 
= false;
                
throw e;

            }

            
return flag;
        }

        
/// <summary>
        
/// 产生缩略图
        
/// </summary>
        
/// <param name="picPath"></param>

        void CreateBreviaryPic(string picPath,string FileName,HttpServerUtility Server,out string ImgUrl)
        
{
            
string HostDomain = ConfigurationManager.AppSettings["Address"];
            
/*读取保存缩略图的虚拟目录名称*/
            
string DummyBreviaryPic = ConfigurationManager.AppSettings["BreviaryPic"];
            
/*保存缩略图的路径*/
            
string pathBreviaryPic = Server.MapPath("../" + DummyBreviaryPic + "/");
            
string NewFileName = CreatFileName(FileName);

            
string newPathBreviaryPic = pathBreviaryPic + NewFileName;

            System.Drawing.Image obj 
= System.Drawing.Image.FromFile(picPath);
            
int imgHeight = obj.Height;
            
int imgWidth = obj.Width;
            
/**/
            
if (imgHeight != 70)
            
{
                imgHeight 
= 70;
            }

            
if (imgWidth != 70)
            
{
                imgWidth 
= 70;
            }


            System.Drawing.Image Breviary 
= obj.GetThumbnailImage(imgWidth, imgHeight, null, IntPtr.Zero);
            Breviary.Save(newPathBreviaryPic);

            obj.Dispose();
            Breviary.Dispose();
            
/*生成的图片连接地址=网站域名+虚拟目录名+文件名*/
            ImgUrl 
= HostDomain + "/" + DummyBreviaryPic + "/" + NewFileName;
        }

        
/// <summary>
        
/// 生成唯一的图片名
        
/// </summary>
        
/// <param name="fileName"></param>
        
/// <returns></returns>

        string CreatFileName(string fileName)
        
{
            DateTime now 
= DateTime.Now;
            
string str =
                now.Year.ToString() 
+ "_"
                
+ now.Month.ToString() + "_"
                
+ now.Day.ToString() + "_"
                
+ now.Hour.ToString() + "_"
                
+ now.Minute.ToString() + "_"
                
+ now.Second.ToString() + "_"
                
+ fileName;
            
return str;
        }

    }

}


然后在配置文件中加上
        <add key="Address" value="http://10.0.76.26"/>

    
<!--保存原图的虚拟目录名称-->
    
<add key="ActuallyPic" value="ActuallyGoodsImage"/>
    
<!--保存缩略图的虚拟目录名称-->
    
<add key="BreviaryPic" value="BreviaryImage"/>

就剩下最后一步,在IIS里对照上面的value值,建立你的虚拟目录,保存您的图片就OK
切记虚拟目录名称要和Value一样

posted on 2006-11-21 17:59  刘荣华  阅读(1816)  评论(0编辑  收藏  举报

导航