(C#) ASP.NET Write Text to Image (PrivateFontCollection-AddFontFile) |
(C#) ASP.NET Write Text to Image (AddFontFile) การเขียนข้อความลงในรูปภาพ และสามารถกำหนดประเภทของ Font ได้ โดยสามารถเพิ่ม Font จากภายนอกได้ครับ ตัวอย่างนี้ผมได้ใช้ Font ชื่อ Matrix
Language Code : VB.NET || C#
Framework : 1,2,3,4
AspNetWriteTextImages3.aspx
<%@ Page Language="C#" Debug="true" %>
<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Drawing.Imaging" %>
<%@ Import Namespace="System.Drawing.Text" %>
<script runat="server">
void Page_Load(object sender, EventArgs e)
{
int intWidth = 0;
int intHeight = 0;
string FileName = null;
intWidth = 400;
intHeight = 200;
FileName = "MyImages/Images.png";
Bitmap objBitmap = new Bitmap(intWidth, intHeight);
Graphics objGraphic = Graphics.FromImage(objBitmap);
//*** Create Images ***//
objGraphic.FillRectangle(Brushes.Maroon, 0, 0, intWidth, intHeight);
//*** Write Text ***//
string strText = "The Matrix";
//*** String ***//
//*** Font **//
PrivateFontCollection myFont = new PrivateFontCollection();
myFont.AddFontFile(Server.MapPath("MyImages/MLTWNII_.ttf"));
FontFamily thisFont = myFont.Families[0];
Font objFont = new Font(thisFont, 30, FontStyle.Bold);
//*** Font Style ***//
SolidBrush objBrushWrite = new SolidBrush(Color.YellowGreen);
//*** Font Color ***//
//*** Text Align ***//
StringFormat strFormat = new StringFormat();
strFormat.Alignment = StringAlignment.Center;
//*** .Near (Left) , .Far (Right) ***//
strFormat.LineAlignment = StringAlignment.Center;
//*** .Near (Top) , .Far (Down) ***//
//*** DrawString ***//
objGraphic.DrawString(strText, objFont, objBrushWrite, new Rectangle(0, 0, intWidth, intHeight), strFormat);
//*** Save As ***//
objBitmap.Save(Server.MapPath(FileName), ImageFormat.Png);
//*** Close ***//
objGraphic.Dispose();
//*** Nothing ***//
objBitmap = null;
objGraphic = null;
//*** View Images ***//
this.imgPicture.ImageUrl = FileName;
}
</script>
<html>
<head>
<title>ThaiCreate.Com ASP.NET - Images (System.Drawing)</title>
</head>
<body>
<form id="form1" runat="server">
<asp:Image id="imgPicture" runat="server" />
</form>
</body>
</html>
</form>
Screenshot
Property & Method (Others Related) |
|