SoFunction
Updated on 2025-03-04

Add watermark information to the image and save it as Jpg type

using ;
using ;
using ;

private void AddTextToImg(string fileName,string text)
{
if(!(MapPath(fileName)))
{
throw new FileNotFoundException("The file don't exist!");
}

if( text ==  )
{
return;
}
//You also need to determine whether the file type is an image type, so I won't go into details here
 image = (MapPath(fileName));
Bitmap bitmap = new Bitmap(image,,);
Graphics g = (bitmap);

float fontSize = 12.0f; //Font sizefloat textWidth = *fontSize; //Length of text//Define a rectangular area below, and then draw black characters on white background in this rectangle in the futurefloat rectX = 0; 
float rectY = 0;
float rectWidth = *(fontSize+8);
float rectHeight = fontSize+8;
//Declare rectangular fieldsRectangleF textArea = new RectangleF(rectX,rectY,rectWidth,rectHeight);

Font font = new Font("Songyi",fontSize); //Define the fontBrush whiteBrush = new SolidBrush(); //White brush, use for textBrush blackBrush = new SolidBrush(); //Black brush, for drawing background
(blackBrush,rectX,rectY,rectWidth,rectHeight); 

(text,font,whiteBrush,textArea);
MemoryStream ms = new MemoryStream( );
//Save as Jpg type(ms,);

//Output the processed image. For the convenience of demonstration, I will display the image on the page.();
 = "image/jpeg";
( () );

();
();
();
}

It's very simple to call.

AddTextToImg("","Program Lifehttp:///");

Everything is OK, I feel that .net is indeed very powerful. These functions are luxury goods in Asp, but they can be easily completed in the .Net environment!