asp.net writing text into a picture

Source: Internet
Author: User
asp.net

/**////<summary>
Function: Write text to a picture
Date: 2006-11-20
Author: Yang Yixian
</summary>
<param name= "strFileName" > File name </param>
<param name= "strtext" > Text to write </param>
private void Texttoimage (String strfilename,string strtext)
{
if (! File.exists (MapPath (strFileName))
{
throw new FileNotFoundException ("file does not exist!");
}

if (strtext = = string. Empty)
{
Return
}
You also need to determine whether the file type is an image type
System.Drawing.Image Image = System.Drawing.Image.FromFile (MapPath (strFileName));
Bitmap Bitmap = new Bitmap (image,image. Width,image. Height);
Graphics grap = graphics.fromimage (bitmap);
float fontsize = 24.0f; Font size
float textWidth = strtext.  Length*fontsize; Length of text
The following defines a rectangular area, in which the white and black characters are later painted in the rectangle.
float rectx = 0;
float recty = 0;
float rectwidth = strtext. length* (FONTSIZE+12);
float rectheight = fontsize+8;
String strfont;//define font type
Declaring a rectangular field
RectangleF TextArea = new RectangleF (rectx,recty,rectwidth,rectheight);
Strfont = Ddlfont. SelectedItem.Text.ToString (). Trim ();
Font font = new Font (strfont,fontsize); Defining fonts
Brush Whitebrush = new SolidBrush (color.white); White brush, painting text with
Brush Blackbrush = new SolidBrush (color.transparent); Black Brush, drawing background

Grap. FillRectangle (Blackbrush,rectx,recty,rectwidth,rectheight);

Grap. DrawString (Strtext,font,whitebrush,textarea);
MemoryStream ms = new MemoryStream ();
Save as JPG type
Bitmap. Save (Ms,imageformat.jpeg);

Output processed image.//Here you can enlarge the generated picture into Oracle database power

Response.Clear ();
Response.ContentType = "Image/jpeg";
Response.BinaryWrite (Ms. ToArray ());
Grap. Dispose ();
Bitmap. Dispose ();
Image. Dispose ();
}
Event:

private void Btnwrite_click (object sender, System.EventArgs e)
{
Httppostedfile Httppostfile; Client upload File Object
Httppostfile = Docupload.postedfile;
String strtemp;
String strfilename;//Completion file name
strtemp = HttpPostFile.FileName.ToString (). Trim ();
strFileName = Getrealfilename (strtemp);
String strimg = "images/";
Texttoimage (strimg +strfilename,txtdocname.text.tostring ());

}/**////<summary>
function: Get filename
Date: 2006-11-20
Author: Yang Yixian
</summary>
<param name= "strpathname" > File path </param>
<returns></returns>
public string Getrealfilename (string strpathname)
{
string [] strFileName = strpathname.split (' \ ');
int iPos = strfilename.getupperbound (0);
return Strfilename[ipos];
}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.