//绘图源码(Begin)
using System.Drawing;
using System.Drawing.Imaging;
using System;
public class Overlay //yxy absolute
{
// "Old" signature overloaded to call new method w/extra parameters
public static Bitmap TextOverlay(string imgs, string OverlayText, Font OverlayFont,
Color OverlayColor, bool AddAlpha, bool AddShadow)
{
return TextOverlay(imgs, OverlayText, OverlayFont, OverlayColor, AddAlpha, AddShadow,
ContentAlignment.MiddleCenter, 0.8F);
}
// Draw text directly onto an image (scaled for best-fit)
// Written to be called from a module (just on the form for simplicity)
public static Bitmap TextOverlay( string imgs, string OverlayText, Font OverlayFont,
Color OverlayColor, bool AddAlpha, bool AddShadow,
System.Drawing.ContentAlignment Position, float PercentFill)
{
Bitmap bmp = new Bitmap(imgs);
if ( OverlayText != null && OverlayText.Length > 0 && PercentFill > 0 )
{
// create bitmap and graphics used for drawing
// "clone" image but use 24RGB format
Graphics g = Graphics.FromImage(bmp);
g.DrawImage(bmp, 0, 0);
int alpha = 255;
if ( AddAlpha )
{
// Compute transparency: Longer text should be less transparent or it gets lost.
alpha = 90 + (OverlayText.Length * 2);
if ( alpha >= 255 ) alpha = 255;
}
// Create the brush based on the color and alpha
SolidBrush b = new SolidBrush(Color.FromArgb(alpha, OverlayColor));
// Measure the text to render (unscaled, unwrapped)
StringFormat strFormat = StringFormat.GenericTypographic;
SizeF s = g.MeasureString(OverlayText, OverlayFont, 100000, strFormat);
// Enlarge font to specified fill (estimated by AREA)
//float zoom = OverlayFont.Size ;//(float)(Math.Sqrt(((double)(bmp.Width * bmp.Height) * PercentFill) / (double)(s.Width * s.Height)));
FontStyle sty = OverlayFont.Style;
Font f = new Font(OverlayFont.FontFamily, ((float)OverlayFont.Size), sty);
//Console.WriteLine(String.Format("Starting Zoom: {0}, Font Size: {1}, Alpha: {2}", zoom, f.Size, alpha));
// Measure using new font size, allow to wrap as needed.
// Could rotate the overlay at a 30-45 deg. angle (trig would give correct angle).
// Of course, then the area covered would be less than "straight" text.
// I'll leave those calculations for someone else....
int charFit;
int linesFit;
float SQRTFill = (float)(Math.Sqrt(PercentFill));
strFormat.FormatFlags = StringFormatFlags.NoClip; //|| StringFormatFlags.LineLimit || StringFormatFlags.MeasureTrailingSpaces;
strFormat.Trimming = StringTrimming.Word;
SizeF layout = new SizeF(((float)bmp.Width) * SQRTFill, ((float)bmp.Height) * 1.5F); // fit to width, allow height to go over<