1 2 3 4 5 6 7 8 9 10 | protected void Page_Load( object sender, EventArgs e) { if (!IsPostBack) { string path = Server.MapPath( @"/Content/images/bg/index_01.jpg" ); AddToImg(path, "图片测试pictureBox在图片上绘制文本_百度知道pictureBox在图片上绘制文本_百度知道" , Guid.NewGuid().ToString( "N" )); } } |
using System.IO;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
绘制图片和文字
/// <summary> /// 指定图片添加指定文字 /// </summary> /// <param name="fileName">指定文件路径</param> /// <param name="text">添加的文字</param> /// <param name="picname">生成文件名</param> private void AddToImg(string file, string text, string picname) { //判断指定图片是否存在 //if (!File.Exists(MapPath(fileName))) //{ // throw new FileNotFoundException("The file don't exist!"); //} if (text == string.Empty) { return; } System.Drawing.Image image = System.Drawing.Image.FromFile(file); Bitmap bitmap = new Bitmap(image, image.Width, image.Height); Graphics g = Graphics.FromImage(bitmap); float fontSize = 40.0f; //字体大小 float textWidth = text.Length * fontSize; //文本的长度 //下面定义一个矩形区域,以后在这个矩形里画上白底黑字 float rectX = 120; float rectY = 200; float rectWidth = 200; // text.Length * (fontSize + 40); float rectHeight = fontSize + 40; //声明矩形域 RectangleF textArea = new RectangleF(rectX, rectY, 270, 270); Font font = new Font("华文隶书", fontSize, FontStyle.Bold); //定义字体 Font font2 = new Font("楷体", fontSize, FontStyle.Bold); //定义字体 //font.Bold = true; Brush whiteBrush = new SolidBrush(Color.DodgerBlue); //白笔刷,画文字用 //Brush blackBrush = new SolidBrush(Color.Black); //黑笔刷,画背景用 //g.FillRectangle(blackBrush, rectX, rectY, rectWidth, rectHeight); g.DrawString(text, font, whiteBrush, textArea, StringFormat.GenericDefault); g.DrawString(text, font, whiteBrush, new RectangleF(rectX, image.Height/2, 270, 270)); g.DrawString("168元", font2, new SolidBrush(Color.Firebrick), new RectangleF(rectX, image.Height - 150, rectWidth, rectHeight)); ////------------------- 绘制高质量 ------------------------------------------- //设置 System.Drawing.Graphics对象的SmoothingMode属性为HighQuality g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; //下面这个也设成高质量 g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; //下面这个设成High g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //画专属推广二维码 System.Drawing.Image qrCodeImage = System.Drawing.Image.FromFile(Server.MapPath(@"/Content/images/money-cards.png")); g.DrawImage(qrCodeImage, new Rectangle(image.Width - qrCodeImage.Width - 200, image.Height - qrCodeImage.Height - 200, qrCodeImage.Width, qrCodeImage.Height), 0, 0, qrCodeImage.Width, qrCodeImage.Height, GraphicsUnit.Pixel); //画微信昵称 g.DrawString("小马快跑", font, new SolidBrush(Color.Red), new Rectangle(image.Width - qrCodeImage.Width - 200, image.Height - qrCodeImage.Height - 300, qrCodeImage.Width + 100, 50)); MemoryStream ms = new MemoryStream(); //输出方法一:将文件生成并保存到C盘 string path = "D:\\test\\" + picname + ".png"; bitmap.Save(path, System.Drawing.Imaging.ImageFormat.Jpeg); //输出方法二,显示在网页中,保存为Jpg类型 //bitmap.Save(ms, ImageFormat.Jpeg); //Response.Clear(); //Response.ContentType = "image/jpeg"; //Response.BinaryWrite(ms.ToArray()); g.Dispose(); bitmap.Dispose(); image.Dispose(); }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!