CopyCode The Code is as follows: using system;
Using system. Collections. Generic;
Using system. text;
Using system. drawing;
Using system. IO;
Using system. Drawing. imaging;
Namespace Chen
{
Public class warterfont
{
Public void addtexttoimg (string filename, string text)
{
If (! File. exists (system. Web. httpcontext. Current. server. mappath (filename )))
{
Throw new filenotfoundexception ("the file don't exist! ");
}
If (text = string. Empty)
{
Return;
}
// You also need to determine whether the file type is of the image type. I will not go into details here.
Image image = image. fromfile (system. Web. httpcontext. Current. server. mappath (filename ));
Bitmap bitmap = new Bitmap (image, image. Width, image. Height );
Graphics G = graphics. fromimage (Bitmap );
Float fontsize = 12.0f; // font size
Float textwidth = text. length * fontsize; // the length of the text.
// The following defines a rectangle area, and then draws black characters on the rectangle.
Float rectx = 0;
Float recty = 0;
Float rectwidth = text. length * (fontsize + 8 );
Float rectheight = fontsize + 8; // declare the rectangular Field
Rectanglef textarea = new rectanglef (rectx, recty, rectwidth, rectheight );
Font font = new font ("", fontsize); // define the font
Brush whitebrush = new solidbrush (color. White); // white brush, used to draw text
Brush blackbrush = new solidbrush (color. Black); // black brush for background painting
G. fillrectangle (blackbrush, rectx, recty, rectwidth, rectheight );
G. drawstring (text, Font, whitebrush, textarea );
Memorystream MS = new memorystream (); // save as JPG type
Bitmap. Save (MS, imageformat. JPEG); // output the processed image, which is displayed on the page for ease of demonstration
Bitmap. Save (system. Web. httpcontext. Current. server. mappath ("/" + "aa.jpg"), imageformat. JPEG); // save to disk
System. Web. httpcontext. Current. response. Clear ();
System. Web. httpcontext. Current. response. contenttype = "image/JPEG ";
System. Web. httpcontext. Current. response. binarywrite (Ms. toarray ());
G. Dispose ();
Bitmap. Dispose ();
Image. Dispose ();
}
}
}