Why are you not using the Graphics instance provided by the PaintEventArgs?


When I run your original code, the performance is not optimal.  However,
changing the SetStyle call to include AllPaintingInWmPaint and using the
Graphics provided with the PaintEventArgs yields very good performance.

Sean

// here's my code
using System;
using System.Drawing;
using System.Windows.Forms;

public class MyControl : UserControl
{
        public MyControl()
        {
                this.SetStyle(ControlStyles.DoubleBuffer |
ControlStyles.AllPaintingInWmPaint, true);
        }

        protected override void OnPaint(PaintEventArgs e)
        {
                Graphics g = e.Graphics; //this.CreateGraphics();
                Pen p = new Pen(Color.Blue, 1);
                g.FillRectangle(new SolidBrush(this.BackColor),
this.DisplayRectangle);
                for(int i = 0; i < this.Width; i++)
                {
                        g.DrawLine(p, i, 0, i, this.Height);
                }
        }
}
public class Form1 : Form
{
        public Form1()
        {
                MyControl mc = new MyControl();
                mc.Dock = DockStyle.Fill;
                this.Controls.Add(mc);
        }
}
public class App
{
        public static void Main()
        {
                Application.Run(new Form1());
        }
}
-----Original Message-----
From: Eric Franz [mailto:[EMAIL PROTECTED]]
Sent: Tuesday, August 20, 2002 9:05 AM
To: [EMAIL PROTECTED]
Subject: Re: [ADVANCED-DOTNET] Help Please on Bitmap Buffer


Okay everyone, thanks for your fast response but I think we still need to
look deeper into this painting issue.

I have some sample code which I feel in may substantiate my claim that
double buffering only buffers ONE drawing command at a time. First lets
consider how DotNet would even know, given a series of paint commands,
when your are ready for the buffered bitmap to be painted. For the most
part I would say that it can't know. What it does know is each INDIVIDUAL
command can be buffered. I have some sample code which draws many lines on
a user control. When you set the doublebuffer style to TRUE, it should in
theory paint the whole control blue all at one time. RIGHT? Wrong, check
it out:

A: Make A UserControl
B: Put on a form with Dockstyle FILL
C: Run and Resize the Form to Fire OnPaint Override
D: Toggle the SetStyle Attribute in your UserControl Contstructor to see
the majic ->  setstyle(ControlStyles.DoubleBuffer, True)

Protected Overrides Sub OnPaint(ByVal e As
System.Windows.Forms.PaintEventArgs)
        Dim g As Graphics = Me.CreateGraphics
        Dim p As New Pen(Color.Blue, 1)

        Dim i As Integer

        g.FillRectangle(New SolidBrush(Me.BackColor), New RectangleF(0, 0,
Me.Width, Me.Height))

        For i = 0 To Me.Width Step 1
            g.DrawLine(p, i, 0, i, Me.Height)
            Me.ParentForm.Text = i
        Next
    End Sub

Take a look,
ERIC FRANZ
Physicians Accounting Ltd

You can read messages from the Advanced DOTNET archive, unsubscribe from
Advanced DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.

You can read messages from the Advanced DOTNET archive, unsubscribe from Advanced 
DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.

Reply via email to