產生縮圖並加浮水印的方法

來源:互聯網
上載者:User

<!--aspx檔案-->
<%@ Page language="c#" Codebehind="WaterPhoto.aspx.cs" AutoEventWireup="false" Inherits="Example.WaterPhoto.WaterPhoto" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
 <HEAD>
  <title>WaterPhoto</title>
  <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
  <meta name="CODE_LANGUAGE" Content="C#">
  <meta name="vs_defaultClientScript" content="JavaScript">
  <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
 </HEAD>
 <body MS_POSITIONING="GridLayout">
  <form id="Form1" method="post" runat="server" Enctype="multipart/form-data">
   <FONT face="宋體"><input type="file" id="UploadFile" runat="Server" NAME="UploadFile"><br>
    <asp:Button id="Button1" style="Z-INDEX: 101; LEFT: 392px; POSITION: absolute; TOP: 264px" runat="server"
     Text="加浮水印"></asp:Button>
    <asp:Button id="Button2" style="Z-INDEX: 102; LEFT: 504px; POSITION: absolute; TOP: 272px" runat="server"
     Text="產生縮圖"></asp:Button>
    <asp:Image id="Image1" style="Z-INDEX: 103; LEFT: 16px; POSITION: absolute; TOP: 88px" runat="server"></asp:Image>
    <asp:Image id="Image2" style="Z-INDEX: 104; LEFT: 200px; POSITION: absolute; TOP: 96px" runat="server"></asp:Image>
    <asp:TextBox id="TextBox1" style="Z-INDEX: 105; LEFT: 16px; POSITION: absolute; TOP: 48px" runat="server"></asp:TextBox></FONT>
  </form>
 </body>
</HTML>
<!--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.Text;
using System.Drawing.Imaging;
using System.IO;

namespace Example.WaterPhoto
{
 /// <summary>
 /// WaterPhoto 的摘要說明。
 /// </summary>
 public class WaterPhoto : System.Web.UI.Page
 {
  protected System.Web.UI.HtmlControls.HtmlInputFile UploadFile;
  protected System.Web.UI.WebControls.Button Button2;
  protected System.Web.UI.WebControls.Image Image1;
  protected System.Web.UI.WebControls.Image Image2;
  protected System.Web.UI.WebControls.TextBox TextBox1;
  protected System.Web.UI.WebControls.Button Button1;
 
  private void Page_Load(object sender, System.EventArgs e)
  {
   // 在此處放置使用者代碼以初始化頁面
  }

  #region Web Form設計器產生的程式碼
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN: 該調用是 ASP.NET Web Form設計器所必需的。
   //
   InitializeComponent();
   base.OnInit(e);
  }
  
  /// <summary>
  /// 設計器支援所需的方法 - 不要使用代碼編輯器修改
  /// 此方法的內容。
  /// </summary>
  private void InitializeComponent()
  {   
   this.Button1.Click += new System.EventHandler(this.Button1_Click);
   this.Button2.Click += new System.EventHandler(this.Button2_Click);
   this.Load += new System.EventHandler(this.Page_Load);

  }
  #endregion

  private void Button1_Click(object sender, System.EventArgs e)
  {
   //string Path;
   if(UploadFile.PostedFile.FileName.Trim()!="")
   {
    //上傳檔案
    string extension = Path.GetExtension(UploadFile.PostedFile.FileName).ToUpper();
    string fileName = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString();
    string path = Server.MapPath(".") + "/UploadFile/" + fileName + extension;
    UploadFile.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", 32);
    Brush b = new SolidBrush(Color.White);
    string addText ="加文字浮水印";
    g.DrawString(addText, f, b, 10, 10);
    g.Dispose();

    //加圖片浮水印
//    System.Drawing.Image image = System.Drawing.Image.FromFile(path);
//    System.Drawing.Image copyImage = System.Drawing.Image.FromFile( Server.MapPath(".") + "/Noname1.gif");
//    Graphics g = Graphics.FromImage(image);
//    g.DrawImage(copyImage, new Rectangle(image.Width-copyImage.Width, image.Height-copyImage.Height, copyImage.Width, copyImage.Height), 0, 0, copyImage.Width, copyImage.Height, GraphicsUnit.Pixel);
//    g.Dispose();

    //儲存加浮水印過後的圖片,刪除原始圖片
    string newPath = Server.MapPath(".") + "/UploadFile/" + fileName + "_new" + extension;
    image.Save(newPath);
    image.Dispose();
    if(File.Exists(path))
    {
     File.Delete(path);
    }

    Response.Redirect(newPath);
   }
  
  }

  private void Button2_Click(object sender, System.EventArgs e)
  {
   if(UploadFile.PostedFile.FileName!=null)
   {   
    string namestr = Path.GetFileName(UploadFile.PostedFile.FileName);//提取檔案名稱  
    UploadFile.PostedFile.SaveAs(Server.MapPath(".")+@"\"+namestr);
    Image2.Visible=true;
    Image2.ImageUrl=Server.MapPath(".")+@"\"+namestr;
    System.Drawing.Image image,aNewImage;
    image=System.Drawing.Image.FromStream(UploadFile.PostedFile.InputStream);
    decimal width=image.Width;
    decimal height=image.Height;
    int newwidth,newheight;
    if(width>height)
    {
     newwidth=150;
     newheight=(int)(height/width*150);
    }
    else
    {
     newheight=150;
     newwidth=(int)(width/height*150);
    }
    aNewImage=image.GetThumbnailImage(newwidth,newheight,null,IntPtr.Zero);
    Bitmap output=new Bitmap(aNewImage);
    Graphics g=Graphics.FromImage(output);
    g.DrawString(TextBox1.Text.Trim(),new Font("Courier New", 9),new SolidBrush(Color.Red),60,60);//寫著作權資訊及文字格式設定及位置
    output.Save(Server.MapPath(".")+@"\s_"+namestr,System.Drawing.Imaging.ImageFormat.Jpeg);
    Image1.Visible=true;
    Image1.ImageUrl=Server.MapPath(".")+@"\s_"+namestr;
   }  
  }
 }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.