Generate text on the specified image, the text format is adjustable, and the final effect is
Public class waterark
{
Private int _ width;
Private int _ height;
Private string _ fontfamily;
Private int _ fontsize;
Private bool _ adaptable;
Private fontstyle _ fontstyle;
Private bool _ shadow;
Private string _ backgroundimage;
Private color _ bgcolor;
Private int _ left;
Private string _ resultimage;
Private string _ text;
Private int _ top;
Private int _ Alpha;
Private int _ Red;
Private int _ green;
Private int _ blue;
Private long _ quality;
Public waterark ()
{
//
// Todo: Add constructor logic here
//
_ Width = 460;
_ Height = 330;
_ Fontfamily = "正 ";
_ Fontsize = 28;
_ Fontstyle = fontstyle. Regular;
_ Adaptable = true;
_ Shadow = false;
_ Left = 45;
_ Top = 80;
_ Alpha = 255;
_ Red = 250;
_ Green= 250;
_ Blue = 250;
_ Backgroundimage = "";
-Quality = 100;
_ Bgcolor = color. fromargb (255,229,229,229 );
_ Shadow = false;
}
/**/
/// <Summary>
/// Font
/// </Summary>
Public String fontfamily
{
Set {This. _ fontfamily = value ;}
}
/**/
/// <Summary>
/// Text size
/// </Summary>
Public int fontsize
{
Set {This. _ fontsize = value ;}
}
/**/
/// <Summary>
/// Text style
/// </Summary>
Public fontstyle
{
Get {return _ fontstyle ;}
Set {_ fontstyle = value ;}
}
/**/
/// <Summary>
/// Transparency 0-255,255 indicates opacity
/// </Summary>
Public int alpha
{
Get {return _ Alpha ;}
Set {_ alpha = value ;}
}
/**/
/// <Summary>
/// Specifies whether the watermark text uses shadow.
/// </Summary>
Public bool shadow
{
Get {return _ shadow ;}
Set {_ shadow = value ;}
}
Public int red
{
Get {return _ Red ;}
Set {_ Red = value ;}
}
Public int green
{
Get {return _ green ;}
Set {_ Green = value ;}
}
Public int blue
{
Get {return _ blue ;}
Set {_ Blue = value ;}
}
/**/
/// <Summary>
/// Basemap
/// </Summary>
Public String backgroundimage
{
Set {This. _ backgroundimage = value ;}
}
/**/
/// <Summary>
/// Left margin of the watermark text
/// </Summary>
Public int left
{
Set {This. _ left = value ;}
}
/**/
/// <Summary>
/// Top margin of the watermark text
/// </Summary>
Public int top
{
Set {This. _ Top = value ;}
}
/**/
/// <Summary>
/// Generated image
/// </Summary>
Public String resultimage
{
Set {This. _ resultimage = value ;}
}
/**/
/// <Summary>
/// Watermark text
/// </Summary>
Public String text
{
Set {This. _ text = value ;}
}
/**/
/// <Summary>
/// Generate the Image Width
/// </Summary>
Public int width
{
Get {return _ width ;}
Set {_ width = value ;}
}
/**/
/// <Summary>
/// Generate the Image Height
/// </Summary>
Public int height
{
Get {return _ height ;}
Set {_ Height = value ;}
}
/**/
/// <Summary>
/// If the text is too large, determine whether to adjust the text size based on the background image. The default value is
/// </Summary>
Public bool adaptable
{
Get {return _ adaptable ;}
Set {_ adaptable = value ;}
}
Public color bgcolor
{
Get {return _ bgcolor ;}
Set {_ bgcolor = value ;}
}
/**/
/// <Summary>
/// Output image quality. The quality range is 0-100 and the type is long.
/// </Summary>
Public long Quality
{
Get {return _ quality ;}
Set {_ Quality = value ;}
}
/// <Summary>
/// Generate a watermark immediately
/// </Summary>
/// <Returns> true is returned if generation is successful; otherwise, false is returned. </returns>
Public bool create ()
{
Try
{
Bitmap bitmap;
Graphics g;
// Use a pure background color
If (this. _ backgroundimage. Trim () = "")
{
Bitmap = new Bitmap (this. _ width, this. _ height, pixelformat. format64bppargb );
G = graphics. fromimage (Bitmap );
G. Clear (this. _ bgcolor );
}
Else
{
Bitmap = new Bitmap (image. fromfile (this. _ backgroundimage ));
G = graphics. fromimage (Bitmap );
}
G. smoothingmode = smoothingmode. highquality;
G. interpolationmode = interpolationmode. highqualitybicubic;
G. compositingquality = compositingquality. highquality;
Font F = new font (_ fontfamily, _ fontsize, _ fontstyle );
Sizef size = G. measurestring (_ text, F );
// Adjust the text size until it can adapt to the image size
While (_ adaptable = true & size. width> bitmap. width)
{
_ Fontsize --;
F = new font (_ fontfamily, _ fontsize, _ fontstyle );
Size = G. measurestring (_ text, F );
}
Brush B = new solidbrush (color. fromargb (_ Alpha, _ Red, _ Green, _ blue ));
Stringformat strformat = new stringformat ();
Strformat. Alignment = stringalignment. Near;
If (this. _ shadow)
{
Brush b2 = new solidbrush (color. fromargb (90, 0, 0, 0 ));
G. drawstring (_ text, F, B2, _ left + 2, _ top + 1 );
}
// G. drawstring (_ text, F, B, new pointf (_ left, _ top), strformat );
If (_ text. Length <= 4)
{
G. drawstring (_ text, F, B, new pointf (140,150), strformat );
}
Else if (4 <_ text. Length & _ text. Length <= 8)
{
G. drawstring (_ text, F, B, new pointf (90,150), strformat );
}
Else if (8 <_ text. Length & _ text. Length <= 11)
{
G. drawstring (_ text. substring (0,4), F, B, new pointf (140,110), strformat );
G. drawstring (_ text. substring (4, _ text. Length-4), F, B, new pointf (90,150), strformat );
}
Else if (11 <_ text. Length & _ text. Length <= 18)
{
If (_ text. Length % 2 = 1)
{
G. drawstring (_ text. substring (0, (_ text. Length-1)/2), F, B, new pointf (60,110), strformat );
G. drawstring (_ text. substring (_ text. length-1)/2, _ text. length-(_ text. length-1)/2), F, B, new pointf (60,150), strformat );
}
Else
{
G. drawstring (_ text. substring (0, (_ text. Length-1)/2), F, B, new pointf (60,110), strformat );
G. drawstring (_ text. substring (_ text. length-1)/2, _ text. length-(_ text. length-1)/2), F, B, new pointf (60,150), strformat );
}
}
Else if (18 <_ text. Length & _ text. Length <= 25)
{
G. drawstring (_ text. substring (60,110), F, B, new pointf (), strformat );
G. drawstring (_ text. substring (8,9), F, B, new pointf (45,150), strformat );
G. drawstring (_ text. substring (17, _ text. Length-17), F, B, new pointf (60,190), strformat );
}
Else if (25 <_ text. Length & _ text. Length <= 31)
{
G. drawstring (_ text. substring (0, 6), F, B, new pointf (90, 70), strformat );
G. drawstring (_ text. substring (6, 8), F, B, new pointf (60,110), strformat );
G. drawstring (_ text. substring (14, 9), F, B, new pointf (45,150), strformat );
G. drawstring (_ text. substring (23, _ text. Length-23), F, B, new pointf (60,190), strformat );
}
Else if (31 <_ text. Length & _ text. Length <= 37)
{
G. drawstring (_ text. substring (0, 6), F, B, new pointf (90, 70), strformat );
G. drawstring (_ text. substring (6, 8), F, B, new pointf (60,110), strformat );
G. drawstring (_ text. substring (14, 9), F, B, new pointf (45,150), strformat );
G. drawstring (_ text. substring (23,8), F, B, new pointf (60,190), strformat );
G. drawstring (_ text. substring (31, _ text. Length-31), F, B, new pointf (90,230), strformat );
}
Else if (37 <_ text. length)
{
G. drawstring (_ text. substring (0, 6), F, B, new pointf (90, 70), strformat );
G. drawstring (_ text. substring (6, 8), F, B, new pointf (60,110), strformat );
G. drawstring (_ text. substring (14, 9), F, B, new pointf (45,150), strformat );
G. drawstring (_ text. substring (23,8), F, B, new pointf (60,190), strformat );
G. drawstring (_ text. substring (31, _ text. Length-31), F, B, new pointf (90,230), strformat );
}
Bitmap. Save (this. _ resultimage, imageformat. PNG );
Bitmap. Dispose ();
G. Dispose ();
Return true;
}
Catch
{
Return false;
}
}
}
Generate text on the specified image. The text format is adjustable.