Text looks bold when i manually double buffer to a bitmap

G

Guest

Hi

Thanks for taking the time to read this post and offering any
suggestions/advice.

I have an owner drawn control that uses the standard setstyle method to
double buffer my painting. Everything works fine and looks as intended.

I am now in the process of expanded my rendering class to support internal
scrollable sections. I decided to do this by creating a wrapper class to
handle the painting. The wrapper class creates a new bitmap and passes the
graphics object from it to the original paint event of the renderer. It then
works out what section of the paint needs to be blitted onto the real canvas
and does so using the drawimage method.

It all works great apart from any text that is rendered to the new bitmap
looks really bad. It almost looks like it is bold but it is also nowhere
near as smooth as a bold font would be.

I believe I have ruled this down to the new bitmap being created with a
transparent background using black pixels. If I change the background to
white using the setpixel method and make it transparent using this colour (a
straight fillrectangle and anything other than white does not work) then
everything looks like it should.

Obviously this is a very slow method and totally unacceptable in the final
product.

Can anyone shed any light on the standard practice used to eliminate such
problems?

My current code in the paint method of the scrolling wrapper class is as
follows ....

'create canvas for child to paint on. we will extract the applicable
portion of the
'canvas and blit the render to the passed graphics object

Dim canvas As New System.Drawing.Bitmap(rect.Left + rect.Width, rect.Top + h)
canvas.SetResolution(g.DpiX, g.DpiY)

Dim gphcs As System.Drawing.Graphics =
System.Drawing.Graphics.FromImage(canvas)

Dim rect2 As New System.Drawing.Rectangle(rect.Left, rect.Top, canvas.Width,
canvas.Height)

Dim srcRect As New System.Drawing.Rectangle((rect.Left + _XOffset),
(rect.Top + _YOffset), rect.Width, rect.Height)

'***** this is the bit that corrects the problem, but it is very slow *****
'Dim x, y As Integer
'For x = srcRect.Left To srcRect.Right - 1 Step 1
' For y = srcRect.Top To srcRect.Bottom - 1 Step 1
' canvas.SetPixel(x, y, System.Drawing.Color.White)
' Next
'Next
'canvas.MakeTransparent(System.Drawing.Color.White)

_renderer.Paint(cp, gphcs, rect2)

g.DrawImage(canvas, rect, srcRect, Drawing.GraphicsUnit.Pixel)

gphcs.Dispose()
canvas.Dispose()
 
H

Herfried K. Wagner [MVP]

Dick Donny said:
It all works great apart from any text that is rendered to the new bitmap
looks really bad. It almost looks like it is bold but it is also nowhere
near as smooth as a bold font would be.

I believe I have ruled this down to the new bitmap being created with a
transparent background using black pixels. If I change the background to
white using the setpixel method and make it transparent using this colour
(a
straight fillrectangle and anything other than white does not work) then
everything looks like it should.

Obtain a 'Graphics' object for the bitmap using 'Graphics.FromImage' and
call its 'Clear' method to fill the bitmap with a certain color before
drawing the text.
 
G

Guest

Herfried K. Wagner said:
Obtain a 'Graphics' object for the bitmap using 'Graphics.FromImage' and
call its 'Clear' method to fill the bitmap with a certain color before
drawing the text.

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

Thank you for your suggestion.

The clear method is great for replacing the setpixel loops rubbish I was
doing and is a lot quicker. Thanks for the tip.

If I call this method with any colour other than White, the font still looks
bad when I call the MakeTransparent method of the bitmap (its ok if I dont
call it, but it has to be transparent). White is a bad choice for the
transparency because it is a valid colour on the paint and any images
rendered also look like they have a white glow around them.

Once again, I appreciate the suggestion and your time.
 
G

Guest

Dick Donny said:
The clear method is great for replacing the setpixel loops rubbish I was
doing and is a lot quicker. Thanks for the tip.

If I call this method with any colour other than White, the font still looks
bad when I call the MakeTransparent method of the bitmap (its ok if I dont
call it, but it has to be transparent). White is a bad choice for the
transparency because it is a valid colour on the paint and any images
rendered also look like they have a white glow around them.

Once again, I appreciate the suggestion and your time.

Ok so I dumped the original code in the end and used a transformation to
handle the scrolling. This has sorted the problem without actually solving
it.

For those interested, the changed code is

Dim gct As System.Drawing.Drawing2D.GraphicsContainer

g.SetClip(rect)

gct = g.BeginContainer

g.TranslateTransform(_XOffset * -1, _YOffset * -1)

_renderer.Paint(cp, g, rect)

g.EndContainer(gct)

g.ResetClip()


Thanks to everyone who took the time to take a look at this for me
 
H

Herfried K. Wagner [MVP]

Dick Donny said:
The clear method is great for replacing the setpixel loops rubbish I was
doing and is a lot quicker. Thanks for the tip.

If I call this method with any colour other than White, the font still
looks
bad when I call the MakeTransparent method of the bitmap (its ok if I dont
call it, but it has to be transparent). White is a bad choice for the
transparency because it is a valid colour on the paint and any images
rendered also look like they have a white glow around them.

I assume the reason is that the text is drawn using antialiasing or
ClearType. You can disable antialiasing as shown below:

\\\
g.TextRenderingHint = _
TextRenderingHint.SingleBitPerPixelGridFit
g.DrawString(...)
///

In addition, take a look at the 'Graphics' object's 'SmoothingMode'
property.
 

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