<script language="网页特效" type="text/javascript">
function previewimg(imgfile)
{
var newpreview = document.getelementbyid("newpreview");
newpreview.filters.item("dximagetransform.microsoft.alphaimageloader").src = imgfile.value;
newpreview.style.width = "80px";
newpreview.style.height = "60px";
}
</script>
<asp:fileupload id="fileupload1" runat="server" onchange="previewimg(this)" />
<div id="newpreview"> </div>
<script>
function $(o){return document.getelementbyid(o);}
function checkimg(o,img)
{
if (!/.((jpg)|(bmp)|(gif)|(png))$/ig.test(o.value))
{
alert('只能上传jpg,bmp,gif,png格式图片!');
o.outerhtml = o.outerhtml;
}
else
{
$(img).filters.item("dximagetransform.microsoft.alphaimageloader").src=o.value;
}
}
</script>
<asp:fileupload id="fileupload1" runat="server" onchange="checkimg(this, 'img');" />
<div id="img" style="filter:progid:dximagetransform.microsoft.alphaimageloader(src= <%= pic%>,sizingmethod=scale);width:88px;height:113px;"> </div>
public string pic="";
</body>
</html>
<%
using system;
using system.data;
using system.configuration;
using system.collections;
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 upfile_upfile : 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 方法保存文件
addshuiyinword(webfilepath, webfilepath_sy);
addshuiyinpic(webfilepath, webfilepath_syp, webfilepath_sypf);
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 = "提示:文件类型不符";
}
}
}
/// <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>
protected 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>
protected 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();
}