请教 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

}

using System.Drawing

using System.Drawing.Drawing2D

using System.Drawing.Imaging

public class ImageHelper

{

/// <summary><SUMMARY></SUMMARY>

/// 获取图片中的各帧

/// </summary>

/// <param name="pPath"><PARAM name="pPath" />图片路径</param>

/// <param name="pSavePath"><PARAM name="pSavePath" />保存路径</param>

public void GetFrames(string pPath, string pSavedPath)

{

Image gif = Image.FromFile(pPath)

FrameDimension fd = new FrameDimension(gif.FrameDimensionsList[0])

//获取帧数(gif图片可能包含多帧,其它格式图片一般仅一帧)

int count = gif.GetFrameCount(fd)

//以Jpeg格式保存各帧

for (int i = 0i <counti++)

{

gif.SelectActiveFrame(fd, i)

gif.Save(pSavedPath + "\\frame_" + i + ".jpg", ImageFormat.Jpeg)

}

}

/// <summary><SUMMARY></SUMMARY>

/// 获取图片缩略图

/// </summary>

/// <param name="pPath"><PARAM name="pPath" />图片路径</param>

/// <param name="pSavePath"><PARAM name="pSavePath" />保存路径</param>

/// <param name="pWidth"><PARAM name="pWidth" />缩略图宽度</param>

/// <param name="pHeight"><PARAM name="pHeight" />缩略图高度</param>

/// <param name="pFormat"><PARAM name="pFormat" />保存格式,通常可以是jpeg</param>

public void GetSmaller(string pPath, string pSavedPath, int pWidth, int pHeight)

{

try

{

Image smallerImg

Image originalImg = Image.FromFile(pPath)

Image.GetThumbnailImageAbort callback = new Image.GetThumbnailImageAbort(ThumbnailCallback)

smallerImg = originalImg.GetThumbnailImage(pWidth, pHeight, callback, IntPtr.Zero)

smallerImg.Save(pSavedPath + "\\smaller.jpg", ImageFormat.Jpeg)

}

catch (Exception x)

{

//

}

}

/// <summary><SUMMARY></SUMMARY>

/// 获取图片指定部分

/// </summary>

/// <param name="pPath"><PARAM name="pPath" />图片路径</param>

/// <param name="pSavePath"><PARAM name="pSavePath" />保存路径</param>

/// <param name="pPartStartPointX"><PARAM name="pPartStartPointX" />目标图片开始绘制处的坐标X值(通常为)</param>

/// <param name="pPartStartPointY"><PARAM name="pPartStartPointY" />目标图片开始绘制处的坐标Y值(通常为)</param>

/// <param name="pPartWidth"><PARAM name="pPartWidth" />目标图片的宽度</param>

/// <param name="pPartHeight"><PARAM name="pPartHeight" />目标图片的高度</param>

/// <param name="pOrigStartPointX"><PARAM name="pOrigStartPointX" />原始图片开始截取处的坐标X值</param>

/// <param name="pOrigStartPointY"><PARAM name="pOrigStartPointY" />原始图片开始截取处的坐标Y值</param>

/// <param name="pFormat"><PARAM name="pFormat" />保存格式,通常可以是jpeg</param>

public void GetPart(string pPath, string pSavedPath, int pPartStartPointX, int pPartStartPointY, int pPartWidth, int pPartHeight, int pOrigStartPointX, int pOrigStartPointY)

{

Image originalImg = Image.FromFile(pPath)

Bitmap partImg = new Bitmap(pPartWidth, pPartHeight)

Graphics graphics = Graphics.FromImage(partImg)

Rectangle destRect = new Rectangle(new Point(pPartStartPointX, pPartStartPointY), new Size(pPartWidth, pPartHeight))//目标位置

Rectangle origRect = new Rectangle(new Point(pOrigStartPointX, pOrigStartPointY), new Size(pPartWidth, pPartHeight))//原图位置(默认从原图中截取的图片大小等于目标图片的大小)

graphics.DrawImage(originalImg, destRect, origRect, GraphicsUnit.Pixel)

partImg.Save(pSavedPath + "\\part.jpg", ImageFormat.Jpeg)

}

public bool ThumbnailCallback()

{

return false

}

}

希望对你有所帮助

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://outofmemory.cn/bake/11915582.html

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

发表评论

登录后才能评论

评论列表(0条)

保存