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

时间:2010-08-05 19:11:24

标签: vb.net graphics imaging drawstring

我正在尝试使用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

2 个答案:

答案 0 :(得分:5)

这些变化似乎让它看起来很棒......

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

答案 1 :(得分:0)

正如您所指出的,这里的区别是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