请教 C# 大神 asp.net 实现 将 在指定 图片下 添加 文字。要求原图大小不变,尺寸 高度增加 一定的高度,

请教 C# 大神 asp.net 实现 将 在指定 图片下 添加 文字。要求原图大小不变,尺寸 高度增加 一定的高度,,第1张

public Image DrawText(Image original,string text,Font font,Color color,int height)

{

Image result=new Bitmap(original.Width,original.Height+height)

using(Graphics graphics=Graphics.FromImage(result))

{

graphics.DrawImage(original,0,0)

Rectangle rect=new Rectangle(0,original.Height,original.Width,height)

using(SolidBrush brush=new SolidBrush(color))

{

using(StringFormat format=new StringFormat())

{

format.Alignment=StringAlignment.Near

format.LineAlignment=StringAlignment.Center

graphics.DrawString(text,font,brush,rect,format)

}

}

}

return result

}

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) 字体颜色


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

原文地址:https://54852.com/bake/11436271.html

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

发表评论

登录后才能评论

评论列表(0条)

    保存