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

cleartype - gdi

P: n/a
I was banging my head against the wall with this code in a sub:

Dim writing As New Bitmap(ctrl.Width, ctrl.Height)
Dim surface As Graphics = Graphics.FromImage(writing)
surface.DrawString(textToWrite, font1, Brushes.Black, 0, 0)
ctrl.Image = writing

It produced terribly blocky text.

I finally added:
surface.TextRenderingHint =
System.Drawing.Text.TextRenderingHint.SingleBitPer PixelGridFit

Which cleared up the mess.

I then got thinking - both machines I was testing it on have lcd's and
cleartype enabled in the control panel. I went and switched it off, and
tried the basic code again and it worked without producing the weird text.

See piccy:
http://www.psatracker.co.uk/pics/Cleartype.gif

If you create a graphics object in a controls paint event and use the same
code to draw a string on that then you don't get the blocky text.

Weird. Anyone know how to detect if the user has Cleartype switched on? I'm
using the cleartpye tuning wizard in the control panel to enable cleartype
if it is relevant.
Nov 21 '05 #1
Share this Question
Share on Google+
7 Replies

P: n/a
hoppy <me@null.invalid> wrote in
news:Xn***************************@194.117.143.53:
I was banging my head against the wall with this code in a sub:

Dim writing As New Bitmap(ctrl.Width, ctrl.Height)
Dim surface As Graphics = Graphics.FromImage(writing)
surface.DrawString(textToWrite, font1, Brushes.Black, 0, 0)
ctrl.Image = writing

It produced terribly blocky text.

I finally added:
surface.TextRenderingHint =
System.Drawing.Text.TextRenderingHint.SingleBitPer PixelGridFit

Which cleared up the mess.

I then got thinking - both machines I was testing it on have lcd's and
cleartype enabled in the control panel. I went and switched it off,
and tried the basic code again and it worked without producing the
weird text.

See piccy:
http://www.psatracker.co.uk/pics/Cleartype.gif

If you create a graphics object in a controls paint event and use the
same code to draw a string on that then you don't get the blocky text.

Weird. Anyone know how to detect if the user has Cleartype switched
on? I'm using the cleartpye tuning wizard in the control panel to
enable cleartype if it is relevant.

got help:

HKEY_CURRENT_USER\Control Panel\Desktop\FontSmoothingType
Nov 21 '05 #2

P: n/a
"hoppy" <me@null.invalid> schrieb:
Dim writing As New Bitmap(ctrl.Width, ctrl.Height)
Dim surface As Graphics = Graphics.FromImage(writing)
surface.DrawString(textToWrite, font1, Brushes.Black, 0, 0)
ctrl.Image = writing

It produced terribly blocky text.

I finally added:
surface.TextRenderingHint =
System.Drawing.Text.TextRenderingHint.SingleBitPer PixelGridFit

Which cleared up the mess.

I then got thinking - both machines I was testing it on have lcd's and
cleartype enabled in the control panel. I went and switched it off, and
tried the basic code again and it worked without producing the weird text.

See piccy:
http://www.psatracker.co.uk/pics/Cleartype.gif

If you create a graphics object in a controls paint event and use the same
code to draw a string on that then you don't get the blocky text.


The picture looks as if you were drawing the same string several times at
the same position without clearing the surface you are drawing on.
ClearType will add RGB values to the surrounding pixels, and thus drawing
the same string at the same position more than one time will lead to ugly
results.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #3

P: n/a
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in
news:eK**************@TK2MSFTNGP12.phx.gbl:
"hoppy" <me@null.invalid> schrieb:
Dim writing As New Bitmap(ctrl.Width, ctrl.Height)
Dim surface As Graphics = Graphics.FromImage(writing)
surface.DrawString(textToWrite, font1, Brushes.Black, 0, 0)
ctrl.Image = writing

It produced terribly blocky text.

I finally added:
surface.TextRenderingHint =
System.Drawing.Text.TextRenderingHint.SingleBitPer PixelGridFit

Which cleared up the mess.

I then got thinking - both machines I was testing it on have lcd's
and cleartype enabled in the control panel. I went and switched it
off, and tried the basic code again and it worked without producing
the weird text.

See piccy:
http://www.psatracker.co.uk/pics/Cleartype.gif

If you create a graphics object in a controls paint event and use the
same code to draw a string on that then you don't get the blocky
text.


The picture looks as if you were drawing the same string several times
at the same position without clearing the surface you are drawing on.
ClearType will add RGB values to the surrounding pixels, and thus
drawing the same string at the same position more than one time will
lead to ugly results.


It happens when you draw the string once.

This (complete) code produces the linked text when cleartype is on.

http://www.psatracker.co.uk/pics/test.bmp
Imports System.Drawing
Module Module1
Sub Main()
Dim bm As New Bitmap(450, 120)
Dim g As Graphics = Graphics.FromImage(bm)
Dim f As Font = New Font("Tahoma", 9.75!, FontStyle.Regular)
Dim s As String
s = "Lorem ipsum dolor sit amet, consectetur adipisicing elit," & _
"sed do eiusmod tempor incididunt ut labore et dolore ma" & _
"gna aliqua. Ut enim ad minim veniam, quis nostrud exerc" & _
"itation ullamco laboris nisi ut aliquip ex ea commodo c" & _
"onsequat. Duis aute irure dolor in reprehenderit in vol" & _
"uptate velit esse cillum dolore eu fugiat nulla pariatu" & _
"r. Excepteur sint occaecat cupidatat non proident, sunt" & _
"in culpa qui officia deserunt mollit anim id est laborum"

Dim sf As StringFormat = _
CType(StringFormat.GenericDefault.Clone(), StringFormat)

Dim recF As New RectangleF(0, 0, 450, 120)
g.DrawString(s, f, Brushes.Black, recF, sf)
bm.Save("C:\test.bmp")
End Sub
End Module
It looks normal when cleartype is off.
Not to worry, I can fix it by testing this key to see if cleartype is on
(value = 2):

HKEY_CURRENT_USER\Control Panel\Desktop\FontSmoothingType

and then setting
g.TextRenderingHint to something appropriate such as:
System.Drawing.Text.TextRenderingHint.SingleBitPer PixelGridFit
Nov 21 '05 #4

P: n/a
"hoppy" <no***@invalid.null> schrieb:
This (complete) code produces the linked text when cleartype is on.

http://www.psatracker.co.uk/pics/test.bmp
Imports System.Drawing
Module Module1
Sub Main()
Dim bm As New Bitmap(450, 120)
Dim g As Graphics = Graphics.FromImage(bm)
Dim f As Font = New Font("Tahoma", 9.75!, FontStyle.Regular)
Dim s As String
s = "Lorem ipsum dolor sit amet, consectetur adipisicing elit," & _
"sed do eiusmod tempor incididunt ut labore et dolore ma" & _
"gna aliqua. Ut enim ad minim veniam, quis nostrud exerc" & _
"itation ullamco laboris nisi ut aliquip ex ea commodo c" & _
"onsequat. Duis aute irure dolor in reprehenderit in vol" & _
"uptate velit esse cillum dolore eu fugiat nulla pariatu" & _
"r. Excepteur sint occaecat cupidatat non proident, sunt" & _
"in culpa qui officia deserunt mollit anim id est laborum"

Dim sf As StringFormat = _
CType(StringFormat.GenericDefault.Clone(), StringFormat)

Dim recF As New RectangleF(0, 0, 450, 120)
g.DrawString(s, f, Brushes.Black, recF, sf)
bm.Save("C:\test.bmp")
End Sub
End Module


As I already said, don't forget to clear the bitmap ('g.Clear(Color.White)')
before drawing the string -- this will fix the problem. Otherwise a
transparent PNG will be saved and many applications are not able to display
these images correctly.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #5

P: n/a
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in
news:uK**************@tk2msftngp13.phx.gbl:
"hoppy" <no***@invalid.null> schrieb:
This (complete) code produces the linked text when cleartype is on.

http://www.psatracker.co.uk/pics/test.bmp
Imports System.Drawing
Module Module1
Sub Main()
Dim bm As New Bitmap(450, 120)
Dim g As Graphics = Graphics.FromImage(bm)
Dim f As Font = New Font("Tahoma", 9.75!, FontStyle.Regular)
Dim s As String
s = "Lorem ipsum dolor sit amet, consectetur adipisicing elit," & _
"sed do eiusmod tempor incididunt ut labore et dolore ma" & _
"gna aliqua. Ut enim ad minim veniam, quis nostrud exerc" & _
"itation ullamco laboris nisi ut aliquip ex ea commodo c" & _
"onsequat. Duis aute irure dolor in reprehenderit in vol" & _
"uptate velit esse cillum dolore eu fugiat nulla pariatu" & _
"r. Excepteur sint occaecat cupidatat non proident, sunt" & _
"in culpa qui officia deserunt mollit anim id est laborum"

Dim sf As StringFormat = _
CType(StringFormat.GenericDefault.Clone(), StringFormat)

Dim recF As New RectangleF(0, 0, 450, 120)
g.DrawString(s, f, Brushes.Black, recF, sf)
bm.Save("C:\test.bmp")
End Sub
End Module


As I already said, don't forget to clear the bitmap
('g.Clear(Color.White)') before drawing the string -- this will fix
the problem. Otherwise a transparent PNG will be saved and many
applications are not able to display these images correctly.


hmm ok, that works. sorry wasn't paying attention.

I'm actually then using the bitmap to make a cursor for drag and drop
(cursor is an image of the text being dragged). If I blank out the back
beforehand then I'll get a white background. For now I'll use the
workaround. I guess what I should do is create the cursor properly from a
bitmap and a mask, I'll have to read up on that.
Nov 21 '05 #6

P: n/a
"hoppy" <no***@invalid.null> schrieb:
I'm actually then using the bitmap to make a cursor for drag and drop
(cursor is an image of the text being dragged). If I blank out the back
beforehand then I'll get a white background. For now I'll use the
workaround. I guess what I should do is create the cursor properly from a
bitmap and a mask, I'll have to read up on that.


When creating a cursor I would not use ClearType rendering. As you already
know you can change the text rendering mode by setting the 'Graphics'
object's 'TextRenderingHint' property to an appropriate value.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #7

P: n/a

'This will create a cursor from a part of the control
'Assume gph is the Graphics object for the control
'ulcornerX, ulcornerY, width, and height descripe what part of the control
you want to make a cursor from

Dim rect As New Rectangle(ulcornerX, ulcornerY,width, height)
Dim cursorbitmap as New Bitmap(rect.Width, rect.Height)
cursorbitmap = CopyRect(gph, rect)
cursorbitmap.MakeTransparent(backgroundcolor)
dim ptrCur as IntPtr = cursorbitmap.GetHicon
dim ps Point = Me.PointToScreen(New Point(0, t))
dim cp as New Point(ulcornerXStartCursorPosition,
ulcornerYStartCursorPosition)
dim cc as New Rectangle(ulcornerX, ulcornerY, WidthofallowedCursormove,
HeightofAllowedCursorMove) 'defines the boundaries that the cursor can be
moved
Me.Cursor = New Cursor(ptrCur)
Me.Cursor.Position = cp
Me.Cursor.Clip = cc
DeleteObject(ptrCur)

--
Dennis in Houston
"hoppy" wrote:
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in
news:uK**************@tk2msftngp13.phx.gbl:
"hoppy" <no***@invalid.null> schrieb:
This (complete) code produces the linked text when cleartype is on.

http://www.psatracker.co.uk/pics/test.bmp
Imports System.Drawing
Module Module1
Sub Main()
Dim bm As New Bitmap(450, 120)
Dim g As Graphics = Graphics.FromImage(bm)
Dim f As Font = New Font("Tahoma", 9.75!, FontStyle.Regular)
Dim s As String
s = "Lorem ipsum dolor sit amet, consectetur adipisicing elit," & _
"sed do eiusmod tempor incididunt ut labore et dolore ma" & _
"gna aliqua. Ut enim ad minim veniam, quis nostrud exerc" & _
"itation ullamco laboris nisi ut aliquip ex ea commodo c" & _
"onsequat. Duis aute irure dolor in reprehenderit in vol" & _
"uptate velit esse cillum dolore eu fugiat nulla pariatu" & _
"r. Excepteur sint occaecat cupidatat non proident, sunt" & _
"in culpa qui officia deserunt mollit anim id est laborum"

Dim sf As StringFormat = _
CType(StringFormat.GenericDefault.Clone(), StringFormat)

Dim recF As New RectangleF(0, 0, 450, 120)
g.DrawString(s, f, Brushes.Black, recF, sf)
bm.Save("C:\test.bmp")
End Sub
End Module


As I already said, don't forget to clear the bitmap
('g.Clear(Color.White)') before drawing the string -- this will fix
the problem. Otherwise a transparent PNG will be saved and many
applications are not able to display these images correctly.


hmm ok, that works. sorry wasn't paying attention.

I'm actually then using the bitmap to make a cursor for drag and drop
(cursor is an image of the text being dragged). If I blank out the back
beforehand then I'll get a white background. For now I'll use the
workaround. I guess what I should do is create the cursor properly from a
bitmap and a mask, I'll have to read up on that.

Nov 21 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.