asp.net上传图片并作处理水印与缩略图的实例代码
方法类:
upfileclass.cs
using system;
using system.data;
using system.configuration;
using system.web;
using system.web.security;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.webcontrols.webparts;
using system.web.ui.htmlcontrols;
/// <summary>
/// upfileclass 的摘要说明
/// </summary>
public class upfileclass
{
public upfileclass()
{
//
// todo: 在此处添加构造函数逻辑
//
}
/// <summary>
/// 生成缩略图
/// </summary>
/// <param name="originalimagepath">源图路径(物理路径)</param>
/// <param name="thumbnailpath">缩略图路径(物理路径)</param>
/// <param name="width">缩略图宽度</param>
/// <param name="height">缩略图高度</param>
/// <param name="mode">生成缩略图的方式</param>
public static void makethumbnail(string originalimagepath, string thumbnailpath, int width, int height, string mode)
{
system.drawing.image originalimage = system.drawing.image.fromfile(originalimagepath);
int towidth = width;
int toheight = height;
int x = 0;
int y = 0;
int ow = originalimage.width;
int oh = originalimage.height;
switch (mode)
{
case "hw"://指定高宽缩放(可能变形)
break;
case "w"://指定宽,高按比例
toheight = originalimage.height * width / originalimage.width;
break;
case "h"://指定高,宽按比例
towidth = originalimage.width * height / originalimage.height;
break;
case "cut"://指定高宽裁减(不变形)
if ((double)originalimage.width / (double)originalimage.height > (double)towidth / (double)toheight)
{
oh = originalimage.height;
ow = originalimage.height * towidth / toheight;
y = 0;
x = (originalimage.width - ow) / 2;
}
else
{
ow = originalimage.width;
oh = originalimage.width * height / towidth;
x = 0;
y = (originalimage.height - oh) / 2;
}
break;
default:
break;
}
//新建一个bmp图片
system.drawing.image bitmap = new system.drawing.bitmap(towidth, toheight);
//新建一个画板
system.drawing.graphics g = system.drawing.graphics.fromimage(bitmap);
//设置高质量插值法
g.interpolationmode = system.drawing.drawing2d.interpolationmode.high;
//设置高质量,低速度呈现平滑程度
g.smoothingmode = system.drawing.drawing2d.smoothingmode.highquality;
//清空画布并以透明背景色填充
g.clear(system.drawing.color.transparent);
//在指定位置并且按指定大小绘制原图片的指定部分
g.drawimage(originalimage, new system.drawing.rectangle(0, 0, towidth, toheight),
new system.drawing.rectangle(x, y, ow, oh),
system.drawing.graphicsunit.pixel);
try
{
//以jpg格式保存缩略图
bitmap.save(thumbnailpath, system.drawing.imaging.imageformat.jpeg);
}
catch (system.exception e)
{
throw e;
}
finally
{
originalimage.dispose();
bitmap.dispose();
g.dispose();
}
}
/// <summary>
/// 在图片上增加文字水印
/// </summary>
/// <param name="path">原服务器图片路径</param>
/// <param name="path_sy">生成的带文字水印的图片路径</param>
public static void addshuiyinword(string path, string path_sy)
{
string addtext = "爱智旮旯";
system.drawing.image image = system.drawing.image.fromfile(path);
system.drawing.graphics g = system.drawing.graphics.fromimage(image);
g.drawimage(image, 0, 0, image.width, image.height);
system.drawing.font f = new system.drawing.font("verdana", 16);
system.drawing.brush b = new system.drawing.solidbrush(system.drawing.color.blue);
g.drawstring(addtext, f, b, 15, 15);
g.dispose();
image.save(path_sy);
image.dispose();
}
/// <summary>
/// 在图片上生成图片水印
/// </summary>
/// <param name="path">原服务器图片路径</param>
/// <param name="path_syp">生成的带图片水印的图片路径</param>
/// <param name="path_sypf">水印图片路径</param>
public static void addshuiyinpic(string path, string path_syp, string path_sypf)
{
system.drawing.image image = system.drawing.image.fromfile(path);
system.drawing.image copyimage = system.drawing.image.fromfile(path_sypf);
system.drawing.graphics g = system.drawing.graphics.fromimage(image);
g.drawimage(copyimage, new system.drawing.rectangle(image.width - copyimage.width, image.height - copyimage.height, copyimage.width, copyimage.height), 0, 0, copyimage.width, copyimage.height, system.drawing.graphicsunit.pixel);
g.dispose();
image.save(path_syp);
image.dispose();
}
}
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">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>asp.net 上传图片并作处理 水印 缩略图</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:fileupload id="fileupload1" runat="server" bordercolor="gray" borderwidth="1px" />
<asp:button id="button1" runat="server" onclick="button1_click" text="上传" bordercolor="gray" borderwidth="1px" width="70px" /><br />
<asp:label id="label1" runat="server"></asp:label>
</div>
</form>
</body>
</html>
default.aspx.cs代码:
using system;
using system.data;
using system.configuration;
using system.web;
using system.web.security;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.webcontrols.webparts;
using system.web.ui.htmlcontrols;
using system.io;
public partial class _default : system.web.ui.page
{
protected void page_load(object sender, eventargs e)
{
}
protected void button1_click(object sender, eventargs e)
{
if (fileupload1.hasfile)
{
string filecontenttype = fileupload1.postedfile.contenttype;
if (filecontenttype == "image/bmp" || filecontenttype == "image/gif" || filecontenttype == "image/pjpeg")
{
string name = fileupload1.postedfile.filename; // 客户端文件路径
fileinfo file = new fileinfo(name);
string filename = file.name; // 文件名称
string filename_s = "s_" + file.name; // 缩略图文件名称
string filename_sy = "sy_" + file.name; // 水印图文件名称(文字)
string filename_syp = "syp_" + file.name; // 水印图文件名称(图片)
//以下路径可以根据情况进行修改
string webfilepath = server.mappath("file/" + filename); // 服务器端文件路径
string webfilepath_s = server.mappath("file/" + filename_s); // 服务器端缩略图路径
string webfilepath_sy = server.mappath("file/" + filename_sy); // 服务器端带水印图路径(文字)
string webfilepath_syp = server.mappath("file/" + filename_syp); // 服务器端带水印图路径(图片)
//以下为水印图片的路径一定要先准备一张水印图片!
string webfilepath_sypf = server.mappath("file/shuiyin.jpg"); // 服务器端水印图路径(图片)
if (!file.exists(webfilepath))
{
try
{
fileupload1.saveas(webfilepath); // 使用 saveas 方法保存文件
upfileclass.addshuiyinword(webfilepath, webfilepath_sy);
upfileclass.addshuiyinpic(webfilepath, webfilepath_syp, webfilepath_sypf);
upfileclass.makethumbnail(webfilepath, webfilepath_s, 130, 130, "cut"); // 生成缩略图方法
label1.text = "提示:文件“" + filename + "”成功上传,并生成“" + filename_s + "”缩略图,文件类型为:" + fileupload1.postedfile.contenttype + ",文件大小为:" + fileupload1.postedfile.contentlength + "b";
}
catch (exception ex)
{
label1.text = "提示:文件上传失败,失败原因:" + ex.message;
}
}
else
{
label1.text = "提示:文件已经存在,请重命名后上传";
}
}
else
{
label1.text = "提示:文件类型不符";
}
}
}
}