Watermark definition: a watermark generally refers to some copyright information text on an image, or some graphics or text attached to the original image for a certain purpose.
The basic way to create a watermark is to use the GDI + method to draw a text or image at the specified position of the image.
When it comes to GDI +, it is generally used for drawing GUI in Winform. For example, to make a text editor, you can use the GDI function to draw text on the form surface. In fact, GDI not only can draw forms, it can draw the surface of all Drawable.
The watermark I recorded is to use the GDI function to merge the source image and the watermark image, or to draw text on the source image.
Key GDI functions: (methods of the System. Drawing. Graphics class)
DrawImage
Draw text with DrawString
These two functions have a lot of overloading. For details, refer to MSDN.
Watermark image:
Watermark images generally use pure green or blue background colors, just like when shooting a movie. When shooting a video, the actors act in the green background. during post-production, the green background will be replaced by transparent colors. In C #, we use the SetRemapTable image feature to complete this task. It is used to replace a color on the image with another color. OldColor is the color to be replaced, while NewColor is the new color. 5-10 rows to complete this operation.
Next, we will decrease the transparency of all the points in the image to 30% of the original. ColorMatrix is used here. Matrices are widely used in many places, especially in transformation areas, such as stretching, scaling, and so on. They all use rapid operations of matrix multiplication. The matrix is also used for high-speed transformation of all pixels. Observe carefully the numbers on the diagonal lines of the 12-row matrix: 0.3, 1, and 1, which are multiples. Corresponding to the transformation multiple of RGBAW. RGB remains unchanged, and the transparency is 0.3 times the original. W also remains unchanged. In this way, the image transparency is reduced.
These color transformations are performed only when the Draw method is executed.
1 private static ImageAttributes getImageAttributes (){
2
3 ImageAttributes imgAttr = new ImageAttributes ();
4
5 ColorMap colorMap = new ColorMap ();
6 colorMap. OldColor = Color. FromArgb (255, 0,255, 0 );
7 colorMap. NewColor = Color. FromArgb (0, 0, 0, 0 );
8 ColorMap [] remapTable = {colorMap };
9
10 imgAttr. SetRemapTable (remapTable, ColorAdjustType. Bitmap );
11
12 float [] [] colorMatrixElements = {
13 new float [] {1.0f, 0.0f, 0.0f, 0.0f, 0.0f },
14 new float [] {0.0f, 1.0f, 0.0f, 0.0f, 0.0f },
15 new float [] {0.0f, 0.0f, 1.0f, 0.0f, 0.0f },
16 new float [] {0.0f, 0.0f, 0.0f, 0.3f, 0.0f },
17 new float [] {0.0f, 0.0f, 0.0f, 0.0f, 1.0f}
18 };
19
20 ColorMatrix wmColorMatrix = new ColorMatrix (colorMatrixElements );
21 imgAttr. SetColorMatrix (wmColorMatrix, ColorMatrixFlag. Default, ColorAdjustType. Bitmap );
22
23 return imgAttr;
24}
There is no need to paste all the code where the watermark is drawn.
1 ImageAttributes imgAttr = getImageAttributes ();
2 Rectangle rect = getPicPoint (src, wm, wp );
3
4g. DrawImage (wm, rect, 0, 0, wm. Width, wm. Height, GraphicsUnit. Pixel, imgAttr );
The first line gets the transformation rule
In the second row, obtain the watermark rectangle: we can specify the watermark position, such as the upper left corner and lower right corner. You can specify the position to obtain the rectangle of different drawn areas. However, the getPicPoint method does not have a good name, and people mistakenly think that a Point object is actually a matrix object. It is recommended to change to getPicPosition or getPicRect.
The third line is painting. in a specified place, use the specified color transform to draw the specified image.
It is quite easy to draw the watermark text. Use a transparent brush to draw the watermark on the image.
FontAndSize fs = FontAndSize. GetValue (g, words, "arial", fontSize, src. Width );
PointF p = getTextPoint (src, fs. size, wp );
StringFormat sf = getStringFormat ();
DrawText (g, words, fs. font, p, sf );
Of course, here I recorded the use of two colors to draw the text, to achieve a shadow effect text effect. It can be seen that the author is developing my records with great care ....
1 private static void drawText (Graphics g, String wmText, Font font, PointF p, StringFormat format ){
2
3 SolidBrush brush = new SolidBrush (Color. FromArgb (153,255,255,255 ));
4g. DrawString (wmText, font, brush, p, format );
5
6 SolidBrush brush2 = new SolidBrush (Color. FromArgb (153, 0, 0, 0 ));
7g. DrawString (wmText, font, brush2, new PointF (p. X + 1, p. Y + 1), format );
8}
Of course, there are too few comments in the code, which may be non-core classes, so there is no comment. Of course, we will gradually supplement the annotations later. As a project serving the masses, annotations are indispensable. The code that everyone can understand is our goal.
The verification code is created in wojiluDrawingValidationCode. cs.
You can create a verification code in three steps.
1. Draw a background with noise
2. Draw the verification code
3. Draw noise at the top layer.
1 public Image CreateImage (String code, int width, int height, String fontFamily ){
2
3 Bitmap bm = new Bitmap (width, height );
4
5 using (Graphics g = Graphics. FromImage (bm )){
6
7g. SmoothingMode = SmoothingMode. AntiAlias;
8
9 HatchBrush brush = new HatchBrush (HatchStyle. SmallConfetti, Color. LightGray, Color. White );
10 Rectangle rect = new Rectangle (0, 0, width, height );
11g. FillRectangle (brush, rect );
12
13 int fontsize = rect. Height + 1;
14 FontAndSize size = FontAndSize. GetValue (g, code, fontFamily, fontsize, bm. Width );
15
16 & nbs