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)?
If you want to cut sth out of the image, you can do it like this:
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.
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.
You could set
Graphics.CompositingMode
toCompositingMode.SourceCopy
- but I'm not sure if that will work with transparent content.The way around this is to:
If speed is not a concern and you deal with small bitmaps, you can use
GetPixel
andSetPixel
methods. But I would recommend usingLockBits
to accessBitmapData
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 theMarshal
class).LockBits reference on MSDN
Marshal reference on MSDN
You might want to try:
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.