473,396 Members | 2,090 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

VB.NET GDI Blurred Text problem

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
0 1269

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Pierre Jelenc | last post by:
I have a blog script that is a customized version of Word Press (wordpress.org) at http://www.web-ho.com/blog/ Because of the way it evolved, some of the CSS may not be the best, I have tended...
12
by: who be dat? | last post by:
I'm trying to make a webpage do the following: I want a user to be able to click on a given image (there will be more than one image). Upon clicking a given image, characters specific to that...
1
by: svijay | last post by:
hi I have got a strange problem. May I know any solution for this. Here is the detailed description about the problem We have got a mainframe system and also production and development...
14
by: Roger Withnell | last post by:
How to I find out what size text the browser is set to? Thanks in anticipation.
2
by: assgar | last post by:
Hi Multiple rows of dynamically created data is retrived from the database and displayed properly. When I get 50 rows displayed scrolling is necessary. The problem is when I scroll to see the...
0
by: manojmajhi | last post by:
suddenly all my internet images are appearing blurred. can anybody help...!
3
by: harig | last post by:
hi all.. how do i focus a textbox from a javascript i'e) <input type="text" size='50' name="txt1" maxlength='254' value="" onblur="checkDir()"> <input type="text" size='50'...
1
by: neovantage | last post by:
Hey all, I am using a PHP script which creates headings at run time in a sense at page execution. I am stuck a with a very little problem which i am sure i will have the solution from experts. ...
2
by: swethak | last post by:
Hi, I am getting the problem the problem with google map in Internet Explorer. This map worked fine in mozilla . When i opened the same map in Internet Explorer i am getting the error...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.