/// <summary> /// Simple rotated text watermark /// </summary> private static void DrawRotatedTextWatermark() { using (var bitmap = new Bitmap("../../../../_Input/Venice.jpg")) using (var gr = bitmap.GetAdvancedGraphics()) using (var textPath = new Path()) { var watermarkColor = new RgbColor(255, 255, 255, 45); var fontSize = UnitConverter.ConvertPixelsToUnits(bitmap.DpiY, bitmap.Height, Unit.Point) / 100.0f; var plainText = new PlainText("Graphics Mill", gr.CreateFont("Arial", fontSize)); textPath.DrawText(plainText); var rect = new RectangleF(0, 0, bitmap.Width, bitmap.Height); var transform = new Matrix(); transform.RotateAt(-35, new PointF(rect.Left + rect.Width / 2, rect.Top + rect.Height / 2)); float offset = plainText.GetBlackBox().Width / 2; using (var tiledTextPath = TilePathText(textPath, GetInvertedRect(rect, transform), offset)) { tiledTextPath.ApplyTransform(transform); gr.FillPath(new SolidBrush(watermarkColor), tiledTextPath); } bitmap.Save("../../../../_Output/VeniceRotatedWatermark.jpg"); } }
/// <summary> /// Adds font to FontRegistry from file and shows its metrics /// </summary> private static void LoadFontAndMeasureText() { using (var bitmap = new Bitmap(600, 250, PixelFormat.Format24bppRgb, new RgbColor(255, 255, 255, 255))) using (var graphics = bitmap.GetAdvancedGraphics()) using (var fontRegistry = new CustomFontRegistry()) { var fontSize = 60f; // Load custom font fontRegistry.Add(@"../../../../_Input/Fonts/Lobster.ttf"); graphics.FontRegistry = fontRegistry; var font = graphics.CreateFont("lobster", fontSize); // Plain text metrics var plainText = new PlainText("plain text", font); plainText.Position = new System.Drawing.PointF(5, plainText.GetBlackBox().Height + 10); ShowTextPosition(plainText.String, plainText.Position, plainText.GetBlackBox()); DrawPlainTextWithMarkup(plainText, graphics); // Round text metrics var roundText = new RoundText("Art round text", font, new System.Drawing.PointF(50, 50)) { Bend = 0.9f }; roundText.Center = new System.Drawing.PointF(graphics.Width - roundText.GetBlackBox().Width / 2 - 15, graphics.Height - roundText.GetBlackBox().Height / 2 - 10); ShowTextPosition(roundText.String, roundText.Center, roundText.GetBlackBox()); DrawArtTextWithMarkup(roundText, graphics); bitmap.Save("../../../../_Output/FontLoadingAndTextMeasuring.png"); } }
private static void DrawEllipseAndText(Graphics graphics, byte alpha) { graphics.DrawEllipse(new Pen(new RgbColor(0x01, 0x97, 0xfd, alpha), 20f), -280, -135, 560, 270); var text = new PlainText("Graphics Mill", graphics.CreateFont("Arial", "Bold", 80f)); text.Alignment = TextAlignment.Center; text.Position = new System.Drawing.PointF(0f, text.GetBlackBox().Height / 2); text.Brush = new SolidBrush(new RgbColor(0xd4, 0x00, 0x00, alpha)); graphics.DrawText(text); }
/// <summary> /// Draws PlainText with ascender and descender lines /// </summary> private static void DrawPlainTextWithMarkup(PlainText text, Graphics graphics) { // Draw plain text, its black box, ascender, descender and position point graphics.DrawText(text); graphics.DrawRectangle(new Pen(RgbColor.Gray, 1f), text.GetBlackBox()); var stringMeasure = text.Font.MeasureString("plain text"); // GetBlackBox is cached inside, so there is almost no penalty graphics.DrawLine(new Pen(RgbColor.Blue, 1f), text.GetBlackBox().X, text.Position.Y - stringMeasure.Ascender, text.GetBlackBox().X + text.GetBlackBox().Width, text.Position.Y - stringMeasure.Ascender); graphics.DrawLine(new Pen(RgbColor.Green, 1f), text.GetBlackBox().X, text.Position.Y - stringMeasure.Descender, text.GetBlackBox().X + text.GetBlackBox().Width, text.Position.Y - stringMeasure.Descender); graphics.DrawLine(new Pen(RgbColor.IndianRed, 1f), text.GetBlackBox().X, text.Position.Y, text.GetBlackBox().X + text.GetBlackBox().Width, text.Position.Y); graphics.FillEllipse(new SolidBrush(RgbColor.Red), text.Position.X - 3, text.Position.Y - 3, 6, 6); }
protected override Text CreateAdvancedText(string text, Graphics graphics) { var font = CreateFont(graphics); var plainText = new PlainText(text, font) { Alignment = VOAligmnentToAdvanced(Alignment), Underline = Underline, Tracking = Tracking, Leading = Utils.EqualsOfFloatNumbers(0, Leading) ? font.Size * 1.2f : Leading, Paragraph = { OverlapLines = true }, Vertical = IsVertical }; var rectangle = MeasureText(graphics.DpiX); var blackbox = plainText.GetBlackBox(); var horizontalScale = !Utils.EqualsOfFloatNumbers(blackbox.Width, 0) ? (rectangle.Width - RenderingMargin * 2) / blackbox.Width : 1f; var verticalScale = !Utils.EqualsOfFloatNumbers(blackbox.Height, 0) ? (rectangle.Height - RenderingMargin * 2) / blackbox.Height : 1f; float alignmentShift = 0; if (Alignment != TextAlignment.Left) { alignmentShift = !IsVertical ? rectangle.Width - blackbox.Width : rectangle.Height - blackbox.Height; alignmentShift -= RenderingMargin * 2; if (Alignment == TextAlignment.Center) { alignmentShift /= 2; } } if (!IsVertical) { plainText.Transform.Translate(rectangle.X - blackbox.X + alignmentShift, rectangle.Y - blackbox.Y * verticalScale); } else { plainText.Transform.Translate(rectangle.X - blackbox.X * horizontalScale, rectangle.Y - blackbox.Y + alignmentShift); } if (!Utils.EqualsOfFloatNumbers(1, horizontalScale) || !Utils.EqualsOfFloatNumbers(1, verticalScale)) { plainText.Transform.Scale(horizontalScale, verticalScale); } return(plainText); }
private static Path CreatePath(Graphics graphics) { var path = new Path(); var font = graphics.CreateFont("Arial", "Bold", 40f); var text = new PlainText("GraphicsMill", font) { Alignment = TextAlignment.Center }; var blackBox = text.GetBlackBox(); text.Position = new System.Drawing.PointF(blackBox.Width, blackBox.Height * 2.3f); path.DrawEllipse(0, 0, blackBox.Width * 2, blackBox.Height * 4); path.DrawText(text); return path; }
/// <summary> /// Simple rotated text watermark /// </summary> private static void DrawRotatedTextWatermark() { using (var bitmap = new Bitmap("../../../../_Input/Venice.jpg")) using (var graphics = bitmap.GetAdvancedGraphics()) { // Modular int baseSize = 100; //Math.Min(bitmap.Width, bitmap.Height) / 10; var watermark = "Graphics Mill"; graphics.Transform.Translate(bitmap.Width / 2, bitmap.Height / 2); graphics.Transform.Rotate(-35f); // Render text var font = graphics.CreateFont("Arial", baseSize / 5); using (var text = new PlainText(watermark, font, new SolidBrush(new RgbColor(255, 255, 255, 45)))) { var baseSizeX = text.GetBlackBox().Width + baseSize / 2; var baseOffsetX = baseSizeX / 3; var halfDiagonal = (float)Math.Sqrt(Math.Pow(bitmap.Width, 2) + Math.Pow(bitmap.Height, 2)) / 2 + baseSize; float offsetX = 0; for (var y = -halfDiagonal; y < halfDiagonal; y += baseSize) { for (var x = -halfDiagonal - offsetX; x <= halfDiagonal; x += baseSizeX) { text.Position = new System.Drawing.PointF(x, y); graphics.DrawText(text); } offsetX = (offsetX + baseOffsetX) % baseSizeX; } } bitmap.Save("../../../../_Output/RotatedTextWatermark.jpg"); } }
/// <summary> /// Multiline text with different settings and additional grids /// </summary> private static void DrawMultilineWatermark() { using (var bitmap = new Bitmap("../../../../_Input/Venice.jpg")) using (var gr = bitmap.GetAdvancedGraphics()) using (var textPath = new Path()) { var watermarkColor = new RgbColor(255, 255, 255, 45); var fontSize = UnitConverter.ConvertPixelsToUnits(bitmap.DpiY, bitmap.Height, Unit.Point) / 40.0f; var watermarkText = string.Format("<span>Graphics Mill</span><br/><span style=\"font-size:{0}pt\">© {1} Aurigma, Inc.</span>", (int)(fontSize / 2), DateTime.Now.Year); var text = new PlainText(watermarkText, gr.CreateFont("Arial", fontSize)); text.Alignment = TextAlignment.Right; text.Leading = 1; textPath.DrawText(text); var rect = new RectangleF(0, 0, bitmap.Width, bitmap.Height); float offset = text.GetBlackBox().Width / 2; using (var tiledTextPath = TilePathText(textPath, rect, offset)) { gr.FillPath(new SolidBrush(watermarkColor), tiledTextPath); for (float i = offset; i < bitmap.Width; i += offset / 2) { gr.DrawEllipse(new Pen(watermarkColor, 3), -i, rect.Height - i, i * 2, i * 2); gr.DrawEllipse(new Pen(watermarkColor, 3), rect.Width - i, rect.Height - i, i * 2, i * 2); } } bitmap.Save("../../../../_Output/VeniceMultilineWatermark.jpg"); } }
/// <summary> /// Watermark with grid and horizontal text /// </summary> private static void RenderGridAndTextWatermark() { // Modular int baseSize = 100; //Math.Min(bitmap.Width, bitmap.Height) / 10; using (var bitmap = new Bitmap("../../../../_Input/Venice.jpg")) using (var graphics = bitmap.GetAdvancedGraphics()) { var mainWatermark = "Graphics Mill"; var subWatermark = "© 2014 Aurigma, Inc."; // Render grid var pen = new Pen(new RgbColor(255, 255, 255, 31), 5f); int diagonal = (int)Math.Sqrt(Math.Pow(bitmap.Width, 2) + Math.Pow(bitmap.Height, 2)); for (int i = baseSize; i < diagonal; i += baseSize) { graphics.DrawEllipse(pen, -i, bitmap.Height - i, i * 2, i * 2); graphics.DrawEllipse(pen, bitmap.Width - i, bitmap.Height - i, i * 2, i * 2); } // Render text var brush = new SolidBrush(new RgbColor(255, 255, 255, 45)); var mainFont = graphics.CreateFont("Arial", baseSize / 2); var subFont = graphics.CreateFont("Arial", baseSize / 5); using (var mainText = new PlainText(mainWatermark, mainFont, brush)) using (var subText = new PlainText(subWatermark, subFont, brush)) { var mainBlackBox = mainText.GetBlackBox(); var subBlackBox = subText.GetBlackBox(); var spacing = baseSize / 10; // We manually align text using text measure result var totalHeight = mainBlackBox.Height + spacing + subBlackBox.Height; var mainOffsetY = mainBlackBox.Height - totalHeight / 2; var mainOffsetX = -mainBlackBox.Width / 2; var subOffsetY = totalHeight / 2; var subOffsetX = mainOffsetX + mainBlackBox.Width - subBlackBox.Width; var baseSizeX = Math.Max(baseSize * 2, mainBlackBox.Width); var baseSizeY = baseSize * 2; int c = 0; for (float y = bitmap.Height / 2 - (bitmap.Height / 2 / baseSizeY + 1) * baseSizeY; y < bitmap.Height + baseSizeY; y += baseSizeY) { for (float x = bitmap.Width / 2 - (bitmap.Width / 2 / baseSizeX + 1) * baseSizeX; x < bitmap.Width + baseSizeX; x += baseSizeX) { // Render with chessboard style if (++c % 2 > 0) { continue; } mainText.Position = new System.Drawing.PointF(x + mainOffsetX, y + mainOffsetY); graphics.DrawText(mainText); subText.Position = new System.Drawing.PointF(x + subOffsetX, y + subOffsetY); graphics.DrawText(subText); } } } bitmap.Save("../../../../_Output/GridAndTextWatermark.jpg"); } }