VB.Net Double Buffering problem - help?

A

Andy Roxburgh

Hi All, can anyone advise me why this code doesn't work? I get nothing
on the screen.

It's an attempt to write a string to an off-screen bitmap, then write
the bitmap to the screen. And incidentally I'm using the NETCF.


-----

Public Sub direct_draw_text_cent_tah_14(ByVal text_col As Color,
ByVal pointx As Integer, ByVal pointy As Integer, ByVal sizex As
Integer, ByVal sizey As Integer, ByVal drawtext As String)

Dim f As New Font("tahoma", 14.25, FontStyle.Regular)
Dim r As New RectangleF(pointx, pointy, sizex, sizey)
Dim b As New SolidBrush(text_col)

Dim bufferbmp As New Bitmap(sizex, sizey)

Dim g As Graphics = Graphics.FromImage(bufferbmp)

g.DrawString(drawtext, f, b, r)
g.Dispose()

Dim gg As Graphics = Me.CreateGraphics()

gg.DrawImage(bufferbmp, 0, 0)
gg.Dispose()

b.Dispose()
f.Dispose()
bufferbmp.Dispose()

End Sub
 
W

Webster

Try not to dispose g until the end of the sub...

You are drawing the text on g, and then disposing of it before you use it...
 
J

Jay B. Harlow [MVP - Outlook]

Andy,
In addition to Webster's comments.

Rather then
Dim gg As Graphics = Me.CreateGraphics()
gg.DrawImage(bufferbmp, 0, 0)
gg.Dispose()

Have you tried drawing the bitmap during the Paint event itself? Using the
Graphics object supplied by the Paint event!

It sounds like you are drawing the image immediately, then the paint event
comes along and effectively erases what you just drew...

Hope this helps
Jay
 
M

Matthew Stoecker

One other this I might suggest-you can enable double buffering using the
.NET FRamework in the following manner.

[Visual Basic]
Public Sub EnableDoubleBuffering()
' Set the value of the double-buffering style bits to true.
Me.SetStyle(ControlStyles.DoubleBuffer _
Or ControlStyles.UserPaint _
Or ControlStyles.AllPaintingInWmPaint, _
True)
Me.UpdateStyles()
End Sub

[C#]
public void EnableDoubleBuffering()
{
// Set the value of the double-buffering style bits to true.
this.SetStyle(ControlStyles.DoubleBuffer |
ControlStyles.UserPaint |
ControlStyles.AllPaintingInWmPaint,
true);
this.UpdateStyles();
}

Hope this helps!

Matthew Stoecker
Visual Basic User Education Team
Microsoft Corp.


--------------------
| From: (e-mail address removed) (Andy Roxburgh)
| Newsgroups: microsoft.public.dotnet.languages.vb
| Subject: VB.Net Double Buffering problem - help?
| Date: 18 Jan 2004 22:35:14 -0800
| Organization: http://groups.google.com
| Lines: 34
| Message-ID: <[email protected]>
| NNTP-Posting-Host: 172.191.31.185
| Content-Type: text/plain; charset=ISO-8859-1
| Content-Transfer-Encoding: 8bit
| X-Trace: posting.google.com 1074494114 5669 127.0.0.1 (19 Jan 2004
06:35:14 GMT)
| X-Complaints-To: (e-mail address removed)
| NNTP-Posting-Date: Mon, 19 Jan 2004 06:35:14 +0000 (UTC)
| Path:
cpmsftngxa07.phx.gbl!cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!newsfeed00.su
l.t-online.de!t-online.de!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!po
stnews1.google.com!not-for-mail
| Xref: cpmsftngxa07.phx.gbl microsoft.public.dotnet.languages.vb:174032
| X-Tomcat-NG: microsoft.public.dotnet.languages.vb
|
| Hi All, can anyone advise me why this code doesn't work? I get nothing
| on the screen.
|
| It's an attempt to write a string to an off-screen bitmap, then write
| the bitmap to the screen. And incidentally I'm using the NETCF.
|
|
| -----
|
| Public Sub direct_draw_text_cent_tah_14(ByVal text_col As Color,
| ByVal pointx As Integer, ByVal pointy As Integer, ByVal sizex As
| Integer, ByVal sizey As Integer, ByVal drawtext As String)
|
| Dim f As New Font("tahoma", 14.25, FontStyle.Regular)
| Dim r As New RectangleF(pointx, pointy, sizex, sizey)
| Dim b As New SolidBrush(text_col)
|
| Dim bufferbmp As New Bitmap(sizex, sizey)
|
| Dim g As Graphics = Graphics.FromImage(bufferbmp)
|
| g.DrawString(drawtext, f, b, r)
| g.Dispose()
|
| Dim gg As Graphics = Me.CreateGraphics()
|
| gg.DrawImage(bufferbmp, 0, 0)
| gg.Dispose()
|
| b.Dispose()
| f.Dispose()
| bufferbmp.Dispose()
|
| End Sub
|
 

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