I would like to share with you today that "C # asp.net uploading images and adding texts with watermarks" has no technical content and I have not installed 13, but I still hope to repost it with the author and source. Thank you!
Articles with images and text watermarks go to Baidu or Google to search a lot, but it seems that there are not many useful articles. Many of them are in a single version, and others are reposted or shared, in many cases, I cannot meet our own needs. Today, I just want to use the image watermarking knowledge. So I sorted out my experiences for your reference only, i'm a cainiao, haha ~~
Let's start with the following:
Step 1: First, I want the principle
Because of the use of Weibo, we have noticed that your Weibo address will be added to the bottom-right corner of the Weibo image, and the watermark will be added to the bottom-right corner no matter whether the image is large or small, based on this idea, I drew a picture to explain it intuitively...:
From the image, we can intuitively and clearly see the principle of the image, so the problem that our code should solve is to get the width and height of the image, and calculate the length and height of the Area Based on the font size and number of characters, which is very simple...
Step 2: Write and test the code
The process is omitted. Check the Code directly:
AddWaterText
/// <Summary>
/// Add watermark text to the image
/// </Summary>
/// <Param name = "oldpath"> old image address </param>
/// <Param name = "text"> watermark text </param>
/// <Param name = "newpath"> New Image address </param>
/// <Param name = "Alpha"> Transparency </param>
/// <Param name = "fontsize"> font size </param>
Public static void AddWaterText (string oldpath, string text, string newpath, int Alpha, int fontsize)
{
Try
{
Text = text + "Copyright ";
FileStream fs = new FileStream (oldpath, FileMode. Open );
BinaryReader br = new BinaryReader (fs );
Byte [] bytes = br. ReadBytes (int) fs. Length );
Br. Close ();
Fs. Close ();
MemoryStream MS = new MemoryStream (bytes );
System. Drawing. Image imgPhoto = System. Drawing. Image. FromStream (MS );
Int imgPhotoWidth = imgPhoto. Width;
Int imgPhotoHeight = imgPhoto. Height;
Bitmap BMP hoto = new Bitmap (imgPhotoWidth, imgPhotoHeight, System. Drawing. Imaging. PixelFormat. Format24bppRgb );
BMP hoto. SetResolution (72, 72 );
Graphics gbmPhoto = Graphics. FromImage (BMP hoto );
// Gif background color
GbmPhoto. Clear (Color. FromName ("white "));
GbmPhoto. InterpolationMode = System. Drawing. Drawing2D. InterpolationMode. High;
GbmPhoto. SmoothingMode = System. Drawing. Drawing2D. SmoothingMode. HighQuality;
GbmPhoto. DrawImage (imgPhoto, new Rectangle (0, 0, imgPhotoWidth, imgPhotoHeight), 0, 0, imgPhotoWidth, imgPhotoHeight, GraphicsUnit. Pixel );
System. Drawing. Font font = null;
System. Drawing. SizeF crSize = new SizeF ();
Font = new Font ("", fontsize, FontStyle. Bold );
// Measurement in the specified area www.2cto.com
CrSize = gbmPhoto. MeasureString (text, font );
Float y = imgPhotoHeight-crSize. Height;
Float x = imgPhotoWidth-crSize. Width;
System. Drawing. StringFormat StrFormat = new System. Drawing. StringFormat ();
StrFormat. Alignment = System. Drawing. StringAlignment. Center;
// Draw two transparent Effects
System. Drawing. SolidBrush semiTransBrush2 = new System. Drawing. SolidBrush (Color. FromArgb (Alpha, 56, 56, 56 ));
GbmPhoto. DrawString (text, font, semiTransBrush2, x + 1, y + 1 );
System. Drawing. SolidBrush semiTransBrush = new System. Drawing. SolidBrush (Color. FromArgb (Alpha, 176,176,176 ));
GbmPhoto. DrawString (text, font, semiTransBrush, x, y );
BMP hoto. Save (newpath, System. Drawing. Imaging. ImageFormat. Jpeg );
GbmPhoto. Dispose ();
ImgPhoto. Dispose ();
BMP hoto. Dispose ();
}
Catch
{
;
}
}
I will not explain the code, but I have basically explained it in principle. In addition, for parameter settings, we can also add fonts, font colors, whether to bold, and what seats to locate, so that the encapsulation is better, as I only need to add the watermark to the bottom right corner, I will not write any more... the principle is the same. encapsulation is also well encapsulated. Let's write it for you, haha...
Next let's take a look, because the figure is the most intuitive display...
I just cut a picture above.
Call method: namespace. Class Name. AddWaterText (picpath, "Tandy Tang-blog Garden", picpath, 255, 18 );
Don't forget to recommend it if you think it is helpful to you ~~
From Tandy Tang's Personal Blog