(C#) ASP.NET Write Text to Picture |
(C#) ASP.NET Write Text to Picture จากตัวอย่างก่อนหน้านี้จะเป็นการสร้างรูปภาพขึ้นมาใหม่ แต่ตัวอย่างนี้ผมจะทำการเปิดไฟล์ที่มีอยู่แล้ว และทำการเขียนข้อความลงไป
Language Code : VB.NET || C#
Framework : 1,2,3,4
AspNetWriteTextPicture.aspx
<%@ Page Language="C#" Debug="true"%>
<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Drawing.Imaging" %>
<script runat="server">
void Page_Load(object sender, EventArgs e)
{
string OpenFileName;
string NewFileName;
OpenFileName = "MyImages/Mygirl.jpg";
NewFileName = "MyImages/New_Mygirl.jpg";
Bitmap objBitmap = new System.Drawing.Bitmap(Server.MapPath(OpenFileName));
Graphics objGraphic = Graphics.FromImage(objBitmap);
//*** Write Text ***//
string strText = "CopyRight 2009 (www.ThaiCreate.Com)";
//*** String ***//
Font objFont = new Font("Tahoma", 8);
//*** Font Style ***//
SolidBrush objBrushWrite = new SolidBrush(Color.Red);
//*** Font Color ***//
//*** Text Align ***//
StringFormat strFormat = new StringFormat();
strFormat.Alignment = StringAlignment.Far;
//*** .Near (Left) , .Far (Right) ***//
strFormat.LineAlignment = StringAlignment.Far;
//*** .Near (Top) , .Far (Down) ***//
//*** DrawString ***//
objGraphic.DrawString(strText, objFont, objBrushWrite, new Rectangle(0, 0, objBitmap.Width, objBitmap.Height), strFormat);
//*** Save As ***//
objBitmap.Save(Server.MapPath(NewFileName), ImageFormat.Jpeg);
//*** Close ***//
objGraphic.Dispose();
//*** Nothing ***//
objBitmap = null;
objGraphic = null;
//*** View Images ***//
this.imgPicture.ImageUrl = NewFileName;
}
</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
|