BoltBait.com

Drawing Text in a GPU Drawing Plugin

Let's draw some text in a Paint.NET plugin


I have not written this tutorial yet, but here's some code to get you started:

protected override unsafe void OnDraw(IDeviceContext deviceContext)
{
	// maintain background
    deviceContext.DrawImage(Environment.SourceImage);
	
	// get the size of our canvas
    RectFloat sourceBounds = new RectFloat(Point2Float.Zero, Environment.Document.Size);
	
	// create a red brush in order to render red text
    ISolidColorBrush solidFillBrush = deviceContext.CreateSolidColorBrush(LinearColors.Red);
	
	// set the antialiasing modes
    deviceContext.AntialiasMode = AntialiasMode.PerPrimitive;
    deviceContext.TextAntialiasMode = TextAntialiasMode.Grayscale;
	
	// set the text rendering mode
    deviceContext.UseTextRenderingMode(TextRenderingMode.Outline);
	
	// prepare for rendering text
    IDirectWriteFactory textFactory = this.Services.GetService<IDirectWriteFactory>();
    IGdiFontMap fm = textFactory.GetGdiFontMap();
	
	// select your font to look at the font properties
    FontProperties fp = fm.TryGetFontProperties("Arial");
	
	// use your font properties to create a font
    ITextFormat textFormat = textFactory.CreateTextFormat(
        fp.FontFamilyName, // font family name
        null,
        FontWeight.Bold, // font weight
        FontStyle.Normal, // font style
        fp.Stretch, // how to stretch the font
        72); // size in points
		
	// render the text
    deviceContext.DrawText(
		"Paint.NET Rocks!", // the actual text to render
		textFormat, // format of text defined above
		sourceBounds, // text location
		solidFillBrush, // our red brush, from above
		DrawTextOptions.None); // Options
}

Go ahead and press Ctrl+P to preview your effect.


Finishing Up

Once you've got that working, remember to save your script! I wouldn't want you to lose any of your hard work.

"File > Save" your text.cs file.

If you'd like to make that plugin a permanent part of your Paint.NET installation, read:

How to Build a DLL from a CodeLab script
How to install a DLL into Paint.NET


What's Next?

Now that you know a bit about rendering text, let's dig a little deeper and design and implement a more complicated effect that adds a UI to our effect.


What's Next?

Head back to the Tutorial Index to learn something else



Donate

The best way to say "Thanks" for teaching you something here, is to fill out this form. It uses PayPal to process your donation. You don't need a PayPal account, just a credit/debit card will do. If PayPal doesn't work for you, no need to worry--just enjoy the tutorials for free!

$
Thank you for your donation. I don't get many, so you can be sure I really appreciate yours!



 

 
 

News



CodeLab 6.12 Released
(February 11, 2024)
This latest release of CodeLab for Paint.NET includes the ability to write GPU accelerated plugins.
More...

Double-Six Dominoes 3.1
(May 10, 2021)
This long-awaited refresh of the most popular dominoes game on Download.com is now available!
More...

HTML Editor 1.5 Released
(March 31, 2016)
This latest release is a complete rewrite adding a wysiwyg editor mode and a much improved UI.
More...