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

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

欢迎加入小哈的星球 ,你将获得:专属的项目实战 / 1v1 提问 / Java 学习路线 / 学习打卡 / 每月赠书 / 社群讨论

  • 新项目:《从零手撸:仿小红书(微服务架构)》 正在持续爆肝中,基于 Spring Cloud Alibaba + Spring Boot 3.x + JDK 17...点击查看项目介绍 ;
  • 《从零手撸:前后端分离博客项目(全栈开发)》 2 期已完结,演示链接: http://116.62.199.48/ ;

截止目前, 星球 内专栏累计输出 63w+ 字,讲解图 2808+ 张,还在持续爆肝中.. 后续还会上新更多项目,目标是将 Java 领域典型的项目都整一波,如秒杀系统, 在线商城, IM 即时通讯,权限管理,Spring Cloud Alibaba 微服务等等,已有 2200+ 小伙伴加入学习 ,欢迎点击围观

介绍

本文介绍了如何使用 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);

输出


相关文章