WebForm水印照片

水印照片需要的元素

绘制:
1、画布
2、画笔 样式 粗细 颜色
3、画什么东西
4、用什么字体画 大小
5、位置

 

展示页面

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    <style>
        #Image1 {
        width:300px;
        height:300px;
        
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:FileUpload ID="FileUpload1" runat="server" />
        <asp:Button ID="Button1" runat="server" Text="上传" />
        <asp:Image ID="Image1"  runat="server" />
    </div>
    </form>
</body>
</html>
View Code

 

代码面

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)
    {

        //绘制画布:接收FileUpload1选中的图片,图片即画布
        System.Drawing.Image img = System.Drawing.Image.FromStream(FileUpload1.FileContent);

        //创建绘制对象,告诉它在哪张照片进行绘制
        Graphics g = Graphics.FromImage(img);
        //绘制文本内容
        string s = "这是一张照片";
        //绘制文本画刷和画刷颜色
        Brush b = new SolidBrush(System.Drawing.Color.Red);
        //绘制文本字体和大小
        Font f = new Font("宋体", 30);
        //开始绘制文本
        g.DrawString(s,f,b,50,50);
        //设置水印后的图片的相对路径和名字  image文件夹与Default平级
        string path = "image/"+DateTime.Now.ToString("yyyy年MM月dd日hh时mm分ss秒")+FileUpload1.FileName;
        //保存照片
        img.Save(Server.MapPath(path));
        //页面显示水印后的照片
        Image1.ImageUrl = path;
    }
}
View Code

完!!

 

posted @ 2016-11-18 13:10  冲天小肥牛  阅读(210)  评论(0编辑  收藏  举报