Add copyright information to the image in Asp.net

Source: Internet
Author: User

Abstract: http://dev.csdn.net/author/starchenzhi/506a6b59aaa04542ae1df3c007184a36.html

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 ();
}

It's easy to call,
Addtexttoimg ("me.jpg", "Xiaozhi ");

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.