为什么 DrawString 看起来这么蹩脚?

发布于 2024-12-11 17:24:05 字数 1032 浏览 5 评论 0原文

我正在尝试向彩色图像添加文本比例。 agcScale.jpg 图像(如下)是顶部和底部的 2 个 winform 标签以及左侧和右侧的 2 个 winform 图片框。 使用完全相同的代码来生成左右图片框中的字符串,唯一的区别是 pictureBoxAgcVscale 仅包含字符串。 为什么 pictureBoxAgc 中的 DrawString 看起来不错,但 pictureBoxAgcVscale 中的 DrawString 看起来那么糟糕?我可能可以通过为每个像素执行 bmp.SetPixel 来修复 pictureBoxAgcVscale,但这似乎是修复此问题的错误方法。

agcScale.jpg

private void DisplayAgcVscale(double min, double max)
{
    var bmp = new Bitmap(pictureBoxAgcVscale.Width, pictureBoxAgcVscale.Height);
    var c = (max - min) / bmp.Height;
    using (var g = Graphics.FromImage(bmp))
    {
        var font = new Font("Microsoft Sans Serif", 8.25F);
        var y1 = bmp.Height / 10;
        for (var y = y1; y < bmp.Height; y += y1)
        {
            var agc = y * c + min;
            var text = agc.ToString("#0.000V");
            var h = bmp.Height - y - font.Height / 2;
            g.DrawString(text, font, Brushes.Black, 0, h);
        }
    }
    pictureBoxAgcVscale.Image = bmp;
}

I am trying to add a text scale to a color image.
The agcScale.jpg image (below) is 2 winform labels on the top and bottom and 2 winform pictureboxes on the left and right.
The exact same code was used to produce the strings in the right and left pictureboxes, the only difference is that pictureBoxAgcVscale contains only the strings.
Why does DrawString in pictureBoxAgc look fine but DrawString in pictureBoxAgcVscale look so bad? I can probably fix pictureBoxAgcVscale by doing a bmp.SetPixel for each pixel but that seems like the wrong way to fix this.

agcScale.jpg

private void DisplayAgcVscale(double min, double max)
{
    var bmp = new Bitmap(pictureBoxAgcVscale.Width, pictureBoxAgcVscale.Height);
    var c = (max - min) / bmp.Height;
    using (var g = Graphics.FromImage(bmp))
    {
        var font = new Font("Microsoft Sans Serif", 8.25F);
        var y1 = bmp.Height / 10;
        for (var y = y1; y < bmp.Height; y += y1)
        {
            var agc = y * c + min;
            var text = agc.ToString("#0.000V");
            var h = bmp.Height - y - font.Height / 2;
            g.DrawString(text, font, Brushes.Black, 0, h);
        }
    }
    pictureBoxAgcVscale.Image = bmp;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

乜一 2024-12-18 17:24:05

您正在透明背景上绘制黑色文本。抗锯齿像素从黑色褪色到黑色,别无选择,将字母变成斑点。它适用于左侧的文本,因为您首先绘制像素。

你忘记了 g.Clear()。

You are drawing black text on a transparent background. The anti-aliasing pixels are fading from black to black, no choice, turning the letters into blobs. It works for the text on the left because you draw the pixels first.

You forgot g.Clear().

似梦非梦 2024-12-18 17:24:05

我遇到了类似的问题,但在列表框中,并且通过清除矩形无法解决该问题。我必须应用“TextRenderingHint”:

e.Graphics.TextRenderingHint = TextRenderingHint.AntiAlias;
e.Graphics.DrawString(listText, myFont, myBrush, e.Bounds, StringFormat.GenericDefault);

I had a similar issue, but in a listbox, and it wasn't resolved by clearing the rectangle. I had to apply a "TextRenderingHint":

e.Graphics.TextRenderingHint = TextRenderingHint.AntiAlias;
e.Graphics.DrawString(listText, myFont, myBrush, e.Bounds, StringFormat.GenericDefault);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文