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

时间:2021-05-28

复制代码 代码如下:
<%@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>
<!--Insertcontenthere-->
</form>
</body>
</html>

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章