• asp.net上传图片加水印(c#)


    Default.aspx

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

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&#34;>

    <html xmlns="http://www.w3.org/1999/xhtml&#34; >
    <head runat="server">
        <title>Upload ...</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        
        
            <INPUT id="File1" type="file" size="47" name="File1" runat="server"><br />
                <asp:Button id="Button1"  runat="server" Text="上传并加文字水印" OnClick="Button1_Click1"></asp:Button>
                <asp:Button id="Button2"  runat="server" Text="上传并加图片水印" OnClick="Button2_Click1"></asp:Button>
                <asp:RequiredFieldValidator id="RequiredFieldValidator1"  runat="server" ErrorMessage="*" ControlToValidate="File1"></asp:RequiredFieldValidator>
                <br />
                <asp:Image id="Image1" runat="server" ImageAlign="Middle"></asp:Image>
        
        </div>
        </form>
    </body>
    </html>


    Default.aspx.cs

    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using System.IO;

    public partial class _Default : System.Web.UI.Page
    {
        private void Page_Load(object sender, System.EventArgs e)
        {
        }
        protected void Button1_Click1(object sender, EventArgs e)
        {
            if (File1.PostedFile.FileName.Trim() != "")
            {
                //上传文件
                string extension = Path.GetExtension(File1.PostedFile.FileName).ToLower();
                string fileName = DateTime.Now.ToString("yyyyMMddhhmmss");
                string path = Server.MapPath(".") + "/upload/" + fileName + extension;
                File1.PostedFile.SaveAs(path);

                //加文字水印,注意,这里的代码和以下加图片水印的代码不能共存
                System.Drawing.Image image = System.Drawing.Image.FromFile(path);
                Graphics g = Graphics.FromImage(image);
                g.DrawImage(image, 0, 0, image.Width, image.Height);
                Font f = new Font("Verdana", 16);
                Brush b = new SolidBrush(Color.Blue);
                string addText = "中国114黄页";
                g.DrawString(addText, f, b, 10, 10);
                g.Dispose();

                //保存加水印过后的图片,删除原始图片
                string newPath = Server.MapPath(".") + "/upload/" + fileName + "_new" + extension;
                image.Save(newPath);
                image.Dispose();
                if (File.Exists(path))
                {
                    File.Delete(path);
                }

                Image1.ImageUrl = newPath;
            }
        }
        protected void Button2_Click1(object sender, EventArgs e)
        {
            //上传文件
            string extension = Path.GetExtension(File1.PostedFile.FileName).ToUpper();
            string fileName = DateTime.Now.ToString("yyyyMMddhhmmss");
            string path = Server.MapPath(".") + "/upload/" + fileName + extension;
            File1.PostedFile.SaveAs(path);


            //加图片水印
            System.Drawing.Image image = System.Drawing.Image.FromFile(path);
            System.Drawing.Image copyImage = System.Drawing.Image.FromFile(Server.MapPath(".") + "/tm.jpg");
            Graphics g = Graphics.FromImage(image);
            g.DrawImage(copyImage, new Rectangle((image.Width - copyImage.Width) / 2, (image.Height - copyImage.Height) / 2,

            copyImage.Width, copyImage.Height), 0, 0, copyImage.Width, copyImage.Height, GraphicsUnit.Pixel);
            g.Dispose();

            //保存加水印过后的图片,删除原始图片
            string newPath = Server.MapPath(".") + "/upload/" + fileName + "_new" + extension;
            image.Save(newPath);
            image.Dispose();
            if (File.Exists(path))
            {
                File.Delete(path);
            }

            Image1.ImageUrl = newPath;
        }
    }
  • 相关阅读:
    js实现截图并下载到本地
    div里面的文本内容居中显示
    div里面的p标签内容上下垂直居中
    《将博客搬至CSDN》
    RobotFramework 用例出错后继续操作
    selenium+log4j+eclipse相关问题及解决方案
    linux 安装maven
    LINUX下查看系统参数的常见命令
    Linux常用命令使用
    grep 命令
  • 原文地址:https://www.cnblogs.com/lds85930/p/1507757.html
Copyright © 2020-2023  润新知