在asp.net中上传图片,上传上去的图片加水印是怎么做的

在asp.net中上传图片,上传上去的图片加水印是怎么做的,第1张

需要引入 System.Drawing命名空间(必要的话还有System.Drawing.Imaging),调用 Graphics对象 (有静态方法可以从Image对象中创建),重新绘图就可以了,或者你的水印就是个图,图片叠加,当然也可以绘制文字了,具体Graphics的使用可以参看 MSDN。

try

{

string filepath = uploadFile.Value//上谈码传图片的路径

string newName = DateTime.Now.ToString("yyyyMMddHHmmss")//图片新名字

string filehz = filepath.Substring(filepath.LastIndexOf(".") + 1).ToLowerInvariant()//后缀名

string uploadpath = Server.MapPath("./images/update/" + newName + "." + filehz)//服务器保存图片路径

if (!(uploadFile.PostedFile.ContentLength >0))

{

lblErrInfo.Text = "没有选择文件"

}

else

{

if (filehz == "jpg" || filehz == "gif" || filehz == "png")

{

if (File.Exists(uploadpath))

{

lblErrInfo.Text = "已经有同名文件"

}

else

{

if (chboxIsInfo.Checked == true)//加版权段侍首信息

{

if (txtLeft.Text.Trim() == "" || txtRight.Text.Trim() == "" || txtAddInfo.Text.Trim() == "")

{

Response.Write("<script>alert('请输入信息位置(半角数字)和信息文本握数!')</script>")

}

else

{

uploadFile.PostedFile.SaveAs(uploadpath)

lbtnDelImage.Visible = true

btnUpdate.Enabled = false

uploadFile.Visible = false

txtGameImage.Text = "images/update/" + newName + "." + filehz

System.Threading.Thread.Sleep(1000)

System.Drawing.Image img = System.Drawing.Image.FromFile(uploadpath)

lblImgWidth.Text = img.Width.ToString()

lblImgHeight.Text = img.Height.ToString()

int width, height, left, right

width = Int32.Parse(lblImgWidth.Text)

height = Int32.Parse(lblImgHeight.Text)

left = Int32.Parse(txtLeft.Text.Trim())

right = Int32.Parse(txtRight.Text.Trim())

System.Threading.Thread.Sleep(1000)

//添加信息

string file = Server.MapPath("./" + txtGameImage.Text)

string newfile = "images/update/" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".jpg"

string strAddInfo = txtAddInfo.Text

System.Drawing.Image oldimage = System.Drawing.Image.FromFile(file)

Response.Clear()

Bitmap output = new Bitmap(oldimage)

Graphics gh = Graphics.FromImage(output)

string ColorHex = SelColor.Value

int r = Int16.Parse(ColorHex.Substring(0, 2), System.Globalization.NumberStyles.AllowHexSpecifier)

int g = Int16.Parse(ColorHex.Substring(2, 2), System.Globalization.NumberStyles.AllowHexSpecifier)

int b = Int16.Parse(ColorHex.Substring(4, 2), System.Globalization.NumberStyles.AllowHexSpecifier)

Color NewColor = Color.FromArgb(r, g, b)

gh.DrawString(strAddInfo, new Font(ddlFont.SelectedValue, int.Parse(ddlFontSize.SelectedValue)), new SolidBrush(NewColor), left, right)

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

Response.ContentType = "image/gif"

ImgPreview.ImageUrl = newfile

ImgPreview.Visible = true

oldimage.Dispose()

txtGameImage.Text = newfile

img.Dispose()

File.Delete(file)

}

}

else//不加版权信息

{

uploadFile.PostedFile.SaveAs(uploadpath)

lbtnDelImage.Visible = true

btnUpdate.Enabled = false

uploadFile.Visible = false

txtGameImage.Text = "images/update/" + newName + "." + filehz

System.Threading.Thread.Sleep(1000)

System.Drawing.Image img = System.Drawing.Image.FromFile(uploadpath)

lblImgWidth.Text = img.Width.ToString()

lblImgHeight.Text = img.Height.ToString()

Response.ContentType = "image/gif"

ImgPreview.ImageUrl = txtGameImage.Text

ImgPreview.Visible = true

img.Dispose()

}

SizeToSize()

}

}

else

{

Response.Write("<script>alert('只能上传jpg|gif|png格式的图片!')</script>")

}

}

}

catch

{

Response.Write("<script>alert('上传图片出错!')</script>")

}

以前写的上传代码

left, right分别代表版权信息的X,Y轴,这里是自己输入的,你可以通过对你输入的版权信息的字符像素长度和图片的宽,高计算得到。

ddlFont.SelectedValue 字体选择

int.Parse(ddlFontSize.SelectedValue)) 字体大小

new SolidBrush(NewColor) 字体颜色


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/11968042.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-20
下一篇 2023-05-20

发表评论

登录后才能评论

评论列表(0条)

保存