473,466 Members | 1,388 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 1270

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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...
1
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.