نوشتن متن روی عکس با استفاده از ASP.Net و #C
یکشنبه 15 شهریور 1394در این مقاله آموزش می دهیم که چگونه با استفاده از ASP.Net و #C روی تصاویر، متن بنویسید.
در این مقاله، از تعدادی توابع کتابخانه ای و متدهای رایج برای نوشتن متن روی تصویر استفاده کرده ایم.
کلاس Image و Bitmap:
- کلاس Image، مثالی از یک کلاس abstract می باشد.
- کلاس Bitmap یک نمونه پیاده سازی از کلاس Image است که از آن ارث بری می کند.
کد #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 = "برنامه نویسان";//Your Text On Image
string Str_TextOnImage2 = "Barnamenevisan.org";//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 و Bitmap:
Bitmap شی ای است که برای کار کردن با تصاویری که به صورت داده های پیکسلی هستند، به کار می رود و ما می توانیم این داده پیکسلی را با استفاده از شیء Graphics رسم نماییم. کد استفاده از آن به شکل زیر است:
//creating a image object
System.Drawing.Image bitmap = (System.Drawing.Image)Bitmap.FromFile(Server.MapPath("barnamenevisan.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;
متن روی عکس:
حال سایز، رنگ و فرمت را تنظیم می کنیم.
string Str_TextOnImage = "برنامه نویسان";//Your Text On Image
string Str_TextOnImage2 = "Barnamenevisan.org";//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";
رنگ رشته متنی:
رنگ را می توان به دو روش زیر انتخاب کرد:
Color StringColor = System.Drawing.Color.Red;//direct color adding
Color StringColor = System.Drawing.ColorTranslator.FromHtml("#933eea");//customise color adding
فضای نام:
فضای نام های زیر باید در پروژه قرار بگیرد:
using System.Drawing; using System.Drawing.Imaging;
فضای نام های بالا شامل Graphics، Bitmap، ویرایش عکس و کتابخانه ترازبندی متن و کتابخانه های دیگری است.
- ASP.net
- 3k بازدید
- 3 تشکر