loading picboxes takes time !!

muskad202
muskad202 used Ask the Experts™
on
Hi
In my program, i am loading a lrage number of picboxes (nearly 10,000 using load picbox(index) method), and it takes a lot of time..(nearly 3 minutes)
anyone have any ideas to speed it up ??
thanks
muskad202

here's the sample code
----------
for i=1 to 10000
  load picbox(i)
  picbox(i).top = ...
  picbox(i).left = ...
  picbox(i).visible = true
next
--------------
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Hi, muskad202

in the first place, why are you loading such a large number of picboxes?
couldn't u like combine all the picboxes into one?

=======CoolBreeze=======
== Cool It, the Breeze is here  ==
=====================
Assuming you have a need to load 10,000 pictureboxes (although I don't see an application for that, but that doesn't mean it doesn't exist...it's just hard to grasp how anyone would be able to view 10,000 pictureboxes in one sitting). I would recommend putting this code in it's own thread with a notify event to tell your program to execute this code asynchronously.  This isn't going to speed things up, but will free your program to do other things (like display a progress bar, etc).  Also consider using images instead of pictureboxes.  And one other optimization:  Put a with around the property sets this puts the picturebox reference in memory instead of having to create a temp memory location each time you reference it.

for i=1 to 10000
 load picbox(i)
 with picbox(i)
   .top = ...
   .left = ...
   .visible = true
 end with
next

With that said, I would consider a different approach.  Prehaps loading only 10 pictureboxes and providing a means to scroll to another set of 10.  Also, the Imagelist control maybe in order here if you don't need to display all 10,000 at once.


quote:

"Put a with around the property sets this puts the picturebox reference in memory instead of having to create a temp memory location each time you reference it."

does using picbox(i).top creates a temp? from what i think i noe, it shouldn't. the compiler should be able to optimize it. of course using a with will make the compiler's job easier though
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

I stand corrected, thanks for that.

Author

Commented:
Hi guys...
actually, the pic boxes are really really small :) thats why they fit on a screen...
i'm trying to make the snake game (nibbles which was present in dos 6.22 , snakey as present in linux..etc), i.e, u control a snake on the screen moving left, right, up, down making sure u don't bang anywhere. in order to make the programming easier, i was colouring individual locations by setting the backcolor property of the picture box at that location.
i would appreciate it if anyone could tell me if there is another way to do it thru just one picturebox, instead of using so many (e.g, can i create a image in memory, and set the color of every pixel, and then just assign that image to the picbox, and after a interval, modify that image by changing the color of some other pixels, and reassigning to the picbox ??)
thanks :)
muskad202
Yes you can do it with a single picturebox (much better technique).  Using the graphics methods on the picturebox (i.e. line, pset, circle, etc) you can draw over a permanent background in the picturebox  just make sure the autoredraw is set to False and it is by default.  So you could load your background picture, then use the craphics methods.  Use cls to clear the foreground thereby erasing your worm. then redraw the worm.

Here's some sample code that shows what I'm talking about:

Create a form with a picturebox (Picture1) that has a loaded bitmap
Then put 2 command buttons on the form (Command1, Command2)
Then paste this code in to your form


Private Sub Command1_Click()
   
    Picture1.Line (10, 10)-(100, 10), , BF
   
End Sub

Private Sub Command2_Click()
   
    Picture1.Cls
    Picture1.Line (100, 10)-(200, 10), , BF
   
End Sub

Author

Commented:
Hi twalgrave ..
Thanks...
just a couple of more questions.
Is there any way to create the image at runtime ?? e.g, i don't want to load the picture from a file, because whenever the worm moves, i need to change the colors of some pixels, etc..so is there a way to do something like

dim x as ImageInMemory
x.width=...
x.height=...
x.pixelat(i,j)=vbred
x.pixelat(k,l)=vbblack
...
picbox1.picture = x

etc...

also, is there any way to draw a colored box inside the picbox, or do i ahve to make do with reapeated "line" function calls ??

Thanks :)
muskad202
OK, the worm is the Picture1.Line (10, 10)-(100, 10), , BF
part of the code.  The picture is the background the worm resides on.  Just before the BF is where you can put the color.  For example:
Picture1.Line (10, 10)-(100, 10), vbred, BF
Picture1.Line (101, 10)-(200, 10), vbBlack, BF

You can increase the width of the worm body by doing something like this
    Picture1.Line (10, 10)-(100, 100), vbRed, BF
    Picture1.Line (101, 10)-(200, 100), vbBlack, BF


Would create a worm with a red head and a black body facing left.  Here's the info from the help file:

Line Method
     

Draws lines and rectangles on an object.

Syntax

object.Line [Step] (x1, y1) [Step] - (x2, y2), [color], [B][F]

The Line method syntax has the following object qualifier and parts:

Part Description
object Optional.Object expression that evaluates to an object in the Applies To list. If object is omitted, the Form with thefocus is assumed to be object.
Step Optional.Keyword specifying that the starting point coordinates are relative to the current graphics position given by the CurrentX and CurrentY properties.
(x1, y1) Optional. Single values indicating the coordinates of the starting point for the line or rectangle.  The ScaleMode property determines the unit of measure used.  If omitted, the line begins at the position indicated by CurrentX and CurrentY.
Step Optional. Keyword specifying that the end point coordinates are relative to the line starting point.
(x2, y2) Required. Single values indicating the coordinates of the end point for the line being drawn.
color Optional. Long integer value indicating the RGB color used to draw the line. If omitted, the ForeColor property setting is used. You can use the RGB function or QBColor function to specify the color.
B Optional. If included, causes a box to be drawn using the coordinates to specify opposite corners of the box.
F Optional. If the B option is used, the F option specifies that the box is filled with the same color used to draw the box. You cannot use F without B. If B is used without F, the box is filled with the current FillColor and FillStyle. The default value for FillStyle is transparent.


Remarks

To draw connected lines, begin a subsequent line at the end point of the previous line.

The width of the line drawn depends on the setting of the DrawWidth property. The way a line or box is drawn on the background depends on the setting of the DrawMode and DrawStyle properties.

When Line executes, the CurrentX and CurrentY properties are set to the end point specified by the arguments.

This method cannot be used in an With…End With block.
wished i was the one, but u beat me to it, twalgrave :)

but just to point out, the common practice is to have something like a picbox, get the device context of the picbox. then we work on the device context directly instead of the picbox, it gives us more flexibility as we can access the device context directly, such as plotting certain pixels. it maps a certain area of memory to the display, so what we do is to draw on this memory.

in addition to this display device context, we also create memory device context. this memory device context is not displayed but then stored a duplicate of the display device context. whatever we want to change, we draw it to this memory device context. when we are done, we use a very fast function called bitblt to transfer it to the display device context. the purpose of this is to reduce flickering, drawing on the display device context takes time and sometimes people can see u drawing on it. the result is that it is not smooth and people sees flickers.
Coolbreeze,

I agree with your method IF this were going to be a commercial graphics app.  In this case because it is a simple game requiring simple mechanisms, the picturebox, Graphics methods, and CLS work GREAT.

that's y i say for his info only...

Author

Commented:
hi guys !
just wanted to ask, would be the bitblt function be faster than the line method ??
thanks
muskad202
Yes it would be faster, but increases the amount of code.  Here's a site that has various links to game programming in VB.  Many of them contain examples of using the graphics API such as bitblt.

http://www.vb-bookmark.com/vbGame.html

and

http://www.vb-links.com/game1.htm

some links are dead, but there's alot of info there to include the APIs, using DirectX in game programming with VB, etc.

muskad202

You decided to award points: 30 for twalgrave, 20 for Coolbreeze.

I have reduced the points on this question from 50 to 30 as indicated by your request at Community Support. Please copy the URL and create a new question in this topic area for the other Experts to whom you wish to award points. The title of the question should read "Points for", followed by the Expert's name. In the question itself, you should paste the link to the original question and perhaps a comment stating that the points are for their help with that question. Once you have created the new questions, you can go back to the original, and accept the comment from the Expert for whom you did not create a new question. The Experts will  comment in your new "Points for" question(s), which you then accept and grade to close.
If you have any questions, or would prefer that a Moderator perform these actions for you, please don't hesitate to ask.
Thank you.

** Mindphaser - Community Support Moderator **

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial