C# 上傳圖片,產生縮圖,產生文字或映像浮水印圖

來源:互聯網
上載者:User
<%@ 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"><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server">    <title>無標題頁</title></head><body>    <form id="form1" runat="server">        <div>            &nbsp;            <table>                <tr>                    <td style="width: 100px">                        上傳圖片:</td>                    <td style="width: 100px">                        <asp:FileUpload ID="FileUpload1" runat="server" /></td>                    <td style="width: 100px">                        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="上傳" /></td>                </tr>                <tr>                    <td style="width: 100px" valign="top">                        原圖:</td>                    <td style="width: 100px">                        <asp:Image ID="Image1" runat="server" /></td>                    <td style="width: 100px">                    </td>                </tr>                <tr>                    <td style="width: 100px" valign="top">                        縮圖:</td>                    <td style="width: 100px">                        <asp:Image ID="Image2" runat="server" /></td>                    <td style="width: 100px">                    </td>                </tr>                <tr>                    <td style="width: 100px" valign="top">                        加浮水印:</td>                    <td style="width: 100px">                        <asp:Image ID="Image3" runat="server" /></td>                    <td style="width: 100px">                    </td>                </tr>            </table>        </div>    </form></body></html>

 

view plain copy to clipboard print ?
  1. using System;   
  2. using System.Data;   
  3. using System.Configuration;   
  4. using System.Web;   
  5. using System.Web.Security;   
  6. using System.Web.UI;   
  7. using System.Web.UI.WebControls;   
  8. using System.Web.UI.WebControls.WebParts;   
  9. using System.Web.UI.HtmlControls;   
  10. using System.Drawing;   
  11.   
  12. public partial class _Default : System.Web.UI.Page   
  13. {   
  14.     protected void Page_Load(object sender, EventArgs e)   
  15.     {   
  16.   
  17.     }   
  18.     protected void Button1_Click(object sender, EventArgs e)   
  19.     {   
  20.         string filename = FileUpload1.FileName;   
  21.         string nowpath = Server.MapPath(".") + "\\";   
  22.         filename = nowpath + filename;   
  23.   
  24.         //儲存原圖   
  25.         FileUpload1.SaveAs(filename);   
  26.   
  27.         System.Drawing.Image image, newimage, syimage;   
  28.         System.Drawing.Image.GetThumbnailImageAbort callb = null;   
  29.         image = System.Drawing.Image.FromFile(filename);   
  30.         syimage = System.Drawing.Image.FromFile(Server.MapPath(".") + "\\" + "縮圖.gif");//要目錄下放一個"縮圖.gif"檔案,可以從網上下載:http://www.baidu.com/img/baidu.gif   
  31.   
  32.         //儲存縮圖   
  33.         newimage = image.GetThumbnailImage(100, 100, callb, new IntPtr());   
  34.         newimage.Save(filename + ".縮圖.png");   
  35.         newimage.Dispose();   
  36.   
  37.         //處理原圖片   
  38.         Graphics g = Graphics.FromImage(image);   
  39.         Font f = new Font("隸書", 16);   
  40.         Brush b = new SolidBrush(ColorTranslator.FromHtml("#FF0000"));   
  41.         string addText = "文字浮水印內容";   
  42.         g.DrawString(addText, f, b, 10, 10);   
  43.         g.DrawImageUnscaled(syimage, 50, 50);   
  44.         //g.DrawImage(newimage,50,50,100,100);   
  45.         g.Dispose();   
  46.   
  47.         //產生浮水印圖   
  48.         image.Save(filename + ".浮水印.png");   
  49.   
  50.         image.Dispose();   
  51.         syimage.Dispose();   
  52.   
  53.         Image1.ImageUrl = FileUpload1.FileName;   
  54.         Image2.ImageUrl = FileUpload1.FileName + ".縮圖.png";   
  55.         Image3.ImageUrl = FileUpload1.FileName + ".浮水印.png";   
  56.   
  57.     }   

聯繫我們

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