asp.net上傳圖片加浮水印(c#)

來源:互聯網
上載者:User
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;
    }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.