Add watermark information to the image in Asp.net and save it as Jpg. asp.net watermark
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 need to determine whether the file type is of the image type. System is not described here. 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 rectangular area. In the future, the white-black float rectX = 0; float rectY = 0; float rectWidth = text. length * (fontSize + 8); float rectHeight = fontSize + 8; // declare the RectangleF textArea = new RectangleF (rectX, rectY, rectWidth, rectHeight ); font font = new Font ("", fontSize); // defines the Font Brush whiteBrush = new SolidBrush (Color. white); // a White Brush that draws text in a Brush blackBrush = new SolidBrush (Color. black); // The Black brush. Use g to paint the background. 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. For the convenience of demonstration, the image is displayed in Response. clear (); Response. contentType = "image/jpeg"; Response. binaryWrite (ms. toArray (); g. dispose (); bitmap. dispose (); image. dispose ();}
It's easy to call,
AddTextToImg ("me.jpg", "program life http://www.manong123.com /");
Everything is okay, and I feel that. net is really powerful. These functions are a luxury in Asp, but they can be easily completed in the. Net environment!
Aspnet (C #) adds a text watermark to the bottom right corner when uploading an image.
Try
{
String filepath = uploadFile. Value; // path of the uploaded Image
String newName = DateTime. Now. ToString ("yyyyMMddHHmmss"); // The new image name.
String filehz = filepath. Substring (filepath. LastIndexOf (".") + 1). ToLowerInvariant (); // suffix
String uploadpath = Server. MapPath ("./images/update/" + newName + "." + filehz); // The Server saves the image path
If (! (UploadFile. PostedFile. ContentLength> 0 ))
{
LblErrInfo. Text = "no file selected ";
}
Else
{
If (filehz = "jpg" | filehz = "gif" | filehz = "png ")
{
If (File. Exists (uploadpath ))
{
LblErrInfo. Text = "a file with the same name already exists ";
}
Else
{
If (chboxIsInfo. Checked = true) // Add copyright information
{
If (txtLeft. Text. Trim () = "" | txtRight. Text. Trim () = "" | txtAddInfo. Text. Trim () = "")
{
Response. Write ("<script> alert ('Enter the information location (halfwidth number) and text! ') </Script> ");
... The remaining full text>
Add a watermark to aspnet
Image Information should be stored in the database? Change the path without warehouse receiving. The watermark image name can be generated based on the system time (+ Random Number). The source image name can be used as an alias.