无论图像颜色如何,在图像上绘制可见文本

时间:2015-02-26 13:26:50

标签: c# winforms graphics

我想使用DrawString()在24bpp图像上打印文本。问题是如果我选择白色作为文本颜色,文本在较亮的图像区域几乎不可见。如果我选择红色作为文本颜色,则文本在包含更多红色的图像区域中几乎不可见。等等。

我想要实现的是文本在任何情况下都是可见的。我尝试的是用粗体字体绘制文本,然后使用相同的字体绘制文本,但再次定期。但粗体文字略宽,所以这不是解决方案。

但是解决方案是什么?有没有?

谢谢!

2 个答案:

答案 0 :(得分:2)

如果图像没有太大差异,以至于可以假设您将在具有非常纯色的区域中绘制字符串,则可以使用以下解决方案。

您可以先使用算法从另一种颜色计算最不同的颜色,如下所示:

public static byte MostDifferent (byte original) {
    if(original < 0x80) {
        return 0xff;
    } else {
        return 0x00;
    }
}
public static Color MostDifferent (Color original) {
    byte r = MostDifferent(original.R);
    byte g = MostDifferent(original.G);
    byte b = MostDifferent(original.B);
    return Color.FromArgb(r,g,b);
}

既然我们已经这样做了,我们必须计算将绘制string的区域内的平均颜色。您可以在Bitmap级别执行此操作:

public static unsafe Color AverageColor (Bitmap bmp, Rectangle r) {
    BitmapData bmd = bmp.LockBits (r, ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
    int s = bmd.Stride;
    int cr = 0;
    int cg = 0;
    int cb = 0;
    int* clr = (int*)(void*)bmd.Scan0;
    int tmp;
    int* row = clr;
    for (int i = 0; i < r.Height; i++) {
        int* col = row;
        for (int j = 0; j < r.Width; j++) {
            tmp = *col;
            cr += (tmp >> 0x10) & 0xff;
            cg += (tmp >> 0x08) & 0xff;
            cb += tmp & 0xff;
            col++;
        }
        row += s>>0x02;
    }
    int div = r.Width * r.Height;
    int d2 = div >> 0x01;
    cr = (cr + d2) / div;
    cg = (cg + d2) / div;
    cb = (cb + d2) / div;
    bmp.UnlockBits (bmd);
    return Color.FromArgb (cr, cg, cb);
}

最后,算法首先测量将绘制字符串的矩形,然后确定最不同的颜色,最后用该颜色绘制字符串:

public static void DrawColorString (this Graphics g, Bitmap bmp, string text, Font font, PointF point) {
    SizeF sf = g.MeasureString (text, font);
    Rectangle r = new Rectangle (Point.Truncate (point), Size.Ceiling (sf));
    r.Intersect (new Rectangle(0,0,bmp.Width,bmp.Height));
    Color brsh = MostDifferent (AverageColor (bmp, r));
    g.DrawString (text, font, new SolidBrush (brsh), point);
}

现在您可以将方法调用为:

Bitmap bmp = new Bitmap("Foo.png");
Graphics g = Graphics.FromImage(bmp);
g.DrawColorString (bmp, "Sky", new Font ("Arial", 72.0f), new PointF (600.0f, 150.0f));
g.DrawColorString (bmp, "Sand", new Font ("Arial", 72.0f), new PointF (600.0f, 450.0f));
bmp.Save ("result.jpg");

此结果例如:

enter image description here

答案 1 :(得分:2)

我建议将文本绘制两次(或三次):一旦在黑暗中,一次在明亮的颜色中,将1或2像素分开放置&amp;向左,取决于字体大小。

如果您的字体非常小并且在许多笔划上只有1个像素宽,那么使用三重绘图将有所帮助:两个外部的一个颜色(黑色)和第三个一个在中间(明亮)

Random R = new Random();
private void pictureBox2_Paint(object sender, PaintEventArgs e)
{
    // using a loop for create random locations..:
    for (int i=0; i< 33; i++)
    {
        Point pt = new Point(R.Next(pictureBox2.ClientSize.Width), 
                             R.Next(pictureBox2.ClientSize.Width));

        e.Graphics.DrawString("Hello World", Font, Brushes.Black, pt.X - 1, pt.Y - 1);
        e.Graphics.DrawString("Hello World", Font, Brushes.Black, pt.X + 1, pt.Y + 1);
        e.Graphics.DrawString("Hello World", Font, Brushes.White, pt.X , pt.Y);
    }
}

以下是三重绘图的示例

enter image description here

(请注意,原版看起来比我在浏览器中看到的更加清晰。你可能想下载它来查看..)

如果您的图像可能包含非常多的噪音并且字体必须相当小,那么在文本后面添加纯色背景会更好。