By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,953 Members | 1,524 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,953 IT Pros & Developers. It's quick & easy.

VB.NET GDI Blurred Text problem

P: n/a
I'm new in GDI programming and I am facing the following problem.
I try to dynamically create "thumbnail"-like images (JPEG) in which I
need to draw text.
This works fine, except that the text is always a little bit
"blurred".
Here is the code, could someone give me some help?
Thanks in advance,
Didier

Public Shared Function AppropriateFont(ByVal g As Graphics, ByVal
minFontSize As Single, ByVal maxFontSize As Single, ByVal layoutSize
As Size, ByVal s As String, ByVal f As Font, ByRef extent As SizeF) As
Font
If maxFontSize = minFontSize Then
f = New Font(f.FontFamily, minFontSize, f.Style)
End If
extent = g.MeasureString(s, f)
If maxFontSize <= minFontSize Then
Return f
End If
Dim hRatio As Single = layoutSize.Height / extent.Height
Dim wRatio As Single = layoutSize.Width / extent.Width
Dim ratio As Single = Microsoft.VisualBasic.IIf((hRatio <
wRatio),hRatio,wRatio)
Dim newSize As Single = f.Size * ratio
If newSize < minFontSize Then
newSize = minFontSize
Else
If newSize maxFontSize Then
newSize = maxFontSize
End If
End If
f = New Font(f.FontFamily, newSize, f.Style)
extent = g.MeasureString(s, f)
Return f
End Function
'--------------------------------------------------------------------------------------------------------

Public Sub Generate(ByVal vstrText As String, ByVal vnMaxWidth As
Integer, ByVal vnMaxHeight As Integer)

' Image Container
Dim bmBitmap As Bitmap = New Bitmap(100, 150,
PixelFormat.Format32bppArgb)
Dim grGraphic As Graphics = Graphics.FromImage(bmBitmap)

' random values for the colors
Dim rndNumber As Random = New
Random(DateTime.Now.Millisecond)
Dim r, g, b As Integer
r = rndNumber.Next(0, 200)
g = rndNumber.Next(0, 200)
b = rndNumber.Next(0, 200)

' Background
Dim myBrush As New SolidBrush(Color.FromArgb(r, g, b))
' Dim myBrush as new SolidBrush(Color.White)
grGraphic.FillRectangle(myBrush, 0, 0, 100, 150)
myBrush.Dispose()

' Rendering quality
grGraphic.SmoothingMode = SmoothingMode.HighQuality
grGraphic.InterpolationMode = InterpolationMode.HighQualityBilinear
grGraphic.TextRenderingHint =
System.Drawing.Text.TextRenderingHint.AntiAliasGri dFit

' Font
Dim myFont As Font = New Font("Times New Roman", 25,
FontStyle.Bold) 'FontStyle.Regular

' text color
' Dim myPen As New SolidBrush(Color.FromArgb(r, g, b))
Dim myPen As New SolidBrush(Color.white)

' location target
Dim initSize As SizeF = new SizeF(vnMaxWidth, vnMaxHeight)
Dim newSize As SizeF

Dim f2 As Font = AppropriateFont(grGraphic, 5, 50,
initSize.ToSize(), vstrText, myFont, newSize)

Try
Dim p As PointF = New PointF((100 - newSize.Width) / 2, 15 +
(vnMaxHeight - newSize.Height) / 2)
grGraphic.DrawString(vstrText, f2, myPen, p)
Finally
CType(f2, IDisposable).Dispose()
End Try

'Release Brush
myPen.Dispose()

'Release Initial Font
CType(myFont, IDisposable).Dispose()

' Affichage de l'image sur la page
bmBitmap.Save("d:\trash\thumbnail.jpg", ImageFormat.Jpeg)

'Dispose Font
myFont.Dispose()

'Dispose bitmap
bmBitmap.Dispose()

'Dispose graphical interface
grGraphic.Dispose()
End Sub
Dec 17 '07 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.