C # text drawing source code

xiaoxiao2021-03-04  62

// Drawing source code (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 be less 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; // (math.sqrt ((double) (bmp.width * bmp.height) * percentfill) / (Double) / (double (S.Width * s.Height))))))))))))))); fontStyle style; font f = new font (overlayfont.fontfamily, (Float overlayFont.size), sty); ///console.writeline( 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 Calculation 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 * SQRTfil l, ((float) bmp.height * 1.5F); // Fit to Width, Allow Height to Go over Console.Writeline ("Target size: {0}, {1}", Layout.Width Layout.height); s = g.measureString (OverlayText, F, Layout, Strformat, Out Charfit, Out Linesfit);

//Ruce Size Until It Actually Fits ... // Most Text Only Has To Be Reduced 1 Or 2 Times. IF ((S.Height> (FLOAT) * SQRTFILL) || (S.Width> Layout.width) {DO {console.writeline (String.Format ("Reducing Font Size: Area Required = {0}, {1}", S.Width, S.HEight)); // Zoom = Math.max ((S.HEIGHT / ((FLOAT) BMP.HEIGHT * SQRTFILL)), S.Width / Layout.Width); // Zoom = 0; // f.size / zoom; // if (zoom> 16f ) Zoom = (floomp); // use a whole number to reduce "jaggies" // if (zoom> = f.size) zoom - = 1; f = new font (overlayfont.fontfamily Overlayfont.size, sty); s = g.measureString (OverlayText, F, Layout, Strformat, Out Charfit, Out linefit); // if (zoom <= 1) Break; // bail} while ((S.Height > (float) * sqrtfill) || (S.Width> layout.width));} console.writeline (String.Format ("Final Font Size: {0}, Area: {1}, { 2} "", f.size, s.width, s.hight);

// determine draw area based; switch (position) {copy contentAlignment.topleft: // = 1 Rect = new Rectanglef (f.size * 0.15f, (f.size * 0.1f), layout.width, (FLOAT) BMP.HEIGHT * SQRTFILL); Break; Case ContentAlignment.topcenter: // = 2 Rect = New Rectanglef ((Bmp.Width - S.Width) / 2F, (F.Size * 0.1f), Layout .Width, ((float) bmp.height) * sqrtfill); Break; Case ContentAlignment.topright: // = 4 Rect = New Rectanglef (bmp.width - s.width) - (f.size * 0.1f), (f.size * 0.1f), layout.width, ((float) bmp.height) * SQRTFILL); Break; Case ContentAlignment.middleleft: // = 16 huh? Where's 8? Rect = New Rectanglef (f.size * 0.15F, (BMP.HEIGHT - S.HEIGHT / 2F, LAYOUT.WIDTH, (FLOAT) BMP.HEIGHT * SQRTFILL); Break; Case ContentAlignment.middleCenter: // = 32 Rect = New Rectanglef ((BMP. Width - S.Width / 2F, (bmp.height - s.height) / 2F, Layout.WIDT H, ((Float) bmp.height * sqrtfill); Break; Case ContentAlignment.middleright: // = 64 Rect = New Rectanglef ((bmp.width - s.width) - (f.size * 0.1f), BMP.HEIGHT - S.HEIGHT / 2F, LAYOUT.WIDTH, (FLOAT) BMP.HEIGHT * SQRTFILL); Break; Case ContentAlignment.botTomLeft: // = 256 and 128 ?; Rect = New Rectanglef (f.size * 0.15F, (bmp.height - s.height) - (F.Size * 0.1f), layout.width, ((float) bmp.height) * sqrtfill; Break; Case ContentAlignment.bottomcenter: // = 512 Rect =

New Rectanglef ((Bmp.Width - S.Width / 2F, (bmp.height - s.hight) - (f.size * 0.1f), layout.width, ((float) bmp.height) * SQRTFILL); Break; Case ContentAlignment.bottomright: // = 1024 Rect = New Rectanglef ((Bmp.Width - S.Width) - (f.size * 0.1f), (bmp.height - s.ht) - (f.size * 0.1f), Layout.Width, (Float) bmp.height) * sqrtfill); Break; default: Rect = new Rectanglef ((bmp.width - s.width) / 2F, (bmp.height - s.Height ) / 2f, layout.width, (FLOAT) bmp.height * sqrtfill); break;} // add rendering hint (THX THOMAS) g.TextRenderingHint = System.drawing.Text.TextRenderingHint.Antialias;

if (AddShadow) {// Add "drop shadow" at half transparency and offset by 1/10 font size SolidBrush shadow = new SolidBrush (Color.FromArgb ((int) (alpha / 2.0), OverlayColor)); RectangleF sRect = new Rectanglef ((Float) Rect.x - (F.Size * 0.1f), (Float) Rect.y - (f.size * 0.1f), Rect.width, Rect.Height); g.drawstring (OverlayText, F Shadow, SRECT, STRFORMAT;

// Finally, Draw Centered text! g.drawstring (OverlayText, F, B, RECT, STRFORMAT);

// clean-up g.dispose (); b.dispose (); f.dispose (); return bmp;} else {// Nothing to overlay! regurgitate image return new bitmap;}}}

// Drawing Source Code (END)

public class OverlayOld {// "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 Formatgraphics g = graphics.fromimage (bmp); g.drawImage (BMP, 0, 0);

INT alpha = 255; if (addalpha) {// compute transparency: longer text be less 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 = 15; // (FLOAT) (((bmp.width * bmp.height) * percentfill) / (Double) (S.) Width * s.height))))))))))); fontstyle style; font f = new font (overlayfont.fontfamily, (float) overlayFont.size * zoom, sty); console.writeline (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 Overy 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 Console.Writeline (String.Format ("Target size: {0}, {1}", layout.width, layout.height); s = g.measureString (OverlayText, F , Layout, Strformat, Out Charfit, Out Linesfit;

//Ruce Size Until It Actually Fits ... // Most Text Only Has To Be Reduced 1 Or 2 Times. IF ((S.Height> (FLOAT) * SQRTFILL) || (S.Width> Layout.width) {DO {console.writeline (String.Format ("Reducing Font Size: Area Required = {0}, {1}", S.Width, S.HEight)); // Zoom = Math.max (((Float) * sqrtfill) * SQRTFILL)), S.Width / Layout.width; zoom = 15; // f.size / zoom; if (zoom> 16f) zoom = FLOAT (Math.floor (zoom)); // Use a whole number to reduce "jaggies" if (zoom> = f.size) zoom - = 1; f = new font (overlayfont.fontfamily, zoom, STY); s = g.measureString (OverlayText, F, Layout, Strformat, Out Charfit, Out Linesfit); if (ZOOM <= 1) Break; // Bail} While ((S.HEight> (Float) * * SQRTFILL) || (S.Width> layout.width));} console.writeline (String.Format ("Final Font Size: {0}, Area: {1}, {2}", F.Size, s. Width, s.height);

// determine draw area based; switch (position) {copy contentAlignment.topleft: // = 1 Rect = new Rectanglef (f.size * 0.15f, (f.size * 0.1f), layout.width, (FLOAT) BMP.HEIGHT * SQRTFILL); Break; Case ContentAlignment.topcenter: // = 2 Rect = New Rectanglef ((Bmp.Width - S.Width) / 2F, (F.Size * 0.1f), Layout .Width, ((float) bmp.height) * sqrtfill); Break; Case ContentAlignment.topright: // = 4 Rect = New Rectanglef (bmp.width - s.width) - (f.size * 0.1f), (f.size * 0.1f), layout.width, ((float) bmp.height) * SQRTFILL); Break; Case ContentAlignment.middleleft: // = 16 huh? Where's 8? Rect = New Rectanglef (f.size * 0.15F, (BMP.HEIGHT - S.HEIGHT / 2F, LAYOUT.WIDTH, (FLOAT) BMP.HEIGHT * SQRTFILL); Break; Case ContentAlignment.middleCenter: // = 32 Rect = New Rectanglef ((BMP. Width - S.Width / 2F, (bmp.height - s.height) / 2F, Layout.WIDT H, ((Float) bmp.height * sqrtfill); Break; Case ContentAlignment.middleright: // = 64 Rect = New Rectanglef ((bmp.width - s.width) - (f.size * 0.1f), BMP.HEIGHT - S.HEIGHT / 2F, LAYOUT.WIDTH, (FLOAT) BMP.HEIGHT * SQRTFILL); Break; Case ContentAlignment.botTomLeft: // = 256 and 128 ?; Rect = New Rectanglef (f.size * 0.15F, (bmp.height - s.height) - (F.Size * 0.1f), layout.width, ((float) bmp.height) * sqrtfill; Break; Case ContentAlignment.bottomcenter: // = 512 Rect =

New Rectanglef ((Bmp.Width - S.Width / 2F, (bmp.height - s.hight) - (f.size * 0.1f), layout.width, ((float) bmp.height) * SQRTFILL); Break; Case ContentAlignment.bottomright: // = 1024 Rect = New Rectanglef ((Bmp.Width - S.Width) - (f.size * 0.1f), (bmp.height - s.ht) - (f.size * 0.1f), Layout.Width, (Float) bmp.height) * sqrtfill); Break; default: Rect = new Rectanglef ((bmp.width - s.width) / 2F, (bmp.height - s.Height ) / 2f, layout.width, (FLOAT) bmp.height * sqrtfill); break;} // add rendering hint (THX THOMAS) g.TextRenderingHint = System.drawing.Text.TextRenderingHint.Antialias;

if (AddShadow) {// Add "drop shadow" at half transparency and offset by 1/10 font size SolidBrush shadow = new SolidBrush (Color.FromArgb ((int) (alpha / 2.0), OverlayColor)); RectangleF sRect = new Rectanglef ((Float) Rect.x - (F.Size * 0.1f), (Float) Rect.y - (f.size * 0.1f), Rect.width, Rect.Height); g.drawstring (OverlayText, F Shadow, SRECT, STRFORMAT;

// Finally, Draw Centered text! g.drawstring (OverlayText, F, B, RECT, STRFORMAT);

// clean-up g.dispose (); b.dispose (); f.dispose (); return bmp;} else {// Nothing to overlay! regurgitate image return new bitmap;}}}

转载请注明原文地址:https://www.9cbs.com/read-31721.html

New Post(0)