绘图"透明"文本通过C#的图形,但在某种程度上,它原来绘制的文本是"失踪"所以它的所产生的图像中的透明 [英] 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
问题描述
我期待上绘制一个DC(图形 - 我使用C#) - 一个字符串,但我想从图像中显示的文本被删除,这样还剩下些什么本质上是一个切出的文字。
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.
有没有方式绘制类似的东西,或者我需要使用2个DC和BitBlt的与穷人或XOR或其他的某种组合(我几年前做过类似的事情,但不知道是否有一个easiery方式)?
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)?
推荐答案
您可以设置 Graphics.CompositingMode
到 CompositingMode.SourceCopy $ C 。$ C> - 但我不知道这将与透明的内容合作。
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:
- 绘制文本用红色画笔在黑色背景上的单独图像。
- 迭代文字图像和目标图像的每个像素...
- ...根据文本的图像红色分量手动设置目标图像的像素颜色的alpha值。
如果速度不是一个问题,你处理小位图,你可以使用 GetPixel
和的setPixel
方法。不过,我会建议使用 LockBits
访问的BitmapData
直接和工艺像素的字节数组。这是快速和不那么难以实现的解决方案,但你必须处理不安全的代码(或使用元帅
类)。
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).
这篇关于绘图"透明"文本通过C#的图形,但在某种程度上,它原来绘制的文本是"失踪"所以它的所产生的图像中的透明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!