欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

ASP.NET上传图片并生成可带版权信息的缩略图

程序员文章站 2023-11-25 14:13:28
复制代码 代码如下:<%@ page language="c#" responseencoding="gb2312" %>...
复制代码 代码如下:

<%@ page language="c#" responseencoding="gb2312" %>
<%@ import namespace="system" %>
<%@ import namespace="system.io" %>
<%@ import namespace="system.drawing" %>
<%@ import namespace="system.drawing.imaging" %>
<script runat="server"> 
  

void page_load(object sender, eventargs e)
{
if(!page.ispostback)
{
imgpreview.visible=false;
}
}
void getthumbnailimage(int width,int height,string strinfo,int left,int right)
{
string file="uploads/"+uploadfile.postedfile.filename.substring(uploadfile.postedfile.filename.lastindexof('\\')+1);
string newfile="uploads/"+uploadfile.postedfile.filename.substring(uploadfile.postedfile.filename.lastindexof('\\')+1)+".jpg";
string stradd=strinfo;
system.drawing.image oldimage = system.drawing.image.fromfile(server.mappath(file));
system.drawing.image thumbnailimage =
oldimage.getthumbnailimage(width, height,new system.drawing.image.getthumbnailimageabort(thumbnailcallback), intptr.zero);
response.clear();
bitmap output=new bitmap(thumbnailimage);
graphics g=graphics.fromimage(output);
g.drawstring(stradd,new font("courier new", 14),new solidbrush(color.red),left,right);
output.save(server.mappath(newfile),system.drawing.imaging.imageformat.jpeg);
response.contenttype = "image/gif";
imgpreview.visible=true;
imgpreview.imageurl=newfile;
}
bool thumbnailcallback()
{
return true;
}

void button_click(object sender, eventargs e)
{
int width,height,left,right;
string straddinfo=txtaddinfo.text;
width=int32.parse(txtwidth.text);
height=int32.parse(txtheight.text);
left=int32.parse(txtleft.text);
right=int32.parse(txtright.text);
if(!(uploadfile.postedfile.contentlength>0))
{
lblerrinfo.text="没有选择文件";
}
else
{

string path = server.mappath("./uploads/"+uploadfile.postedfile.filename.substring(uploadfile.postedfile.filename.lastindexof('\\')+1));
if(file.exists(path))
{
lblerrinfo.text="已经有同名文件";
}
else
{
uploadfile.postedfile.saveas(path);
getthumbnailimage(width,height,straddinfo,left,right);
}
}
}
</script>
<html>
<head>
</head>
<body>
<form method="post" enctype="multipart/form-data" runat="server">
<p>
<input id="uploadfile" type="file" runat="server" />
<asp:label id="lblerrinfo" runat="server" forecolor="red"></asp:label>
</p>
<p>
width:<asp:textbox id="txtwidth" runat="server" width="40px">100</asp:textbox>
height:<asp:textbox id="txtheight" runat="server" width="40px">150</asp:textbox>

</p>
<p>
添加信息:<asp:textbox id="txtaddinfo" runat="server"> aspxboy.com</asp:textbox>
</p>
<p>
信息位置:left:<asp:textbox id="txtleft" runat="server" width="40px">10</asp:textbox>
right:<asp:textbox id="txtright" runat="server" width="40px">135</asp:textbox>
</p>
<p>

<input id="button" type="button" value="上传生成所略图" onserverclick="button_click" runat="server" />
</p>
<p><asp:image id="imgpreview" runat="server"></asp:image>
</p>
<!-- insert content here -->
</form>
</body>
</html>