您的位置:首页 > 编程语言 > ASP

Asp.net上传图片同时生成文字水印图,图片水印图,缩略图

2012-01-21 06:59 831 查看
.aspx

<body>

<form id="form1" runat="server">

<div>

<asp:FileUpload ID="FileUpload1" runat="server" /><br />

<asp:Button ID="Button1" runat="server" Text="上传图片" onclick="Button1_Click" />

</div>

</form>

</body>



.cs

protected void Button1_Click(object sender, EventArgs e)

{

string fileContentType = this.FileUpload1.PostedFile.ContentType;

if (this.FileUpload1.HasFile)

{

if (fileContentType == "image/bmp" || fileContentType == "image/gif" ||

fileContentType == "image/pjpeg")

{

// 文件名,如1.jpeg

string filename = this.FileUpload1.FileName;



// 控件里的源路径,如C://1.jpeg

string fullfilename = this.FileUpload1.PostedFile.FileName;



// 上传至服务器的图片路径

string webfilename = Server.MapPath("image/" + filename);

// 上传至服务器的文字水印图片路径

string textwaterwebfilename = Server.MapPath("textwater/" + filename);

// 上传至服务器的图片水印图片路径

string picwaterfilename = Server.MapPath("picwater/" + filename);

// 水印图片

string waterpic = Server.MapPath("picwater/waterpic.png");

// 缩略图

string thumbnailfilename = Server.MapPath("thumbnailpic/" + filename);

// 检验文件尺寸

if (IsPecificSize(fullfilename, 150, 200))

{

if (!File.Exists(webfilename))

{

this.FileUpload1.SaveAs(webfilename);

SaveTextWaterImage(webfilename, textwaterwebfilename); // 给图片添

加文字水印

SavePicWaterImage(webfilename, picwaterfilename, waterpic);// 给图片添


加图片水印

GetThumbnailPic(webfilename, thumbnailfilename, 80, 80);

}

else

{

Response.Write("<script>alert('图片已经存在')</script>");

}

}

else

{

Response.Write("<script>alert('文件大小不符,必须是50*50的大小')

</script>");

}



}

else

{

Response.Write("<script>alert('文件类型不符')</script>");

}

}

}

/// <summary>

/// 是否符合规定大小

/// </summary>

/// <param name="path">源图片路径</param>

/// <param name="width">宽,单位为像素</param>

/// <param name="height">高,单位为像素</param>

/// <returns></returns>

private bool IsPecificSize(string path, int width, int height)

{

System.Drawing.Image image = System.Drawing.Image.FromFile(path);

if (image.Width == width && image.Height == height)

return true;

else

return false;

}

/// <summary>

/// 在图片上增加文字水印

/// </summary>

/// <param name="path">原服务器图片路径</param>

/// <param name="newpath">生成的带文字水印的图片路径</param>

private void SaveTextWaterImage(string path, string newpath)

{

string addText = "xuyue";

System.Drawing.Image image = System.Drawing.Image.FromFile(path); // 从指定的文件

创建
System.Drawing.Image

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", 15);

System.Drawing.Brush b = new System.Drawing.SolidBrush(System.Drawing.Color.Green);

g.DrawString(addText, f, b,70,170);

g.Dispose();

image.Save(newpath);

image.Dispose();

}

/// <summary>

/// 在图片上生成图片水印

/// </summary>

/// <param name="path">源图片路径</param>

/// <param name="newpath">新图片路径</param>

/// <param name="waterpicpath">水印图片路径</param>

private void SavePicWaterImage(string path, string newpath, string waterpicpath)

{

System.Drawing.Image image = System.Drawing.Image.FromFile(path);

System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image);

System.Drawing.Image waterimage = System.Drawing.Image.FromFile(waterpicpath);

g.DrawImage(waterimage, new System.Drawing.Rectangle

(image.Width - waterimage.Width, image.Height - waterimage.Height,

waterimage.Width, waterimage.Height), 0, 0, waterimage.Width,

waterimage.Height, System.Drawing.GraphicsUnit.Pixel);

g.Dispose();

image.Save(newpath);

image.Dispose();

}

/// <summary>

/// 获得缩略图

/// </summary>

/// <param name="originalpicpath">源图路径</param>

/// <param name="smallpicpath">缩略成小图后的新地址</param>

/// <param name="smallwidth">小图的宽度</param>

/// <param name="smallheight">小图的高度</param>

private void GetThumbnailPic(string originalpicpath,string smallpicpath,int

smallwidth,int smallheight )

{

// 定义变量

System.Drawing.Image.GetThumbnailImageAbort callb = null;

System.Drawing.Image imageSmall, imageBig;

// 加载大图片

imageBig = System.Drawing.Image.FromFile(originalpicpath);

//开始生成

imageSmall = imageBig.GetThumbnailImage(smallwidth, smallheight, callb, new


System.IntPtr());

//保存缩略图

imageSmall.Save(smallpicpath);

//释放图片对象资源

imageSmall.Dispose();

imageBig.Dispose();

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐