当前位置: 移动技术网 > IT编程>开发语言>.net > ASP.Net 上传图片并生成高清晰缩略图

ASP.Net 上传图片并生成高清晰缩略图

2018年02月26日  | 移动技术网IT编程  | 我要评论

昆山二手房市场,3D预测彩酷酷caikuku,蒋洁敏最新消息

<%@ page language="c#" autoeventwireup="true" codefile="default2.aspx.cs" inherits="default2" %>
<!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>
<input id="file1" runat="server" type="file" /></div><asp:button id="button1" runat="server" onclick="button1_click" text="button" />
</form>
</body>
</html>
protected void button1_click(object sender, eventargs e)
{
string a = this.uploadimage(this.file1, "upload/", "thumb_", 118, 118);
}
/// <summary>
/// asp.net上传图片并生成缩略图
/// </summary>
/// <param name="upimage">htmlinputfile控件</param>
/// <param name="ssavepath">保存的路径,些为相对服务器路径的下的文件夹</param>
/// <param name="sthumbextension">缩略图的thumb</param>
/// <param name="intthumbwidth">生成缩略图的宽度</param>
/// <param name="intthumbheight">生成缩略图的高度</param>
/// <returns>缩略图名称</returns>
public string uploadimage(htmlinputfile upimage, string ssavepath, string sthumbextension, int intthumbwidth, int intthumbheight)
{
string sthumbfile = "";
string sfilename = "";
if (upimage.postedfile != null)
{
httppostedfile myfile = upimage.postedfile;
int nfilelen = myfile.contentlength;
if (nfilelen == 0)
return "没有选择上传图片";
//获取upimage选择文件的扩展名
string extendname = system.io.path.getextension(myfile.filename).tolower();
//判断是否为图片格式
if (extendname != ".jpg" && extendname != ".jpge" && extendname != ".gif" && extendname != ".bmp" && extendname != ".png")
return "图片格式不正确";
byte[] mydata = new byte[nfilelen];
myfile.inputstream.read(mydata, 0, nfilelen);
sfilename = system.io.path.getfilename(myfile.filename);
int file_append = 0;
//检查当前文件夹下是否有同名图片,有则在文件名+1
while (system.io.file.exists(system.web.httpcontext.current.server.mappath(ssavepath + sfilename)))
{
file_append++;
sfilename = system.io.path.getfilenamewithoutextension(myfile.filename)
+ file_append.tostring() + extendname;
}
system.io.filestream newfile
= new system.io.filestream(system.web.httpcontext.current.server.mappath(ssavepath + sfilename),
system.io.filemode.create, system.io.fileaccess.write);
newfile.write(mydata, 0, mydata.length);
newfile.close();
//以上为上传原图
try
{
//原图加载
using (system.drawing.image sourceimage = system.drawing.image.fromfile(system.web.httpcontext.current.server.mappath(ssavepath + sfilename)))
{
//原图宽度和高度
int width = sourceimage.width;
int height = sourceimage.height;
int smallwidth;
int smallheight;
//获取第一张绘制图的大小,(比较 原图的宽/缩略图的宽 和 原图的高/缩略图的高)
if (((decimal)width) / height <= ((decimal)intthumbwidth) / intthumbheight)
{
smallwidth = intthumbwidth;
smallheight = intthumbwidth * height / width;
}
else
{
smallwidth = intthumbheight * width / height;
smallheight = intthumbheight;
}
//判断缩略图在当前文件夹下是否同名称文件存在
file_append = 0;
sthumbfile = sthumbextension + system.io.path.getfilenamewithoutextension(myfile.filename) + extendname;
while (system.io.file.exists(system.web.httpcontext.current.server.mappath(ssavepath + sthumbfile)))
{
file_append++;
sthumbfile = sthumbextension + system.io.path.getfilenamewithoutextension(myfile.filename) +
file_append.tostring() + extendname;
}
//缩略图保存的绝对路径
string smallimagepath = system.web.httpcontext.current.server.mappath(ssavepath) + sthumbfile;
//新建一个图板,以最小等比例压缩大小绘制原图
using (system.drawing.image bitmap = new system.drawing.bitmap(smallwidth, smallheight))
{
//绘制中间图
using (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(color.black);
g.drawimage(
sourceimage,
new system.drawing.rectangle(0, 0, smallwidth, smallheight),
new system.drawing.rectangle(0, 0, width, height),
system.drawing.graphicsunit.pixel
);
}
//新建一个图板,以缩略图大小绘制中间图
using (system.drawing.image bitmap1 = new system.drawing.bitmap(intthumbwidth, intthumbheight))
{
//绘制缩略图
using (system.drawing.graphics g = system.drawing.graphics.fromimage(bitmap1))
{
//高清,平滑
g.interpolationmode = system.drawing.drawing2d.interpolationmode.high;
g.smoothingmode = system.drawing.drawing2d.smoothingmode.highquality;
g.clear(color.black);
int lwidth = (smallwidth - intthumbwidth) / 2;
int bheight = (smallheight - intthumbheight) / 2;
g.drawimage(bitmap, new rectangle(0, 0, intthumbwidth, intthumbheight), lwidth, bheight, intthumbwidth, intthumbheight, graphicsunit.pixel);
g.dispose();
bitmap1.save(smallimagepath, system.drawing.imaging.imageformat.jpeg);
}
}
}
}
}
catch
{
//出错则删除
system.io.file.delete(system.web.httpcontext.current.server.mappath(ssavepath + sfilename));
return "图片格式不正确";
}
//返回缩略图名称
return sthumbfile;
}
return "没有选择图片";
}

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网