C#圖片上傳,加浮水印,自動產生縮圖原始碼-1

來源:互聯網
上載者:User

超強C#圖片上傳,加浮水印,自動產生縮圖原始碼。可以指定浮水印位置、文字;可以設定縮圖背景顏色,邊框及其顏色,陰影及其顏色;可以指定縮圖品質;支援GIF,JPEG,JPG,PNG,BMP,TIFF等多種格式圖片檔案。

<%@ Page Language="C#" AutoEventWireup="true" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Net" %>
<%@ Import NameSpace="System.Web" %>
<%@ Import NameSpace="Legalsoft.Images" %>
<script language="C#" runat="server">

protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
foreach (string f in Request.Files.AllKeys)
{
// 尋找是否存在指定的目,如果允許建立目錄,則建立之。
string f_path = TextBox1.Text;
if (!System.IO.Directory.Exists(Server.MapPath(f_path)))
{
if (CheckBox1.Checked)
{
System.IO.Directory.CreateDirectory(Server.MapPath(f_path));
}
else
{
return;
}
}
// 擷取提交的檔案名稱,包含的全部的目錄資訊;需要提取其檔案名稱(名字+尾碼)
HttpPostedFile file = Request.Files[f];
string fname = file.FileName.ToLower();
fname = fname.Replace("\\", "/");
int lsub = fname.LastIndexOf("/");
if (lsub >= 0)
{
fname = fname.Substring(lsub + 1, fname.Length - lsub - 1);
string[] fa_ext = fname.Split(".");
string f_pre = fa_ext[fa_ext.Length - 2];
string f_ext = fa_ext[fa_ext.Length - 1];
if (f_ext == "gif" || f_ext == "jpg" || f_ext == "jpeg")
{
file.SaveAs(Server.MapPath(f_path) + "/" + f_pre + "." + f_ext);

XImage img = new XImage();
// 添加浮水印
if (TextBox2.Text.Trim() != "")
{
img.markPosition = DropDownList5.Items[DropDownList5.SelectedIndex].Text;
img.Mark(f_path + f_pre + "." + f_ext, TextBox2.Text);
}

// 如果需要產生所略圖
if (CheckBox2.Checked)
{
int tWidth = Int32.Parse(TextBox3.Text);
int tHeight = Int32.Parse(TextBox4.Text);
img.tBackground = img.ToColor(DropDownList1.Items[DropDownList1.SelectedIndex].Text);
img.tBorder = img.ToColor(DropDownList2.Items[DropDownList2.SelectedIndex].Text);
img.tShadow = img.ToColor(DropDownList3.Items[DropDownList3.SelectedIndex].Text);
img.tQuality = img.ToQuality(DropDownList4.Items[DropDownList4.SelectedIndex].Text);
img.ToThumbnail(f_path + f_pre + "." + f_ext, f_path + f_pre + "_thumb." + f_ext, tWidth, tHeight);
}
}
}
}
}
}

</script>

<!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 id="Head1" runat="server">
<title>C#圖片上傳及產生縮圖,添加浮水印的代碼</title>
<meta name="keywords" content="C#檔案上傳的執行個體代碼"/>
<meta name="description" content="線上式代碼自動產生器SharpOnline只需要資料庫Table定義就自動產生網站的全套C#原始碼與頁面(包括資料查詢於操作、輸入頁面、編輯頁面及其後台頁面)。" />
<meta http-equiv="content-type" content="text/html; charset=gb2312"/>
<meta http-equiv="content-language" content="zh-cn"/>
<link href="/favicon.ico" type="image/x-icon" rel="shortcut icon"/>
<link href="/favicon.ico" type="image/x-icon" rel="bookmark"/>
<meta http-equiv="imagetoolbar" content="no"/>
<meta name="distribution" content="global"/>
<link href="/common/style.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<form id="form1" runat="server">
<center>

<div style="width:938px;background-color:#FFFFFF;">

<table width="938" style="background-color:#F0F0FF;padding:8px;line-height:24px;text-align:left;" cellpadding="0" cellspacing="0">
<tr>
<td align="right">儲存的目錄:</td>
<td>
<asp:TextBox ID="TextBox1" runat="server">/UploadedFiles/</asp:TextBox>
<asp:CheckBox ID="CheckBox1" runat="server" Checked="true" />自動建立目錄
</td>
</tr>
<tr>
<td align="right">添加浮水印字:</td>
<td>
文字:<asp:TextBox ID="TextBox2" runat="server">50018.COM</asp:TextBox> (可以為空白,則不加浮水印)<br />
位置:
<asp:DropDownList ID="DropDownList5" runat="server">
<asp:ListItem Selected="True">左下角</asp:ListItem>
<asp:ListItem>左上方</asp:ListItem>
<asp:ListItem>右下角</asp:ListItem>
<asp:ListItem>右上方</asp:ListItem>
<asp:ListItem>圖片中間</asp:ListItem>
</asp:DropDownList><br />
</td>
</tr>
<tr>
<td align="right">產生縮圖:</td>
<td>
<asp:CheckBox ID="CheckBox2" Checked="true" runat="server" />產生縮圖<br />
寬度: <asp:TextBox ID="TextBox3" runat="server">100</asp:TextBox><br />
高度: <asp:TextBox ID="TextBox4" runat="server">75</asp:TextBox><br />
背景:
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem Selected="True">無色</asp:ListItem>
<asp:ListItem>白色</asp:ListItem>
<asp:ListItem>紅色</asp:ListItem>
<asp:ListItem>藍色</asp:ListItem>
<asp:ListItem>黃色</asp:ListItem>
<asp:ListItem>黑色</asp:ListItem>
<asp:ListItem>綠色</asp:ListItem>
<asp:ListItem>紫色</asp:ListItem>
<asp:ListItem>灰色</asp:ListItem>
</asp:DropDownList><br />
邊框:
<asp:DropDownList ID="DropDownList2" runat="server">
<asp:ListItem Selected="True">無色</asp:ListItem>
<asp:ListItem>白色</asp:ListItem>
<asp:ListItem>紅色</asp:ListItem>
<asp:ListItem>藍色</asp:ListItem>
<asp:ListItem>黃色</asp:ListItem>
<asp:ListItem>黑色</asp:ListItem>
<asp:ListItem>綠色</asp:ListItem>
<asp:ListItem>紫色</asp:ListItem>
<asp:ListItem>灰色</asp:ListItem>
</asp:DropDownList><br />
陰影:
<asp:DropDownList ID="DropDownList3" runat="server">
<asp:ListItem Selected="True">無色</asp:ListItem>
<asp:ListItem>白色</asp:ListItem>
<asp:ListItem>紅色</asp:ListItem>
<asp:ListItem>藍色</asp:ListItem>
<asp:ListItem>黃色</asp:ListItem>
<asp:ListItem>黑色</asp:ListItem>
<asp:ListItem>綠色</asp:ListItem>
<asp:ListItem>紫色</asp:ListItem>
<asp:ListItem>灰色</asp:ListItem>
</asp:DropDownList><br />
品質:
<asp:DropDownList ID="DropDownList4" runat="server">
<asp:ListItem Selected="True">100%</asp:ListItem>
<asp:ListItem>90%</asp:ListItem>
<asp:ListItem>80%</asp:ListItem>
<asp:ListItem>60%</asp:ListItem>
<asp:ListItem>50%</asp:ListItem>
<asp:ListItem>40%</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>
<td align="right">請選擇檔案:</td>
<td><asp:FileUpload ID="FileUpload1" runat="server" /></td>
</tr>
<tr>
<td align="right"></td>
<td><asp:Button ID="Button2" runat="server" Text="提交" Width="88px" /></td>
</tr>
</table>

</div>

</center>
</form>
</body>
</html>

聯繫我們

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