Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / Drawing / May 2004

Tip: Looking for answers? Try searching our database.

GDI+ error when trying to use DoubleBuffer

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Chris - 28 May 2004 05:52 GMT
When I run this code I get this error.  If I comment out DoubleBuffer, the
program works fine.  Can anyone figure out why it is failing?

Error message:
An unhandled exception of type 'System.ArgumentException' occurred in
system.drawing.dll
Additional information: Invalid parameter used.

Code:  (This code works if you put it into a new application and add a
timer.  Also import System.Drawing.Drawing2D)

Dim RotAng As Decimal = 1.0F
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As
System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint

Dim canvas As Graphics = e.Graphics

Dim paintBrush As New SolidBrush(Color.Black)

Dim pen1 As New Pen(Color.Black)

Dim gpath As New GraphicsPath

Dim XPoints1(3) As PointF

XPoints1(0) = New PointF(6, 6)

XPoints1(1) = New PointF(0, 6)

XPoints1(2) = New PointF(0, 9)

XPoints1(3) = New PointF(6, 9)

Dim myMatrix As New Matrix

Dim myMatrix2 As New Matrix

Dim rotatePoint As New PointF(3.0F, 7.5F)

myMatrix.RotateAt(RotAng, rotatePoint, MatrixOrder.Append)

gpath.AddPolygon(XPoints1)

gpath.Transform(myMatrix)

myMatrix2.Translate(50, 50)

myMatrix2.Scale(4, 4)

gpath.Transform(myMatrix2)

'e.Graphics.FillPath(paintBrush, gpath)

e.Graphics.DrawPath(pen1, gpath)

paintBrush.Dispose()

pen1.Dispose()

canvas.Dispose()

RotAng = RotAng + 3.0F Mod 360.0

End Sub

Private Sub Form1_Resize(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Resize

Invalidate()

End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Timer1.Tick

Me.Invalidate()

End Sub

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles MyBase.Load

Me.SetStyle(ControlStyles.DoubleBuffer, True)

Me.SetStyle(ControlStyles.UserPaint, True)

Me.SetStyle(ControlStyles.AllPaintingInWmPaint, True)

Me.UpdateStyles()

End Sub

This code rotates a simple box drawn with a graphics path and pen.

Thanks!

Chris
Bob Powell [MVP] - 28 May 2004 07:48 GMT
I have noticed this same exception on several occasions and I beleive there
is a bug in Windows Forms double buffer code but I cannot say exactly what's
needed to reproduce the fault.

To fix the problem in one of my applications that blows up regularly with
this problem I used manula double buffering.

The Windows Forms Tips and Tricks page has an article on double buffering
that illustrates how to do this.

http://www.bobpowell.net/tipstricks.htm

Signature

Bob Powell [MVP]
Visual C#, System.Drawing

Image transition effects, automatic persistent configuration and
design time mouse operations all in April's issue of Well Formed
http://www.bobpowell.net/wellformed.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

The GDI+ FAQ RSS feed: http://www.bobpowell.net/faqfeed.xml
Windows Forms Tips and Tricks RSS: http://www.bobpowell.net/tipstricks.xml
Bob's Blog: http://royo.is-a-geek.com/siteFeeder/GetFeed.aspx?FeedId=41

> When I run this code I get this error.  If I comment out DoubleBuffer, the
> program works fine.  Can anyone figure out why it is failing?
[quoted text clipped - 93 lines]
>
> Chris
Chris - 29 May 2004 06:09 GMT
The code is in C# which I am not familiar with.  I have tried to convert it
but have been unsuccessfull.  Could someone help me find or write the VB.NET
version of double buffering?

Thanks!
Chris

> I have noticed this same exception on several occasions and I beleive there
> is a bug in Windows Forms double buffer code but I cannot say exactly what's
[quoted text clipped - 106 lines]
> >
> > Chris
Chris - 29 May 2004 06:42 GMT
Why is this not working?  The problem is explained in a comment in the code:

       Dim canvas As Graphics = e.Graphics
       Dim pBMPBuffer As New Bitmap(Me.ClientSize.Height,
Me.ClientSize.Width)
       Dim PGRAGraphics As Graphics = Graphics.FromImage(pBMPBuffer)
       Dim paintBrush As New SolidBrush(Color.Black)
       Dim gpath As New GraphicsPath
       Dim pts(3) As PointF
       Dim pt As New Point(0, 0)

       pts(0) = New PointF(50, 50)
       pts(1) = New PointF(41, 50)
       pts(2) = New PointF(41, 55)
       pts(3) = New PointF(50, 55)

       gpath.AddPolygon(pts)

       'canvas.FillPath(paintBrush, gpath)                    'when this
line is uncommented, the rectangle is displayed (no double buffering)

       'PGRAGraphics.FillPath(paintBrush, gpath)        'when these two
lines are uncommented, the rectangle fails to display
       'PGRAGraphics.DrawImageUnscaled(pBMPBuffer, pt)

       paintBrush.Dispose()
       PGRAGraphics.Dispose()
       canvas.Dispose()

What am I doing wrong?

Thanks!
Chris

> The code is in C# which I am not familiar with.  I have tried to convert it
> but have been unsuccessfull.  Could someone help me find or write the VB.NET
[quoted text clipped - 116 lines]
> > >
> > > Chris
Bob Powell [MVP] - 29 May 2004 10:36 GMT
Hello Chris.

Did you subscribe to the Windows Forms Tips and Tricks RSS feed? If you did
you'll have discovered that the article has been updated to include code in
VB.NET.

I reccommend RSS Bandit if you haven't already chosen an RSS aggregator yet.

Signature

Bob Powell [MVP]
Visual C#, System.Drawing

Image transition effects, automatic persistent configuration and
design time mouse operations all in April's issue of Well Formed
http://www.bobpowell.net/wellformed.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

The GDI+ FAQ RSS feed: http://www.bobpowell.net/faqfeed.xml
Windows Forms Tips and Tricks RSS: http://www.bobpowell.net/tipstricks.xml
Bob's Blog: http://royo.is-a-geek.com/siteFeeder/GetFeed.aspx?FeedId=41

> The code is in C# which I am not familiar with.  I have tried to convert it
> but have been unsuccessfull.  Could someone help me find or write the VB.NET
[quoted text clipped - 116 lines]
> > >
> > > Chris
Chris - 29 May 2004 14:56 GMT
Oh, I didn't even see that part of the sight.  Thanks!

Chris

> Hello Chris.
>
[quoted text clipped - 131 lines]
> > > >
> > > > Chris

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.