using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;/// <summary>
/// Cls_pic 的图片操作说明
/// </summary>
public class Cls_pic
{
    /// <summary>
    /// 图片验证码
    /// </summary>
    /// <param name="typeid">(0数字验证,1字母验证)</param>
    /// <param name="regxid">(0cookies验证,1session验证)</param>
    /// <param name="CodeName">cookies/session名</param>
    public static void GetCode(int typeid, int regxid,string CodeName)
    {
        string Txt;
        Random Rtxt = new Random();
        int w = 70;
        int h = 22;
        switch (typeid)
        {
            case 0:
                Txt = Rtxt.Next(100000, 1000000).ToString();
                break;
            case 1:
                w = 80;
                StringBuilder Stxt = new StringBuilder();
                string ABC = "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z";
                for (int i = 0; i < 5; i++)
                {
                    Stxt.Append(ABC.Split(',')[Rtxt.Next(1, 16)]);
                }
                Txt = Stxt.ToString();
                break;
            default:
                Txt = Rtxt.Next(100000, 1000000).ToString();
                break;
        }
        switch (regxid)
        {
            case 0:
                HttpContext.Current.Response.Cookies[CodeName].Value = Txt;
                break;
            case 1:
                HttpContext.Current.Session[CodeName] = Txt;
                break;
        }
        Bitmap Bmp = new Bitmap(w, h);
        Graphics G = Graphics.FromImage(Bmp);
        G.Clear(Color.Beige);
        Font cfont = new Font("宋体,Arial", 13, FontStyle.Bold);
        Brush cbrush = Brushes.Brown;
        G.DrawString(Txt, cfont, cbrush, 2, 2);
        HttpContext.Current.Response.ContentType = "image/jpeg";
        Bmp.Save(HttpContext.Current.Response.OutputStream, ImageFormat.Jpeg);
        G.Dispose();
        Bmp.Dispose();
    }
    /// <summary>
    /// 生成图片缩放
    /// </summary>
    /// <param name="PicPath">图片相对路经</param>
    /// <param name="w">宽度</param>
    /// <param name="h">高度</param>
    /// <param name="bit">是否水印</param>
    /// <returns></returns>
    public static string PicCreateSmall(string PicPath, int w, int h, bool bit)
    {
        string filename = PicPath.Substring(0, PicPath.LastIndexOf(".")) + "_small" + PicPath.Substring(PicPath.LastIndexOf("."), PicPath.Length - PicPath.LastIndexOf("."));
        if (!File.Exists(HttpContext.Current.Server.MapPath(PicPath)))
        {
            Cls_Common.ErrView("·要缩放的原图片不存在或<BR>·路经(" + PicPath + ")错误");
        }
        Bitmap Bmp = new Bitmap(HttpContext.Current.Server.MapPath(PicPath));
        Bitmap CreatePic = new Bitmap(w, h);
        Graphics G = Graphics.FromImage(CreatePic);
        G.DrawImage(Bmp, new System.Drawing.Rectangle(0, 0, w, h), new System.Drawing.Rectangle(0, 0, Bmp.Width, Bmp.Height), System.Drawing.GraphicsUnit.Pixel);
        CreatePic.Save(HttpContext.Current.Server.MapPath(filename), ImageFormat.Jpeg);
        Bmp.Dispose();
        CreatePic.Dispose();
        G.Dispose();
        if (bit == true)
        {
            PicWater(filename);
        }
        return filename;
    }
    /// <summary>
    /// 图片加水印
    /// </summary>
    /// <param name="PicPath">图片相对路经</param>
    /// <returns>返回图片路经名+后缀</returns>
    public static string PicWater(string PicPath)
    {
        string filename = PicPath.Substring(0, PicPath.LastIndexOf(".")) + "_Wat" + PicPath.Substring(PicPath.LastIndexOf("."), PicPath.Length - PicPath.LastIndexOf("."));
        Bitmap Syspic = new Bitmap(HttpContext.Current.Server.MapPath("i" + "ma" + "g" + "es/" + "07" + "1" + "2c" + "l" + "u" + "b" + "." + "g" + "i" + "f"));
        Bitmap Bmp = new Bitmap(HttpContext.Current.Server.MapPath(PicPath));
        Graphics G = Graphics.FromImage(Bmp);
        G.DrawImage(Syspic, Bmp.Width - Syspic.Width, Bmp.Height - Syspic.Height);
        Bmp.Save(HttpContext.Current.Server.MapPath(filename), ImageFormat.Jpeg);
        Syspic.Dispose();
        Bmp.Dispose();
        G.Dispose();
        if (File.Exists(HttpContext.Current.Server.MapPath(PicPath)))
        {
            File.Delete(HttpContext.Current.Server.MapPath(PicPath));
        }
        return filename;
    }
    /// <summary>
    /// 上传图片
    /// </summary>
    /// <param name="MyUpfile">上传控件</param>
    /// <param name="UpPath">上传相对路经</param>
    /// <param name="DBpath">返回相对路经</param>
    /// <param name="UpFoldertype">文件夹格式</param>
    /// <param name="Uptype">上传格式(*表示不控制格式)</param>
    /// <param name="ReName">是否重命名</param>
    /// <returns>返回相对路经</returns>
    public static string Uploadfile(FileUpload MyUpfile, string UpPath, string DBpath, int UpFoldertype, string Uptype, bool ReName)
    {
        //检测格式
        string Hz = MyUpfile.FileName.Substring(MyUpfile.FileName.LastIndexOf('.')+1, MyUpfile.FileName.Length -1- MyUpfile.FileName.LastIndexOf('.')).ToLower();
        if (Uptype != "*")
        {
            if (Uptype.ToLower().Replace(Hz, "") == Uptype.ToLower())
            {
                Cls_Common.ErrView("上传格式错误,格式仅限:" + Uptype);
            }
        }        if (MyUpfile.FileName != "")
        {
            //检测上传目录
            if (Cls_Fso.FsoIsTrue(UpPath + Cls_Common.UpFileDir(UpFoldertype), 0) == false) { Cls_Fso.FsoCreate(UpPath + Cls_Common.UpFileDir(UpFoldertype), 0); }
            string UpDir = Cls_Common.UpFileDir(UpFoldertype);   //上传目录格式
            string Name = MyUpfile.FileName;                     //原始文件名(含后缀)
            if (ReName)
            {
                //随机名
                Name = DateTime.Now.ToFileTimeUtc().ToString() + "." +Hz;
            }
            string UpFile = System.Web.HttpContext.Current.Server.MapPath(UpPath + UpDir + Name);
            MyUpfile.SaveAs(UpFile);
            return DBpath + UpDir + Name;
        }
        return null;
    }
}信息来源http://www.spouseseo.com