C#/VB.NET:如何提高抗锯齿质量
问题:
在报告服务中,我需要有一个垂直的、从底部开始、从下到上、水平中间对齐的文本。
执行此操作的唯一方法是在代码中创建图像,并将该图像设置到标题列中。
请参阅下面的代码。
基本上,它工作得很好,只是抗锯齿质量相当糟糕。
我可以做些什么来改进它吗?
请参见下面的屏幕截图:
垂直文本不知何故苍白,而不是全黑,
而且文本周围的背景颜色也有涂抹。 它看起来比左侧的文本更粗体,但两者都有格式 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
Question:
In Reporting Service, I need to have a vertical, starting at bottom, bottom-to-top, horizontally middle aligned text.
The only way to do this is to create an image in code, and set this image into the title column.
See the code below.
Basically, it works fine, just that the anti-alias quality is pretty crappy.
Is there anything I can do to improve it ?
See below screenshot:
The vertical text is somehow pale, and not full black,
and also there is smearing all around the text, in the background color.
As well as it appears bolder than the text on the left, but both have format arial, size 8, bold
I've tried all other values of
System.Drawing.Text.TextRenderingHint.*
, as well as no anti-alias at all
but the current one seems to be the least crappy.
I've also tried to change the image format, to no avail:
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我遇到了这个问题,但仅限于某些浏览器。我认为这是浏览器问题(IE)。 Firefox 和 Chrome 似乎没问题。
最后,我决定手动构建 html 格式的报告,并使用 HighCharts 作为图形。 全部渲染为 PDF
然后我使用 wkHTMLtoPDF 将其 已经走了。我认为您可能会在 SQL Reporting Services 中遇到这些问题。
I was having this problem, but only in certain browsers. I think its a browser issue (IE). Firefox and Chrome seemed to be ok.
In the end I decided to manually build my reports in html and used HighCharts for the graphics. I then rendered it all out to PDF using wkHTMLtoPDF
Its come together quite well and the anti alias issues have gone. I think you might be stuck with these problems in SQL Reporting Services.
如果您使用的是 SQL Server Reporting Services 2008 R2,则无需创建映像:
选择文本框并设置以下属性:
清晰的文本,使用样式呈现。
If you are using SQL Server Reporting Services 2008 R2 you do not need to create an image:
Select the text box and set the following properties:
Crisp clean text, rendered with styles.
执行此操作的唯一方法是在代码中创建图像,并将该图像设置到标题列中。
如果您在浏览器中呈现此图像,则图像不是唯一的方法去。您可以使用 CSS 旋转文本,它在优秀的浏览器中看起来很棒,并且在IE下还可以< 9.
当然,你提到了 PDF,在这种情况下 CSS 技巧显然不适用。从问题中尚不清楚您生成的这张图像到底嵌入到什么中。
The only way to do this is to create an image in code, and set this image into the title column.
If you're rendering this in a browser, it's not true that an image is the only way to go. You can rotate the text with CSS, and it looks great in good browsers, and passable in IE < 9.
Of course, you mentioned PDF, in which case CSS tricks obviously wouldn't apply. It's just not clear from the question exactly what this image you're generating is being embedded into.