Re: Fastest way to update a Picturebox's image
- From: "Mike D Sutton" <EDais@xxxxxxxx>
- Date: Tue, 25 Jul 2006 09:24:26 +0100
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/
.
- Follow-Ups:
- Re: Fastest way to update a Picturebox's image
- From: kebalex
- Re: Fastest way to update a Picturebox's image
- References:
- Fastest way to update a Picturebox's image
- From: kebalex
- Fastest way to update a Picturebox's image
- Prev by Date: Re: Suspend process
- Next by Date: Re: Fastest way to update a Picturebox's image
- Previous by thread: Re: Fastest way to update a Picturebox's image
- Next by thread: Re: Fastest way to update a Picturebox's image
- Index(es):
Relevant Pages
|