Why is setpixel so slow?

I have written a Mandelbrot program which writes colour data to a bitmap using the setpixel method. It wasn't a particularly fast method before under VBE beta 2 but now I have installed the new version VB2005 it takes ages! - over a minute to fill a screen sized bitmap! What is going on

Answer this question

Why is setpixel so slow?

  • VShaneCurtis

    I have since discovered that the .exe file produced by VB2005 runs at an acceptable speed. It is only when running the program in debugging mode that setpixel is so slow. Other graphics routines are slowed down by a certain anmount when debugging but nothing like as much as setpixel. Can anyone explain Is anyone else out there using setpixel under VB2005

    Here is the code I am using.

    Imports System.Drawing.Drawing2D

    Public Class Form1

    'This example fills a 400x400 pixel bitmap with colour

    'When run in debugging mode

    'the SetPixel method takes over 6 seconds

    'the FillRectangle mathod takes 2.5 seconds

    'Why is the former slower than the latter and why are they both so slow

    'When the exe file is run directly, both routines take less than half a second and setpixel is faster

    Dim start, finish As Double

    Dim n, x, y As Integer

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    n = 400

    Dim bm1 As New Bitmap(n, n)

    Dim bm2 As New Bitmap(n, n)

    start = Microsoft.VisualBasic.DateAndTime.Timer

    For y = 0 To n - 1

    For x = 0 To n - 1

    bm1.SetPixel(x, y, Color.Blue)

    Next

    Next

    finish = Microsoft.VisualBasic.DateAndTime.Timer

    TextBox1.Text = Str((finish - start) * 1000)

    PictureBox1.BackgroundImage = bm1

    Dim gr As Graphics

    gr = Graphics.FromImage(bm2)

    start = Microsoft.VisualBasic.DateAndTime.Timer

    For y = 0 To n - 1

    For x = 0 To n - 1

    gr.FillRectangle(Brushes.Red, x, y, 1, 1)

    Next

    Next

    finish = Microsoft.VisualBasic.DateAndTime.Timer

    TextBox2.Text = Str((finish - start) * 1000)

    PictureBox2.BackgroundImage = bm2

    End Sub

    End Class


  • Why is setpixel so slow?