0

I'm working on some image processing and for debug I'm overlaying colours on the original bitmap.

The problem is the image is rendered in a picture box with SizeMode set to Zoom and invalidating every time I update a pixel is Really slow and just gets slower the larger picturebox is (for the same size image)

What I think might help is if I only invalidate the pixel(s) I've changed but I don't know how convert the co-ordinates of the pixel I've changed into a rectangle rendered on the control. Obviously if the image is being drawn larger than the original image then the rectangle I'm invalidating is going to be more than one pixel

James
  • 9,774
  • 5
  • 34
  • 58

3 Answers3

1

Added a method to get the zoom and padding of the picture pox

    private void CalculateZoomAndPadding()
    {
        Double imageAspect = (Double)pictureBox1.Image.Width / (Double)pictureBox1.Image.Height;
        Double pbAspect = (Double)pictureBox1.Width / (Double)pictureBox1.Height;
        Boolean heightRestricted = imageAspect < pbAspect;
        hPadding = 0;
        vPadding = 0;
        if (heightRestricted)
        {
            zoom = (Double)pictureBox1.Height / (Double)pictureBox1.Image.Height;
            Double imageWidth = (Double)pictureBox1.Image.Width * zoom;
            hPadding = (Double)(pictureBox1.Width - imageWidth) / 2d;
        }
        else
        {
            zoom = (Double)pictureBox1.Width / (Double)pictureBox1.Image.Width;
            Double imageHeight = (Double)pictureBox1.Image.Height * zoom;
            vPadding = (Double)(pictureBox1.Height - imageHeight) / 2d;
        }
    }

then to invalidate a pixel called invalidate like this:

pictureBox1.Invalidate(new Rectangle(Convert.ToInt32(Math.Floor(x * zoom)) + Convert.ToInt32(hPadding) -1, Convert.ToInt32(Math.Floor(y * zoom)) + Convert.ToInt32(vPadding) -1, PixelSize, PixelSize));

when I first did this I only invalidated the are directly covered by the pixel but found that this was subject to rounding errors so expanded it to include a few extra.

James
  • 9,774
  • 5
  • 34
  • 58
0

Can you change all the pixels and then just invalidate the image once?

PRMan
  • 535
  • 1
  • 8
  • 16
  • That way I don't see how the image processing is working on each pixel, I'd only get the finished product instead of the steps it took to get there. – James May 13 '11 at 21:56
  • You won't see it working on every pixel anyway unless you're either throtling it down or your processing is rather slow anyway. Don't forget your monitor isn't able to update 1000 times a second. – Mario May 13 '11 at 21:58
  • I'm throttling it down when I'm looking at an area that I want to investigate but when I'm looking at a large image it can take 10 minutes to get to where I'm interested. – James May 13 '11 at 22:12
0

I'd just add a timer that fires 30 or 60 times per second that invalidates the whole control. While there might be a slight delay in updating you shouldn't be able to notice it due to your monitor's refresh rate most likely being 60 Hz only anyway.

Mario
  • 35,726
  • 5
  • 62
  • 78