所以,我有一个视频源的快照,我将它放入一个 Image 中,为其抓取一个 Graphics 对象,然后在图像的右下角绘制一个时间戳。到目前为止没有问题。但是,我不能保证文本后面会是什么颜色,所以无论我使用什么画笔,它几乎肯定会与它所绘制的某些图像发生冲突,从而使文本无法阅读。
我想知道是否有人知道一种方法(.net 中的方法或不错的算法),用于根据字符串背后的图像确定字符串的最佳颜色。
干杯
所以,我有一个视频源的快照,我将它放入一个 Image 中,为其抓取一个 Graphics 对象,然后在图像的右下角绘制一个时间戳。到目前为止没有问题。但是,我不能保证文本后面会是什么颜色,所以无论我使用什么画笔,它几乎肯定会与它所绘制的某些图像发生冲突,从而使文本无法阅读。
我想知道是否有人知道一种方法(.net 中的方法或不错的算法),用于根据字符串背后的图像确定字符串的最佳颜色。
干杯
just draw the string 5 times.
One time 1(or2) pixels to the left in black
One time 1(or2) pixels to the right in black
One time 1(or2) pixels above it in black
One time 1(or2) pixels below it in black
and the final time in white on the place where you want it
唯一可靠的方法是使用对比鲜明的轮廓。
回到 Commodore 64 sprite 图形的时代,如果您想要在任何背景下脱颖而出,您可以使用 XOR blitting。有人将此称为“反向视频”。
您可以使用 以这种方式绘制线条ControlPaint.DrawReversibleLine
,但这不适用于文本。
这篇CodeProject 文章展示了如何使用 interop to 创建 XOR 画笔gdi32.dll
。
这可能是reinier 答案的多种变化。
有关最后一个选项的一些示例,请查看SlideShare 上的高级 OSM 制图中的幻灯片 18 和 21。
以下片段显示了如何反转颜色(背景),然后应用 Dinah 的建议使用 Graphics.DrawString() 创建背景。
private static Color InvertColor(Color c)
{
return Color.FromArgb(255 - c.R, 255 - c.G, 255 - c.B);
}
// In the following, constants and inplace vars can be parameters in your code
const byte ALPHA = 192;
var textColor = Color.Orange;
var textBrush = new SolidBrush(Color.FromArgb(ALPHA, textColor));
var textBrushBkg = new SolidBrush(Color.FromArgb(ALPHA, InvertColor(textColor)));
var font = new Font("Tahoma", 7);
var info = "whatever you wanna write";
var r = new Rectangle(10, 10, 10, 10);
// write the text
using (var g = Graphics.FromImage(yourBitmap))
{
g.Clear(Color.Transparent);
// to avoid bleeding of transparent color, must use SingleBitPerPixelGridFit
g.TextRenderingHint = TextRenderingHint.SingleBitPerPixelGridFit;
// Draw background for text
g.DrawString(info, font, textBrushBkg, r.Left - 1, r.Top - 1);
g.DrawString(info, font, textBrushBkg, r.Left + 1, r.Top + 1);
g.DrawString(info, font, textBrushBkg, r.Left + 1, r.Top - 1);
g.DrawString(info, font, textBrushBkg, r.Left - 1, r.Top + 1);
// Draw text
g.DrawString(info, font, textBrush, r.Left, r.Top);
}