ASP.NET上传图片并生成可带版权信息的缩略图
ASP.NET上传图片并生成可带版权信息的缩略图
发布时间:2016-12-29 来源:查字典编辑
摘要:复制代码代码如下:voidPage_Load(Objectsender,EventArgse){if(!Page.IsPostBack){I...

复制代码 代码如下:

<%@PageLanguage="C#"ResponseEncoding="gb2312"%>

<%@importNamespace="System"%>

<%@importNamespace="System.IO"%>

<%@importNamespace="System.Drawing"%>

<%@importNamespace="System.Drawing.Imaging"%>

<scriptrunat="server">

voidPage_Load(Objectsender,EventArgse)

{

if(!Page.IsPostBack)

{

ImgPreview.Visible=false;

}

}

voidGetThumbnailImage(intwidth,intheight,stringstrInfo,intleft,intright)

{

stringfile="Uploads/"+uploadFile.PostedFile.FileName.Substring(uploadFile.PostedFile.FileName.LastIndexOf('')+1);

stringnewfile="Uploads/"+uploadFile.PostedFile.FileName.Substring(uploadFile.PostedFile.FileName.LastIndexOf('')+1)+".jpg";

stringstrAdd=strInfo;

System.Drawing.Imageoldimage=System.Drawing.Image.FromFile(Server.MapPath(file));

System.Drawing.ImagethumbnailImage=

oldimage.GetThumbnailImage(width,height,newSystem.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback),IntPtr.Zero);

Response.Clear();

Bitmapoutput=newBitmap(thumbnailImage);

Graphicsg=Graphics.FromImage(output);

g.DrawString(strAdd,newFont("CourierNew",14),newSolidBrush(Color.Red),left,right);

output.Save(Server.MapPath(newfile),System.Drawing.Imaging.ImageFormat.Jpeg);

Response.ContentType="image/gif";

ImgPreview.Visible=true;

ImgPreview.ImageUrl=newfile;

}

boolThumbnailCallback()

{

returntrue;

}

voidButton_Click(objectsender,EventArgse)

{

intwidth,height,left,right;

stringstrAddInfo=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

{

stringpath=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>

<formmethod="post"enctype="multipart/form-data"runat="server">

<p>

<inputid="uploadFile"type="file"runat="server"/>

<asp:Labelid="lblErrInfo"runat="server"forecolor="Red"></asp:Label>

</p>

<p>

width:<asp:TextBoxid="txtWidth"runat="server"Width="40px">100</asp:TextBox>

height:<asp:TextBoxid="txtHeight"runat="server"Width="40px">150</asp:TextBox>

</p>

<p>

添加信息:<asp:TextBoxid="txtAddInfo"runat="server">AspxBoy.Com</asp:TextBox>

</p>

<p>

信息位置:left:<asp:TextBoxid="txtLeft"runat="server"Width="40px">10</asp:TextBox>

right:<asp:TextBoxid="txtRight"runat="server"Width="40px">135</asp:TextBox>

</p>

<p>

<inputid="button"type="button"value="上传生成所略图"onServerClick="Button_Click"runat="server"/>

</p>

<p><asp:Imageid="ImgPreview"runat="server"></asp:Image>

</p>

<>

</form>

</body>

</html>

推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
相关阅读
网友关注
最新asp.net教程学习
热门asp.net教程学习
编程开发子分类