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>
代码面
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 = );
//开始绘制文本
g.DrawString(s,f,b,,);
//设置水印后的图片的相对路径和名字 image文件夹与Default平级
string path = "image/"+DateTime.Now.ToString("yyyy年MM月dd日hh时mm分ss秒")+FileUpload1.FileName;
//保存照片
img.Save(Server.MapPath(path));
//页面显示水印后的照片
Image1.ImageUrl = path;
}
}
完!!
C#中使用DrawString
绘制文本时的对齐方式
void 绘制文字(Graphics 画家)
{
StringFormat 格式 = new StringFormat();
格式.Alignment = StringAlignment.Center; //居中
格式.Alignment = StringAlignment.Far; //右对齐
string 文本 = "";
Rectangle 矩形 = new Rectangle(0, 0, 200, 200);
Font 字体 = new Font("宋体", 10.5F);
Brush 画笔 = Brushes.Blue;
画家.DrawString(文本, 字体, 画笔, 矩形, 格式);
}
还没有评论,来说两句吧...