(Vs2005 is used for design and development of small programs to add text information to images. Of course, professional image processing software can also be used, but it is quite troublesome.
Using system. drawing;
Using system. IO;
Using system. Drawing. imaging;
Private void addtexttoimg (string filename, string text)
{
If (! File. exists (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.
System. Drawing. Image image = system. Drawing. image. fromfile (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
Bitmap. Save (MS, imageformat. JPEG );
// Output the processed image. For demonstration convenience, I will display the image on the page
Response. Clear ();
Response. contenttype = "image/JPEG ";
Response. binarywrite (Ms. toarray ());
G. Dispose ();
Bitmap. Dispose ();
Image. Dispose ();
}