C#上传并生成缩略图类
只是用它来上传文件;
程序代码 程序代码

using System;
using System.IO;
using System.Web;
using System.Drawing;
using System.Drawing.Imaging;
/// <summary>
/// UpLoad 的摘要说明
/// </summary>
public class UpLoad
{
    public UpLoad()
    {
        //
        // TODO: 在此处添加构造函数逻辑
        //
    }
    public static string SaveAs(HttpPostedFile file, string path)
    {
        if (CreatePath(path))
        {
            string filename;
            string extension;
            extension = Path.GetExtension(file.FileName).ToLower();

            do
            {
                filename = Guid.NewGuid().ToString().Substring(24) + extension;
            } while (File.Exists(path + filename));

            file.SaveAs(path + filename);
            return filename;
        }
        return "";
    }

    public static string SavePictureAs(HttpPostedFile file, string path)
    {
        if (CreatePath(path))
        {
            string filename;
            string extension;
            if (file.ContentType.ToString().ToLower().IndexOf("image") >= 0)
            {
                extension = Path.GetExtension(file.FileName).ToLower();

                do
                {
                    filename = Guid.NewGuid().ToString().Substring(24) + extension;
                } while (File.Exists(path + filename));

                file.SaveAs(path + "/" + filename);
                return filename;
            }
        }
        return "";
    }

    public static string SavePictureAs(HttpPostedFile file, string path, string filename)
    {
        if (CreatePath(path))
        {

            //string filename;
            //string extension;

            if (file.ContentType.ToString().ToLower().IndexOf("image") >= 0)
            {
                file.SaveAs(path + filename);
                return filename;
            }
        }
        return filename;
    }

    public static string SaveThumbnailAs(HttpPostedFile file, string path)
    {
        if (CreatePath(path))
        {
            string filename;
            int Width;
            int Height;

            if (file.ContentType.ToLower().IndexOf("image") >= 0)
            {
                System.Drawing.Image oImg = System.Drawing.Image.FromStream(file.InputStream);

                if (oImg.Height > oImg.Width)
                {
                    Height = 50;
                    Width = (50 * oImg.Width) / oImg.Height;
                }
                else
                {
                    Width = 50;
                    Height = (50 * oImg.Height) / oImg.Width;
                }

                oImg = oImg.GetThumbnailImage(Width, Height, null, IntPtr.Zero);

                do
                {
                    filename = Guid.NewGuid().ToString().Substring(24) + ".jpg";
                } while (File.Exists(path + filename));

                //oImg.Save(path + filename, oImg.RawFormat);
                oImg.Save(path + filename, ImageFormat.Jpeg);
                return filename;
            }
        }
        return "";
    }

    private static bool CreatePath(string path)
    {
        try
        {
            if (!Directory.Exists(path))
            {
                if (Directory.GetParent(path).Exists)
                {
                    Directory.CreateDirectory(path);
                }
                else
                {
                    CreatePath(Directory.GetParent(path).ToString());
                }
            }
            return true;
        }
        catch (Exception)
        {
            return false;
        }
    }
}
posted on 2009-03-30 20:08  许维光  阅读(469)  评论(0)    收藏  举报