Graphics.Drawstring 在 PictureBox 中看起来不错,但在 Bitmap 中却很糟糕

发布于 2024-09-13 06:06:51 字数 1620 浏览 11 评论 0原文

我正在尝试使用 DrawString 将文本写入图像,然后将其旋转 90 度。它无论是在位图上还是直接在 PictureBox 上都可以正常工作,但最大的区别在于质量。绘制的 PictureBox 文本质量很好,而且看起来很漂亮。当我把它画在图像上时,它看起来很可怕而且是块状的。我做了一些改变,试图让它看起来更好,但它看起来并不像应有的那么好。

示例代码:

使用一个 Windows 窗体项目并在其上放置 2 个图片框和一个按钮,然后使用以下代码运行它以了解我的意思:

Public Class Form1
   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

       Dim g As System.Drawing.Graphics
       g = PictureBox1.CreateGraphics

       ' x.DrawRectangle(New Pen(Brushes.White, 200), New Rectangle(0, 0, 200, 200))
       g.TranslateTransform(10.0F, 0.0F)
       g.RotateTransform(90)
       g.DrawString("MM Components", New Font("Arial", 7, FontStyle.Regular), Brushes.DarkBlue, New PointF(0, 0))

       Dim g2 As System.Drawing.Graphics
       Dim img As New Bitmap(300, 300, Drawing.Imaging.PixelFormat.Format24bppRgb)
       g2 = Graphics.FromImage(img)

       g2.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
       g2.TextRenderingHint = Drawing.Text.TextRenderingHint.AntiAliasGridFit
       g2.CompositingQuality = Drawing2D.CompositingQuality.HighQuality
       ' img.SetResolution(150, 150)

       ' x.DrawRectangle(New Pen(Brushes.White, 200), New Rectangle(0, 0, 200, 200))
       g2.TranslateTransform(10.0F, 0.0F)
       g2.RotateTransform(90)
       g2.FillRectangle(Brushes.White, 0, 0, 300, 300)
       g2.DrawString("MM Components", New Font("Arial", 7, FontStyle.Regular), Brushes.DarkBlue, New PointF(0, 0))
       PictureBox2.Image = img

      'System.Threading.Thread.Sleep(20)
   End Sub
End Class

I am trying to use DrawString to write text as an image and then rotate it 90 degrees. It works fine with both a bitmap or directly on a PictureBox, but the big difference is in the quality. The PictureBox text drawn has great quality and looks nice. When I draw it on an image it looks horrible and blocky. I've made a few changes to try and get it to look nicer, but it doesn't look nearly as nice as it should.

Sample code:

Use a Windows Forms project and place 2 picture boxes on it and a button and run it with the following code to see what I mean:

Public Class Form1
   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

       Dim g As System.Drawing.Graphics
       g = PictureBox1.CreateGraphics

       ' x.DrawRectangle(New Pen(Brushes.White, 200), New Rectangle(0, 0, 200, 200))
       g.TranslateTransform(10.0F, 0.0F)
       g.RotateTransform(90)
       g.DrawString("MM Components", New Font("Arial", 7, FontStyle.Regular), Brushes.DarkBlue, New PointF(0, 0))

       Dim g2 As System.Drawing.Graphics
       Dim img As New Bitmap(300, 300, Drawing.Imaging.PixelFormat.Format24bppRgb)
       g2 = Graphics.FromImage(img)

       g2.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
       g2.TextRenderingHint = Drawing.Text.TextRenderingHint.AntiAliasGridFit
       g2.CompositingQuality = Drawing2D.CompositingQuality.HighQuality
       ' img.SetResolution(150, 150)

       ' x.DrawRectangle(New Pen(Brushes.White, 200), New Rectangle(0, 0, 200, 200))
       g2.TranslateTransform(10.0F, 0.0F)
       g2.RotateTransform(90)
       g2.FillRectangle(Brushes.White, 0, 0, 300, 300)
       g2.DrawString("MM Components", New Font("Arial", 7, FontStyle.Regular), Brushes.DarkBlue, New PointF(0, 0))
       PictureBox2.Image = img

      'System.Threading.Thread.Sleep(20)
   End Sub
End Class

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

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

发布评论

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

评论(2

堇年纸鸢 2024-09-20 06:07:13

正如您所指出的,这里的区别在于 ClearType。

阅读本文以获取有关它是什么及其工作原理的更多说明:
http://msdn.microsoft.com/en-us /library/dd183433(VS.85).aspx

另外还有 msdn 论坛上同一问题的另一份报告,其中包含修复程序:

http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/9a66c7a2-79ad- 4c9b-91cc-361ec72d13de

The difference here is, as you noted, ClearType.

Read this for more explanation on what it is and how it works:
http://msdn.microsoft.com/en-us/library/dd183433(VS.85).aspx

Also another report of the same issue on msdn forums, with included fix:

http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/9a66c7a2-79ad-4c9b-91cc-361ec72d13de

皓月长歌 2024-09-20 06:07:03

这些改变似乎让它看起来很不错......

    g2.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
    g2.TextRenderingHint = Drawing.Text.TextRenderingHint.ClearTypeGridFit
    g2.CompositingQuality = Drawing2D.CompositingQuality.HighQuality

These changes seem to make it look nice....

    g2.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
    g2.TextRenderingHint = Drawing.Text.TextRenderingHint.ClearTypeGridFit
    g2.CompositingQuality = Drawing2D.CompositingQuality.HighQuality
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文