ASP.NET Write Text to Image (StringFormat)       | 
   
 
			  
			  
                ASP.NET Write Text to Image (StringFormat) ตัวอย่างการสร้างรูปภาพ และ การเขียนข้อความลงในรูปภาพ และการจัดตำแหน่งของข้อความในตำแหน่งต่าง ๆ 
 
Language Code :   VB.NET || C# 
 
Framework : 1,2,3,4 
 
AspNetWriteTextImages2.aspx 
 
<%@ Page Language="VB" %>
<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Drawing.Imaging" %>
<script runat="server">
	Sub Page_Load(sender As Object, e As EventArgs)	
		Dim intWidth,intHeight As Integer
		Dim FileName As String
		intWidth = 	400
		intHeight = 200
		FileName = "MyImages/Images.png"
		Dim objBitmap As Bitmap = New Bitmap(intWidth, intHeight)
		Dim objGraphic As Graphics = Graphics.FromImage(objBitmap)		
		'*** Create Images ***'
		objGraphic.FillRectangle(Brushes.Maroon, 0, 0, intWidth, intHeight)
		'*** Write Text ***'
		Dim strText As String = "ยินดีต้อนรับ Welcome To www.ThaiCreate.Com" '*** String ***'
		Dim objFont As New Font("Tahoma", 12,FontStyle.Bold) '*** Font Style ***'
		Dim objBrushWrite As New SolidBrush(Color.YellowGreen) '*** Font Color ***'
		
		'*** Text Align ***'
		Dim strFormat As 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 = Nothing
		objGraphic = Nothing
		'*** View Images ***'
		Me.imgPicture.ImageURL = FileName
		
	End Sub
</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> 
 
ในตัวอย่างจะสามารถจัดตำแหน่งได้ตามต้องการ ซึ่งผมได้เขียน Comment เพื่อสะดวกในการใช้งาน โดย Property ต่าง ๆ  เหล่านี้จะสามารถมีให้เลือกใช้งานได้ เมื่อพัฒนาโปรแกรมบน Visual Studio .NET 2003 หรือ Visual Studio 2005,Visual Studio 2008 
 
  
 
Screenshot  
 
               
  
              			
			  
								  
			  
  
                            
                
                  | Property & Method (Others Related) | 
                 
               
			   
			  			  
			  			  
			              
  |