webform文件上傳加水印

1.文件上傳加水印spa

頁面代碼:code

 

後臺代碼:blog

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Button1.Click += Button1_Click;
    }

    void Button1_Click(object sender, EventArgs e)
    {
        if (FileUpload1.FileName == "") 
        {
            Label1.Text = "上傳的文件不能爲空";
            return;
        }



        //選擇上傳的圖片
        if (FileUpload1.PostedFile.ContentLength > (4 * 1024 * 1024))
        {
            Label1.Text = "文件過大,不能上傳";
        }
        else
        {
            System.Drawing.Image img = System.Drawing.Image.FromStream(FileUpload1.FileContent);

            //繪製水印
            Graphics g = Graphics.FromImage(img);
            //添加的水印
            string s = "你好";
            //繪製的文字樣式
            Font f = new Font("微團雅黑", 30);
            //繪製的顏色
            Brush b = new SolidBrush(Color.Red);
            //繪製開始的座標
            PointF p = new PointF(50, 50);
            g.DrawString(s, f, b, p);

            //上傳
            //路徑
            string path = "Upload/" + DateTime.Now.ToString("yyyyMMddmmssms") + FileUpload1.FileName;
            Label1.Text = "上傳成功";
            //上傳的方法
            img.Save(Server.MapPath(path));
            //上傳的文件在頁面中顯示
            Image1.ImageUrl = path;
        }

    }
}
相關文章
相關標籤/搜索