cleartype - gdi

H

hoppy

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.SingleBitPerPixelGridFit

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.
 
H

hoppy

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.SingleBitPerPixelGridFit

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
 
H

Herfried K. Wagner [MVP]

hoppy said:
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.SingleBitPerPixelGridFit

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.
 
H

hoppy

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.SingleBitPerPixelGridFit
 
H

Herfried K. Wagner [MVP]

hoppy said:
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.
 
H

hoppy

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.
 
H

Herfried K. Wagner [MVP]

hoppy said:
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.
 
G

Guest

'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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top