WebForm水印照片

快来打我* 2021-06-26 16:06 458阅读 0赞

水印照片需要的元素

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

展示页面

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
  2. <!DOCTYPE html>
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head runat="server">
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  6. <title></title>
  7. <style> #Image1 { width:300px; height:300px; } </style>
  8. </head>
  9. <body>
  10. <form id="form1" runat="server">
  11. <div>
  12. <asp:FileUpload ID="FileUpload1" runat="server" />
  13. <asp:Button ID="Button1" runat="server" Text="上传" />
  14. <asp:Image ID="Image1" runat="server" />
  15. </div>
  16. </form>
  17. </body>
  18. </html>

代码面

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using System.Drawing;
  8. public partial class _Default : System.Web.UI.Page
  9. {
  10. protected void Page_Load(object sender, EventArgs e)
  11. {
  12. Button1.Click += Button1_Click;
  13. }
  14. void Button1_Click(object sender, EventArgs e)
  15. {
  16. //绘制画布:接收FileUpload1选中的图片,图片即画布
  17. System.Drawing.Image img = System.Drawing.Image.FromStream(FileUpload1.FileContent);
  18. //创建绘制对象,告诉它在哪张照片进行绘制
  19. Graphics g = Graphics.FromImage(img);
  20. //绘制文本内容
  21. string s = "这是一张照片";
  22. //绘制文本画刷和画刷颜色
  23. Brush b = new SolidBrush(System.Drawing.Color.Red);
  24. //绘制文本字体和大小
  25. Font f = );
  26. //开始绘制文本
  27. g.DrawString(s,f,b,,);
  28. //设置水印后的图片的相对路径和名字 image文件夹与Default平级
  29. string path = "image/"+DateTime.Now.ToString("yyyy年MM月dd日hh时mm分ss秒")+FileUpload1.FileName;
  30. //保存照片
  31. img.Save(Server.MapPath(path));
  32. //页面显示水印后的照片
  33. Image1.ImageUrl = path;
  34. }
  35. }

完!!


C#中使用DrawString绘制文本时的对齐方式

  1. void 绘制文字(Graphics 画家)
  2. {
  3. StringFormat 格式 = new StringFormat();
  4. 格式.Alignment = StringAlignment.Center; //居中
  5. 格式.Alignment = StringAlignment.Far; //右对齐
  6. string 文本 = "";
  7. Rectangle 矩形 = new Rectangle(0, 0, 200, 200);
  8. Font 字体 = new Font("宋体", 10.5F);
  9. Brush 画笔 = Brushes.Blue;
  10. 画家.DrawString(文本, 字体, 画笔, 矩形, 格式);
  11. }

发表评论

表情:
评论列表 (有 0 条评论,458人围观)

还没有评论,来说两句吧...

相关阅读

    相关 WebForm 三级联动

    三级联动 数据库根据父级代号条件写查询 返回list<>集合 方法一: 创建三个下拉列表: ※AutoPostBack:否发生自动回传到服务器的操作。如果把该属性设置为