Image Control Box...LoadPicture() is slow...-VBForums
Results 1 to 5 of 5

Thread: Image Control Box...LoadPicture() is slow...

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2016
    Posts
    80

    Image Control Box...LoadPicture() is slow...

    Hello everyone,

    So I am trying to use an image control box to play an animation. I know that it cannot it play an animated .Gif file. So what I am trying to do is, use a series of images that will loop through the image control box in a certain order and speed to play the animation.

    The animation itself is controlled by a MouseDown event that is bound to the image control box. When the event occurs, I track the X,Y position of the cursor on screen as it moves in real time. This way I can loop the images in the control box left or right according to the direction that I click and drag my mouse. So when a MouseDown event occurs, I track the coordinates of the mouse and loop images through the control box. When the MouseUp event occurs, the animation stops.

    Without getting into more details about the mouse movement code, I am having issues actually loading images fast enough to get a decent animation to play.

    I am using LoadPicture() to load the images. The image files are numerically named, so its easy to track the order in which they should load. Is there a faster way to load images in a control box?

    I saw somebody suggest putting the images into a bitmap array so they will load faster. However wouldn't I need GDI+ to do that? Not sure if I can use that library in Excel VBA. I can't reference any .DLLs either.

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    39,426

    Re: Image Control Box...LoadPicture() is slow...

    If you are likely to load them repeatedly, using an array is the way to go - because then you only do the loading once.

    If the images were very large it might not be a good idea (because the memory usage could be huge), but it doesn't sound like that is the case.

    Quote Originally Posted by JohnnyWaffles View Post
    I saw somebody suggest putting the images into a bitmap array so they will load faster. However wouldn't I need GDI+ to do that? Not sure if I can use that library in Excel VBA. I can't reference any .DLLs either.
    You don't need GDI+, you can use the features already built in.

    I think that LoadPicture returns a StdPicture object, so simply using an array of StdPicture should do it.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Dec 2016
    Posts
    80

    Re: Image Control Box...LoadPicture() is slow...

    Okay, thank you for the fast response! So I went ahead and tried using the StdPicture object as an array. I think I am doing it correctly, but not entirely sure.
    It didn’t really improve the speed of loading images that much. The difference is barely noticeable.
    The images themselves are very small. They are JPEGs. I have 90 of them and they total 2.5 MB.

    Here is some of my code:


    Code:
     
           Dim imageFiles() As Integer
           Dim ImageArray() As New StdPicture
           Dim e As Integer
           Dim i As Integer
    
    
           'Get image files in directory to process
            imageFiles = GetFiles("C:\Users\User\Desktop\images\")
    
            'Function to sort array numerically, True for ascending order sort,
            'False for descending order sort
            imageFiles = BubbleSrt(imageFiles, True)
    
            For i = 0 To UBound(imageFiles)
                ReDim Preserve ImageArray(e)
          Set ImageArray(e) = LoadPicture("C:\Users\User\Desktop\images\" & imageFiles(i) & ".jpg")
                e = e + 1
            Next
    
    	‘ImageIndex.Value is a cell reference that is a result of the mouse movement
    	‘The range of numbers only cycles between 1 and 90 depending on the direction
    	‘of my mouse movement (left or right in X axis).
    
               Set Worksheets("Navigation").Image1.Picture = ImageArray(ImageIndex.Value)
    What do you guys think?
    Last edited by JohnnyWaffles; Oct 6th, 2017 at 08:18 AM.

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    39,426

    Re: Image Control Box...LoadPicture() is slow...

    Are you loading the images in the same routine you are showing them?

    That would negate the benefits, and you should be loading them in advance (the array should be declared outside any sub/function, so it keeps its values).


    Also note that this:
    Code:
            For i = 0 To UBound(imageFiles)
                ReDim Preserve ImageArray(e)
    ...would be more efficient like this:
    Code:
            ReDim Preserve ImageArray(UBound(imageFiles))
            For i = 0 To UBound(imageFiles)
    (the difference is likely to be very small, but this is faster and uses less memory)

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Dec 2016
    Posts
    80

    Re: Image Control Box...LoadPicture() is slow...

    Quote Originally Posted by si_the_geek View Post
    Are you loading the images in the same routine you are showing them?

    That would negate the benefits, and you should be loading them in advance (the array should be declared outside any sub/function, so it keeps its values).
    The arrays are actually declared publicly at the top of the module. Otherwise I couldn't use them throughout the rest of the subroutine. They are actually inside of an If Statement that is dependent on a MouseMove event. I didn't put in all of the code because I thought most of it would be confusing, as it is simply referring to worksheet references.

    At it's core though, the code I posted is doing most of the work. Collecting the files in a directory, sorting them numerically and then assigning them to the StdPicture Object. Also, thanks for the tip on the Redim Preserve Statement. Very good to know.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.