Have you tried using .net for image processing? In a recent project carried out by the company, a lot of product pictures were stored in the database, but none of them had copyright information. At that time, the customer required that all pictures be labeled with the company name? At this time, what should you do?
Although I am still a novice, there are still many solutions to this problem, which can be summarized as follows:
1. Use graphics processing software, such as Photoshop, etc., and use its batch processing function to achieve this function, but every time the data entry clerk adds If you want to enter pictures, you have to process them, which is very troublesome. I see that the entry clerk is usually very nice to me and greets me with a smile every day. Can I bear to torture her? This plan was rejected.
2. Using .net's smooth image processing, when the entry clerk uploads the picture, it will automatically add the company logo. Wouldn't it be better? Well, this is a good idea. It can be ranked among the top 10,000 best solutions in 2005. , just do it as you say.
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 an image type, which will not be described 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
//Define a rectangular area below, and then draw black text on a white background in this rectangle
float rectX = 0;
float rectY = 0;
float rectWidth = text.Length*(fontSize+8);
float rectHeight = fontSize+8;
//Declare the rectangular domain
RectangleF textArea = new RectangleF(rectX,rectY,rectWidth,rectHeight);
Font font = new Font("宋体",fontSize); //Define font
Brush whiteBrush = new SolidBrush(Color.White); //White brush, used for drawing text
Brush blackBrush = new SolidBrush(Color.Black); //Black brush, draw the background with
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. For the convenience of demonstration, I display the image on the page.
Response.Clear();
Response.ContentType = "image/jpeg";
Response.BinaryWrite( ms.ToArray() );
g.Dispose();
bitmap.Dispose();
image.Dispose();
}
The call is very simple,
AddTextToImg("me.jpg","Xiao Zhi");
everything is OK. I feel that .net is really powerful. These functions are luxury goods in Asp, but they can be easily done in the .Net environment. Complete!