C#图片上传,加水印,自动生成缩略图源代码-1

超强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>

posted @ 2009-04-13 11:53  陈星  阅读(644)  评论(0编辑  收藏  举报