Giter VIP home page Giter VIP logo

draw-string-line-height's Introduction

This package contains extension methods to the System.Drawing.Graphics class to allow for measuring and drawing text with a custom line height and font.

The project source code is located in the src folder and the unit tests project for the library is located in the tests directory.

Requirements

This package targets netstandard2.0 so it is compatible with netcoreapp2.2^ and net461^.

Additionally, you'll probably need to install tools like a dotnet sdk and runtime on your computer

Running tests

In order to run the unit tests for yourself:

  1. Change directory to the root of this repository (where the .sln file is located)
  2. Run dotnet test

Installation

You can download the package from nuget.org

Usage

Firstly, you'll need to use the NarcityMedia.DrawStringLineHeight namespace to have access to the extensions methods to the System.Drawing.Graphics class.

Code Example

public static Bitmap GenerateImage()
{
    string text = "Some pretty cooooooool text";
    // PAY ATTENTION to the GraphicsUnit you use, it should match that of the PageUnit property of your System.Drawing.Graphics instance
    Font arialFont = new Font("Arial", 32, GraphicsUnit.Pixel);

    SolidBrush brush = new SolidBrush(Color.Black);

    int maxWidth = 300;
    int customLineHeight = 20; // px
    // The variable g here represents a globaly available 'dummy' static instance of System.Drawing.Graphics (it's not important)
    SizeF textSize = g.MeasureStringLineHeight(text, arialFont, maxWidth, customLineHeight)
    Bitmap newImage = new Bitmap(1080, 1080);
    using (Graphics gr = Graphics.FromImage(newImage))
    {
        gr.Clear(Color.White);

        Point textOrigin = new Point(10, 10);
        gr.DrawString(text, arialFont, brush, maxWidth, customLineHeight,
                                        new Rectangle(textOrigin, new Size(maxWidth, 1080)), StringFormat.GenericDefault);

        return newImage;
    }
}

API

MeasureStringLineHeight

public static SizeF MeasureStringLineHeight(this Graphics that, string text, Font font, int maxWidth, int lineHeight)

Summary

Measures the space taken up by a given text for a given font, width and line height

Parameters

String text : The text to measure

System.Drawing.Font font : Instance of System.Drawing.Font to use

int maxWidth : A positive number (grater than zero) that represents the width in which the text must fit (will affect text wrapping)

int lineHeight : The custom line height used to calculate the text size

Returns System.Drawing.SizeF

The size taken up by the given text with the given parameters

Remarks

This extension method isn't an overload of the System.Drawing.Graphics.MeasureString(string, Font) method because the method had conflicting overloads and it ended up being confusing

DrawString

public static void DrawString(this Graphics that, string text, Font font, Brush brush, int maxWidth,
                                            int lineHeight, RectangleF layoutRectangle, StringFormat format)

Summary

Draws the specified text string in the specified rectangle with the specified font, brush, width, line height and format

Parameters

String text : The text to measure

System.Drawing.Font font : Instance of System.Drawing.Font to use

System.Drawing.Brush brush : The brush to use to paint the text

int lineHeight : The custom line height used to calculate the text size

System.Drawing.RectangleF layoutRectangle : structure that specifies the location of the drawn text

System.Drawing.StringFormat format : Specifies formatting attributes, such as line spacing and alignment, that are applied to the drawn text.

GetWrappedLines

public static IEnumerable<string> GetWrappedLines(this Graphics that, string text, Font font, double maxWidth = double.PositiveInfinity)

Summary

Calculates how the given text will have to be wrapped in order to fit in the given width.

This method is used by the library internally to perform text wrapping but you can use it if you need it.

Parameters

String text : The text to measure

System.Drawing.Font font : Instance of System.Drawing.Font to use

int maxWidth : A positive number (grater than zero) that represents the width in which the text must fit (will affect text wrapping)

Returns IEnumerable<string>

A string enumerable where each string is a substring of the text param and represents a line resulting of the text wrapping

draw-string-line-height's People

Contributors

gacardinal avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.