图片:如何使用 ASP.NET 和 C# 在图像上写入文本

一则或许对你有用的小广告

欢迎加入小哈的星球 ,你将获得:专属的项目实战 / Java 学习路线 / 一对一提问 / 学习打卡/ 赠书活动

目前,正在 星球 内带小伙伴们做第一个项目:全栈前后端分离博客项目,采用技术栈 Spring Boot + Mybatis Plus + Vue 3.x + Vite 4手把手,前端 + 后端全栈开发,从 0 到 1 讲解每个功能点开发步骤,1v1 答疑,陪伴式直到项目上线,目前已更新了 204 小节,累计 32w+ 字,讲解图:1416 张,还在持续爆肝中,后续还会上新更多项目,目标是将 Java 领域典型的项目都整上,如秒杀系统、在线商城、IM 即时通讯、权限管理等等,已有 870+ 小伙伴加入,欢迎点击围观

介绍

本文介绍了如何使用 Asp.Net 和 C# 在图像上编写文本。在这篇文章中,我使用了一些常见的库函数和方法。

图像类和位图类

  • 图像类是抽象类的示例之一
  • Bitmap 类是 Image 类的实现,它继承自 Abstract Image 类
  • 实现后我们可以在Bitmap Class中得到Image Class的功能。

C#代码

以下代码用于在图像上写文字。我们可以根据我们的要求更改以下功能。


 //creating a image object
System.Drawing.Image bitmap = (System.Drawing.Image)Bitmap.FromFile(Server.MapPath("onam.jpg")); // set image 
//draw the image object using a Graphics object
Graphics graphicsImage = Graphics.FromImage(bitmap);

//Set the alignment based on the coordinates
StringFormat stringformat = new StringFormat(); stringformat.Alignment = StringAlignment.Far; stringformat.LineAlignment = StringAlignment.Far;

StringFormat stringformat2 = new StringFormat(); stringformat2.Alignment = StringAlignment.Center; stringformat2.LineAlignment = StringAlignment.Center;

//Set the font color/format/size etc..
Color StringColor = System.Drawing.ColorTranslator.FromHtml("#933eea");//direct color adding Color StringColor2 = System.Drawing.ColorTranslator.FromHtml("#e80c88");//customise color adding string Str_TextOnImage = "Happy";//Your Text On Image string Str_TextOnImage2 = "Onam";//Your Text On Image

graphicsImage.DrawString(Str_TextOnImage, new Font("arial", 40, FontStyle.Regular), new SolidBrush(StringColor), new Point(268, 245), stringformat); Response.ContentType = "image/jpeg";

graphicsImage.DrawString(Str_TextOnImage2, new Font("Edwardian Script ITC", 111, FontStyle.Bold), new SolidBrush(StringColor2), new Point(145, 255), stringformat2); Response.ContentType = "image/jpeg";

bitmap.Save(Response.OutputStream, ImageFormat.Jpeg);

图形和位图

Bitmap 是用于处理由像素数据定义的图像的对象,您可以使用 Graphics 对象绘制图像对象。检查下面的代码。


 //creating a image object
System.Drawing.Image bitmap = (System.Drawing.Image)Bitmap.FromFile(Server.MapPath("onam.jpg")); // set image 
//draw the image object using a Graphics object
Graphics graphicsImage = Graphics.FromImage(bitmap);

//Set the alignment based on the coordinates
StringFormat stringformat = new StringFormat(); stringformat.Alignment = StringAlignment.Far; stringformat.LineAlignment = StringAlignment.Far;

StringFormat stringformat2 = new StringFormat(); stringformat2.Alignment = StringAlignment.Center; stringformat2.LineAlignment = StringAlignment.Center;

//Set the font color/format/size etc..
Color StringColor = System.Drawing.ColorTranslator.FromHtml("#933eea");//direct color adding Color StringColor2 = System.Drawing.ColorTranslator.FromHtml("#e80c88");//customise color adding string Str_TextOnImage = "Happy";//Your Text On Image string Str_TextOnImage2 = "Onam";//Your Text On Image

graphicsImage.DrawString(Str_TextOnImage, new Font("arial", 40, FontStyle.Regular), new SolidBrush(StringColor), new Point(268, 245), stringformat); Response.ContentType = "image/jpeg";

graphicsImage.DrawString(Str_TextOnImage2, new Font("Edwardian Script ITC", 111, FontStyle.Bold), new SolidBrush(StringColor2), new Point(145, 255), stringformat2); Response.ContentType = "image/jpeg";

bitmap.Save(Response.OutputStream, ImageFormat.Jpeg);

格式化字符串

根据坐标更改文本对齐方式。


 //creating a image object
System.Drawing.Image bitmap = (System.Drawing.Image)Bitmap.FromFile(Server.MapPath("onam.jpg")); // set image 
//draw the image object using a Graphics object
Graphics graphicsImage = Graphics.FromImage(bitmap);

//Set the alignment based on the coordinates
StringFormat stringformat = new StringFormat(); stringformat.Alignment = StringAlignment.Far; stringformat.LineAlignment = StringAlignment.Far;

StringFormat stringformat2 = new StringFormat(); stringformat2.Alignment = StringAlignment.Center; stringformat2.LineAlignment = StringAlignment.Center;

//Set the font color/format/size etc..
Color StringColor = System.Drawing.ColorTranslator.FromHtml("#933eea");//direct color adding Color StringColor2 = System.Drawing.ColorTranslator.FromHtml("#e80c88");//customise color adding string Str_TextOnImage = "Happy";//Your Text On Image string Str_TextOnImage2 = "Onam";//Your Text On Image

graphicsImage.DrawString(Str_TextOnImage, new Font("arial", 40, FontStyle.Regular), new SolidBrush(StringColor), new Point(268, 245), stringformat); Response.ContentType = "image/jpeg";

graphicsImage.DrawString(Str_TextOnImage2, new Font("Edwardian Script ITC", 111, FontStyle.Bold), new SolidBrush(StringColor2), new Point(145, 255), stringformat2); Response.ContentType = "image/jpeg";

bitmap.Save(Response.OutputStream, ImageFormat.Jpeg);

图片上的文字

设置字体颜色、大小、格式。


 //creating a image object
System.Drawing.Image bitmap = (System.Drawing.Image)Bitmap.FromFile(Server.MapPath("onam.jpg")); // set image 
//draw the image object using a Graphics object
Graphics graphicsImage = Graphics.FromImage(bitmap);

//Set the alignment based on the coordinates
StringFormat stringformat = new StringFormat(); stringformat.Alignment = StringAlignment.Far; stringformat.LineAlignment = StringAlignment.Far;

StringFormat stringformat2 = new StringFormat(); stringformat2.Alignment = StringAlignment.Center; stringformat2.LineAlignment = StringAlignment.Center;

//Set the font color/format/size etc..
Color StringColor = System.Drawing.ColorTranslator.FromHtml("#933eea");//direct color adding Color StringColor2 = System.Drawing.ColorTranslator.FromHtml("#e80c88");//customise color adding string Str_TextOnImage = "Happy";//Your Text On Image string Str_TextOnImage2 = "Onam";//Your Text On Image

graphicsImage.DrawString(Str_TextOnImage, new Font("arial", 40, FontStyle.Regular), new SolidBrush(StringColor), new Point(268, 245), stringformat); Response.ContentType = "image/jpeg";

graphicsImage.DrawString(Str_TextOnImage2, new Font("Edwardian Script ITC", 111, FontStyle.Bold), new SolidBrush(StringColor2), new Point(145, 255), stringformat2); Response.ContentType = "image/jpeg";

bitmap.Save(Response.OutputStream, ImageFormat.Jpeg);

字符串颜色

以两种方式添加字符串颜色...


 //creating a image object
System.Drawing.Image bitmap = (System.Drawing.Image)Bitmap.FromFile(Server.MapPath("onam.jpg")); // set image 
//draw the image object using a Graphics object
Graphics graphicsImage = Graphics.FromImage(bitmap);

//Set the alignment based on the coordinates
StringFormat stringformat = new StringFormat(); stringformat.Alignment = StringAlignment.Far; stringformat.LineAlignment = StringAlignment.Far;

StringFormat stringformat2 = new StringFormat(); stringformat2.Alignment = StringAlignment.Center; stringformat2.LineAlignment = StringAlignment.Center;

//Set the font color/format/size etc..
Color StringColor = System.Drawing.ColorTranslator.FromHtml("#933eea");//direct color adding Color StringColor2 = System.Drawing.ColorTranslator.FromHtml("#e80c88");//customise color adding string Str_TextOnImage = "Happy";//Your Text On Image string Str_TextOnImage2 = "Onam";//Your Text On Image

graphicsImage.DrawString(Str_TextOnImage, new Font("arial", 40, FontStyle.Regular), new SolidBrush(StringColor), new Point(268, 245), stringformat); Response.ContentType = "image/jpeg";

graphicsImage.DrawString(Str_TextOnImage2, new Font("Edwardian Script ITC", 111, FontStyle.Bold), new SolidBrush(StringColor2), new Point(145, 255), stringformat2); Response.ContentType = "image/jpeg";

bitmap.Save(Response.OutputStream, ImageFormat.Jpeg);

重要部分

命名空间

以下命名空间包含 Graphics、Bitmaps、Image Editing 和 Alignment 等库。


 //creating a image object
System.Drawing.Image bitmap = (System.Drawing.Image)Bitmap.FromFile(Server.MapPath("onam.jpg")); // set image 
//draw the image object using a Graphics object
Graphics graphicsImage = Graphics.FromImage(bitmap);

//Set the alignment based on the coordinates
StringFormat stringformat = new StringFormat(); stringformat.Alignment = StringAlignment.Far; stringformat.LineAlignment = StringAlignment.Far;

StringFormat stringformat2 = new StringFormat(); stringformat2.Alignment = StringAlignment.Center; stringformat2.LineAlignment = StringAlignment.Center;

//Set the font color/format/size etc..
Color StringColor = System.Drawing.ColorTranslator.FromHtml("#933eea");//direct color adding Color StringColor2 = System.Drawing.ColorTranslator.FromHtml("#e80c88");//customise color adding string Str_TextOnImage = "Happy";//Your Text On Image string Str_TextOnImage2 = "Onam";//Your Text On Image

graphicsImage.DrawString(Str_TextOnImage, new Font("arial", 40, FontStyle.Regular), new SolidBrush(StringColor), new Point(268, 245), stringformat); Response.ContentType = "image/jpeg";

graphicsImage.DrawString(Str_TextOnImage2, new Font("Edwardian Script ITC", 111, FontStyle.Bold), new SolidBrush(StringColor2), new Point(145, 255), stringformat2); Response.ContentType = "image/jpeg";

bitmap.Save(Response.OutputStream, ImageFormat.Jpeg);

输出