技术资讯   > 技术文章 > 图片上传包括缩略、文字及图片水印\图片上传(本站原创)
[发表时间: 2008-07-14 01:43 ]

核心代码如下:

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.IO;
using System.Text;
using System.Drawing;


public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string path = FileUpload1.PostedFile.FileName;
        string type = FileUpload1.PostedFile.ContentType;
        int filelen = FileUpload1.PostedFile.ContentLength;
        type = type.Substring(0, 5);

        if (path != null&&path!="")
        {
            if (type != "image")
            {
                HttpContext.Current.Response.Write("上传文件类型不对");
                return;
            }
            if (filelen > 200000000)
            {
                HttpContext.Current.Response.Write("上传文件太大!");
                return;
            }
            string imgfile = DateTime.Now.ToString("yyyyMMddhhmmssffff");
            string exte = Path.GetExtension(path);
            string imgfilename = imgfile + exte;
            //
            string mulu = HttpContext.Current.Server.MapPath("~/" + "image/") + DateTime.Now.ToString("yyyyMMdd");
            if (!Directory.Exists(mulu))
            {
                Directory.CreateDirectory(mulu);
            }
           string newpath=mulu+"/"+imgfilename;
           //Label1.Text = newpath;
            //=========================
           string simgfile = DateTime.Now.ToString("yyyyMMddhhmmssff");
           //string exte = Path.GetExtension(path);
           string simgfilename = simgfile + exte;
           string smulu = HttpContext.Current.Server.MapPath("~/" + "simage/") + DateTime.Now.ToString("yyyyMMdd");
           if (!Directory.Exists(smulu))
           {
               Directory.CreateDirectory(smulu);
           }
           string snewpath = smulu + "/" + simgfilename;
            //=========================
           try
           {
              
               FileUpload1.PostedFile.SaveAs(newpath);
               FileUpload1.Dispose();
               Simages(newpath, snewpath);
              
             
               //添加文字水印
               System.Drawing.Image im =System.Drawing.Image.FromFile(newpath);
               Graphics g = Graphics.FromImage(im);
               g.DrawImage(im, 0, 0, im.Width, im.Height);
               Font f = new Font("文鼎中行书简", 15);
               Brush b = new SolidBrush(Color.White);
               string s="全诚软件 www.1wsoft.cn";
               g.DrawString(s, f, b, 10, 10);
               g.Dispose();
               string np = HttpContext.Current.Server.MapPath(".") +"/image/"+ DateTime.Now.ToString("yyyyMMdd") + "/" + imgfile+"_new"+exte;
               im.Save(np);
               im.Dispose();
           
              //添加图片水印

               System.Drawing.Image ima = System.Drawing.Image.FromFile(np);
               System.Drawing.Image copyImage = System.Drawing.Image.FromFile(Server.MapPath(".") + "/logo.png");
               g = Graphics.FromImage(ima);
               g.DrawImage(copyImage, new Rectangle(ima.Width - copyImage.Width, ima.Height - copyImage.Height,

               copyImage.Width, copyImage.Height), 0, 0, copyImage.Width, copyImage.Height, GraphicsUnit.Pixel);
               g.Dispose();
               string newPath = HttpContext.Current.Server.MapPath(".") + "/image/" + DateTime.Now.ToString("yyyyMMdd") + "/" + imgfile + "_neww" + exte;
               ima.Save(newPath);
               ima.Dispose();

              // if (File.Exists(newpath))
               // {
               //    File.Delete(newpath);
              //   }


               Image1.Visible = true;
               Image1.ImageUrl = "~/image/" + DateTime.Now.ToString("yyyyMMdd") + "/" + imgfile + "_neww" + exte;
               Image2.Visible = true;
               Image2.ImageUrl = "~/simage/" + DateTime.Now.ToString("yyyyMMdd") + "/" + simgfilename;
               TextBox1.Text = imgfilename;
               HttpContext.Current.Response.Write("上传成功");
           }
           catch (Exception ex)
           {
               HttpContext.Current.Response.Write(ex.ToString());
           }
          
        }
        else
        {
            HttpContext.Current.Response.Write("请选择上传文件!");
            return;
        }

 

    }
    public void Simages(string pathfrom, string pathto)
    {
        int x;
        int y;

        float x1 = Bitmap.FromFile(pathfrom).Width;
        float y1 = Bitmap.FromFile(pathfrom).Height;

        float z = x1 / y1;
        x = Convert.ToInt32(x1);
        y = Convert.ToInt32(y1);
        float u = 1 / z;
        if (z > 1)
        {
            x = 130;

            y = Convert.ToInt32(130 * u);
        }
        else if (z < 1)
        {
            y = 130;

            //Response.Write(z);

            x = Convert.ToInt32(130 * z);
        }
        else
        {
            x = 130;
            y = 130;
        }
        System.Drawing.Image img = System.Drawing.Image.FromFile(pathfrom);
        img = img.GetThumbnailImage(x, y, null, IntPtr.Zero);//width,height,是高,改变图片的大小
        img.Save(pathto);
        img.Dispose();
       
       // return true;
    }
}