Re: Fastest way to update a Picturebox's image

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



I have an app (written in .NET 2.0) which updates a picturebox
according to some user input (a slider control). when the user makes a
change i loop through all of the pixels, do a calculation and update
the picture. i currently use the GDI SetPixel method on each pixel of
the Pictureboxes image. This is proving far to slow, about 1.5 seconds
on average. This app needs to display the update as fast as possible.
Has anyone got any ideas as to how to speed this up? i was thinking
about trying to access the picturebox's memory space directly, pseudo
code:

For Each pixel in <PictureBox.Image's bytearray>
<PictureBox.Image's bytearray> (pixel) = <new pixel value from
calculation>
Next

This way you're only updating some byte values in memory rather than
calling GDI method's.

Am i barking up the wrong tree? Has anyone got any better solutions?
Any help would be much appreciated.

As Bob mentioned, this group is for VB (Classic) queries, however here's the answer to the GDI side of your question:
You first need to work out whether you're using a DIBSection or DDB, you can do this by calling GetObject() on the
Bitmap handle and checking the .bmBits member of the returned BITMAP structure for a valid pointer. If filled you're
working with a DIBSection and have direct access to its image data so can manipulate it's image memory directly - This
is the ideal situation for image manipulation.
If on the other hand the bmBits member is set to 0, you're working with a DDB and as such don't have access to its image
data. In this situation you'd need to call GetDIBits() to extract the image data, edit it, then put it back using
SetDIBits(). This method isn't quite as efficient as the former, however will still be a _lot_ faster than individual
PSet()'s/SetPixel()'s.
There's more information on the various GDI articles on my site, however the code there is all for VB6.
Another general technique for speeding up image processing like this is to use lookup tables, this only works when there
is a 1:1 relationship between source and destination pixel values (for example brightness/contrast adjustments.) In
this case you pre-calculate a lookup table with all 256 (*3 if each channel is modified independently) possible input
cases then simply run the image data through this table in the main processing loop. For complex image manipulation
tasks this can greatly reduce the processing time so you're counting in milliseconds rather than seconds.
Hope this helps,

Mike


- Microsoft Visual Basic MVP -
E-Mail: EDais@xxxxxxxx
WWW: Http://EDais.mvps.org/


.



Relevant Pages

  • Re: Fastest way to update a Pictureboxs image
    ... doesn't matter that they're in VB6 because its the same GDI methods and ... For Each pixel in ... working with a DIBSection and have direct access to its image data so can manipulate it's image memory directly - This ... There's more information on the various GDI articles on my site, however the code there is all for VB6. ...
    (microsoft.public.vb.winapi)
  • Re: Fastest way to update a Pictureboxs image
    ... The calculation is a bespoke alteration that matches the 3rd party ... three lookups for each pixel that returns a new RGB value. ... I have an app which updates a picturebox ... calling GDI method's. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: PictureBox Scale Confusion
    ... I was able to drag the GDI vertical ... Scale in set in routine which plots the underlying graphics. ... I'm setting the scale to draw a line in a PictureBox as follows ... Why do I need to rescale the picturebox to its actual pixel ...
    (microsoft.public.vb.general.discussion)
  • Re: Question for Herbert
    ... lea ->r3 ... for one pixel. ... The diffrence, for my code, without prefecth, and with it, was a gain of about ~100 cycles perpixel. ... (You should have seen my face, when I first saw that GDI was faster then then this tight loop). ...
    (alt.lang.asm)
  • Re: How to extract bitmap images from a .ps file?
    ... hexadecimal of the pixels values, scanlines from top to bottom, pixel ... sequence 'dup fil exch writestring' was added to the procedure passed ... to colorimage and then after calling colorimage the file is closed ... image data as raw data in a file, where do you save the width, height, ...
    (comp.lang.postscript)