C#/ VB.NET:如何提高抗锯齿质量

时间:2011-08-16 15:25:15

标签: c# .net vb.net reporting-services antialiasing

问题:
在Reporting Service中,我需要一个垂直的,从底部开始,从下到上,水平对齐的文本。

执行此操作的唯一方法是在代码中创建图像,并将此图像设置为标题列。

请参阅以下代码
基本上,它工作正常,只是抗锯齿质量很糟糕 我能做些什么来改善它吗? 见下面的截图:

SSRS Problem

垂直文字有点苍白,而不是全黑,
并且在背景颜色的文本周围也有涂抹。 它看起来比左边的文字更大胆,但两者都有格式arial,大小8,粗体

我已经尝试了所有其他值 System.Drawing.Text.TextRenderingHint。* ,以及根本没有反别名

但是现在似乎是最糟糕的。 我也试图改变图像格式,但无济于事:

Function LoadImage2(ByVal sImageText As String, ByVal sImageTextMax As String) As System.Drawing.Image
    sImageTextMax = sImageTextMax.PadRight(15)
    Dim iFontSize As Integer = 8 '//Change this as needed
    Dim bmpImage As New System.Drawing.Bitmap(1, 1)
    Dim iWidth As Integer = 0
    Dim iHeight As Integer = 0
    Dim bgColor As System.Drawing.Color = System.Drawing.Color.LemonChiffon ' LightGray
    Dim TextColor As System.Drawing.Color = System.Drawing.Color.Black

    Dim fsFontStyle As System.Drawing.FontStyle = System.Drawing.FontStyle.Bold

    '// Create the Font object for the image text drawing.
    Dim MyFont As New System.Drawing.Font("Arial", iFontSize, fsFontStyle, System.Drawing.GraphicsUnit.Point)
    '// Create a graphics object to measure the text's width and height.
    'Graphics(MyGraphics = Graphics.FromImage(bmpImage))
    Dim MyGraphics As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(bmpImage)
    '// This is where the bitmap size is determined.
    iWidth = MyGraphics.MeasureString(sImageTextMax, MyFont).Width
    iHeight = MyGraphics.MeasureString(sImageTextMax, MyFont).Height
    '// Create the bmpImage again with the correct size for the text and font.
    'bmpImage = New Drawing.Bitmap(bmpImage, New Drawing.Size(iWidth, iHeight))

    'inches = pixels / dpi
    'pixel = inches * dpi
    '1 centimeter = 0.393700787 inch
    'pixel = cm * 0.393700787  * dpi


    ' vice-versa, because 270° turn
    iHeight = 1 * 0.393700787 * bmpImage.HorizontalResolution 'x DPI
    iWidth = 2.25 * 0.393700787 * bmpImage.VerticalResolution 'y DPI


    bmpImage = New System.Drawing.Bitmap(bmpImage, New System.Drawing.Size(iHeight, iWidth))

    '// Add the colors to the new bitmap.
    MyGraphics = System.Drawing.Graphics.FromImage(bmpImage)
    MyGraphics.Clear(bgColor)
    MyGraphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias

    MyGraphics.TranslateTransform(0, iWidth)
    MyGraphics.RotateTransform(270)

    Dim iTextStartX As Single = 2
    Dim iTextStartY As Single = CSng(iHeight) / CSng(2.0) - CSng(iFontSize) / CSng(2.0)
    iTextStartY -= 2

    MyGraphics.DrawString(sImageText, MyFont, New System.Drawing.SolidBrush(TextColor), iTextStartX, iTextStartY)
    MyGraphics.Flush()
    Return bmpImage
End Function


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Me.PictureBox1.Image = LoadImage2("test", "")
End Sub



' This piece is only needed in reporting service itselfs
Function LoadImage(ByVal strText As String) As Byte()
    Dim ThisImageFormat As System.Drawing.Imaging.ImageFormat = System.Drawing.Imaging.ImageFormat.Jpeg


    Dim stream As System.IO.MemoryStream = New System.IO.MemoryStream
    Dim bitmapBytes As Byte()
    Dim bmpImage As System.Drawing.Image = LoadImage2(strText, "")


    bmpImage.Save(stream, ThisImageFormat)
    bitmapBytes = stream.ToArray
    stream.Close()
    bmpImage.Dispose()
    Return bitmapBytes
End Function

3 个答案:

答案 0 :(得分:1)

我遇到了这个问题,但仅限于某些浏览器。我认为这是一个浏览器问题(IE)。 Firefox和Chrome似乎没问题。

最后,我决定在html中手动构建报告,并使用HighCharts作为图形。然后我使用wkHTMLtoPDF

将其全部渲染为PDF

它很好地结合在一起,反别名问题已经消失了。我想你可能会在SQL Reporting Services中遇到这些问题。

答案 1 :(得分:1)

  

执行此操作的唯一方法是在代码中创建图像,并将此图像设置为标题列。

如果您使用的是SQL Server Reporting Services 2008 R2,则无需创建映像: 选择文本框并设置以下属性:

TextAlign: Left
VerticalAlign: Middle
WritingMode: Rotate 270

清晰干净的文字,用风格渲染。

答案 2 :(得分:0)

执行此操作的唯一方法是在代码中创建图像,并将此图像设置为标题列。

如果你在浏览器中渲染它,那么图像是唯一可行的方法。您可以rotate the text with CSS,它在良好的浏览器中看起来很棒,并且在IE中可以通过< 9。

当然,你提到了PDF,在这种情况下,CSS技巧显然不适用。从问题中确切地说,你正在生成的这个图像是什么,这一点并不清楚。