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.FontFami ly, minFontSize, f.Style)
End If
extent = g.MeasureString (s, f)
If maxFontSize <= minFontSize Then
Return f
End If
Dim hRatio As Single = layoutSize.Heig ht / extent.Height
Dim wRatio As Single = layoutSize.Widt h / extent.Width
Dim ratio As Single = Microsoft.Visua lBasic.IIf((hRa tio <
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.FontFami ly, 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.For mat32bppArgb)
Dim grGraphic As Graphics = Graphics.FromIm age(bmBitmap)
' random values for the colors
Dim rndNumber As Random = New
Random(DateTime .Now.Millisecon d)
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(Colo r.FromArgb(r, g, b))
' Dim myBrush as new SolidBrush(Colo r.White)
grGraphic.FillR ectangle(myBrus h, 0, 0, 100, 150)
myBrush.Dispose ()
' Rendering quality
grGraphic.Smoot hingMode = SmoothingMode.H ighQuality
grGraphic.Inter polationMode = InterpolationMo de.HighQualityB ilinear
grGraphic.TextR enderingHint =
System.Drawing. Text.TextRender ingHint.AntiAli asGridFit
' Font
Dim myFont As Font = New Font("Times New Roman", 25,
FontStyle.Bold) 'FontStyle.Regu lar
' text color
' Dim myPen As New SolidBrush(Colo r.FromArgb(r, g, b))
Dim myPen As New SolidBrush(Colo r.white)
' location target
Dim initSize As SizeF = new SizeF(vnMaxWidt h, 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.DrawS tring(vstrText, f2, myPen, p)
Finally
CType(f2, IDisposable).Di spose()
End Try
'Release Brush
myPen.Dispose()
'Release Initial Font
CType(myFont, IDisposable).Di spose()
' Affichage de l'image sur la page
bmBitmap.Save(" d:\trash\thumbn ail.jpg", ImageFormat.Jpe g)
'Dispose Font
myFont.Dispose( )
'Dispose bitmap
bmBitmap.Dispos e()
'Dispose graphical interface
grGraphic.Dispo se()
End Sub