473,563 Members | 2,633 Online
Bytes | Software Development & Data Engineering Community
+ 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.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
Dec 17 '07 #1
0 1282

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

Similar topics

0
2415
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 to keep the default style sheet and add my modifications. The style sheet itself is at http://www.web-ho.com/blog/layout2b.css I use Mozilla 1.4.1...
12
8341
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 image will be inserted into a textbox which is actually a text area. If the cursor is in the textbox/textarea then the characters are to be inserted...
1
2395
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 server.
14
3974
by: Roger Withnell | last post by:
How to I find out what size text the browser is set to? Thanks in anticipation.
2
1511
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 items at the bottom of the list the rows get blurred (jumbled). To be specific at the top and bottom rows of the list are displayed properly but in...
0
827
by: manojmajhi | last post by:
suddenly all my internet images are appearing blurred. can anybody help...!
3
1172
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' name="txt2" maxlength='254' value="" onblur="checkDir()"> i've a funtion in js called checkDir()... and i've more text boxes... i need to focus the...
1
3383
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. The problem is when it creates transparent PNG format image then and it pixel ate the image. e.g. If i am using a gradient in background then it...
2
3136
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 message as in alert box as " Internet Explorer cannot open the Internet site http://google.citycarrentals.com.au/viewalllocations.php . Operation...
0
7579
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7877
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8101
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
6238
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5479
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5204
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3631
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3615
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2077
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 we have to send another system

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.