Drawing “transparent” text via C# Graphics, but in a way that it turns the drawn text to be “missing” so it's transparent in the resulting image

I am looking to draw a string on a DC (Graphics - I am using C#) - but I want the drawn text to be "deleted" from the image so that what's left is essentially a cut-out of the text.

If I was to DrawString with a transparent brush obviously nothing would have happened.

Is there a way of drawing something like that, or do I need to use 2 DCs and BitBlt with some combination of NOTs or XOR or whatever (I did similar things years ago, but was wondering if there's an easiery way)?


You could set Graphics.CompositingMode to CompositingMode.SourceCopy - but I'm not sure if that will work with transparent content.

The way around this is to:

  1. Draw your text to a separate image using red brush over black background.
  2. Iterate over each pixel of text image and target image...
  3. ...manually set target image pixel color's Alpha value according to text's image Red component.

If speed is not a concern and you deal with small bitmaps, you can use GetPixel and SetPixel methods. But I would recommend using LockBits to access BitmapData directly and process pixels in a byte array. This is fast and not-so-hard-to-implement solution, although you'll have to deal with "unsafe" code (or use the Marshal class).

LockBits reference on MSDN

Marshal reference on MSDN

If you want to cut sth out of the image, you can do it like this:

Drawing2D.GraphicsContainer c = graphics.BeginContainer();
GraphicsPath p = new GraphicsPath();


graphics.SetClip(p, CombineMode.Xor);
graphics.DrawImage(this.Image, this.Location);


It's like the solution farther above, but first without SetClip the Graphics-Object doesn't get updated, and second, you need to use Xor.

Note, that making a GraphicsContainer is optional, but makes encapsulation easier, especially if you work with transforms.

You might want to try:

// g is your Graphics object
using (var path = new GraphicsPath())
    path.AddString(.... );
// Do your other painting here

Sounds like this may require you to make a new image and draw the old one on top. I'm not to sure of your situation though.

From a quick search it unfortunately doesn't look like System.Drawing supports XOR drawing and you need to use unmanaged calls to GDI+. This answer to a similar question links to a page that might explain how to do that - the interesting file is here.

Hope this helps.

Need Your Help

jQuery UI Autocomplete: Clear Previous Search Term

javascript jquery jquery-ui autocomplete

On the keydown event of the jQuery UI Autocomplete widget the default case statement has the following code:

what data structure to use in my case?

java data-structures

A FileManager Class has a static filed to hold a file collection, this collection may contains files or folders or both , a folder may contains files or folders or both, the FileManager Class conta...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.