dcsimg
Results 1 to 10 of 10

Thread: [RESOLVED] Images causing Out of Memory Condition

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2016
    Posts
    9

    Resolved [RESOLVED] Images causing Out of Memory Condition

    Hi Everyone,

    A little background. I have an application that creates picture communication boards. The boards may contain up to 40 images. The pictures and images that I include have all been resized, so the application has been running smoothly without running out of memory. Since I know most people don't have my photo editing skills, they will attempt to use their images. Also, this application will probably run on tablets which may not have the memory capacity.

    I am checking for images that are more than 200KB in size and if so, extracting the thumbnails. I am disposing the images and running Garbage Collect, but I'm still having issues. My changes have allowed maybe 7 or 8 images to display before memory issues pop up plus the application is slower than a turtle.

    Can the size, width, and height of the image be checked without loading it into memory?

    Thanks for any help!!!!

    Here is the code where the images are displayed (only code related to images are below):
    Code:
                    Call ResizeImage()
    
                             
                    Image.FromFile(gstrPicture(intIndex)).Dispose()             'to free up memory
    
                    GC.Collect()
    
                  
                    If _MyTemplateStatus = AppTemplateStatus.FontBorderChanged Then
                        
                        If File.Exists(gstrPicture(intIndex)) Then
                           Dim bnewBitMap As New Bitmap(bMyImage, newWidth, newHeight)
                            bMyImage.Dispose()
                            lblSymbols.Image = bnewBitMap
                            bnewBitMap = Nothing
      
                    End If
    
                     'Dispose Images
                    GC.Collect()

    Here is the code to resize the image:


    Code:
     Public Sub ResizeImage()
            'Get thumbnail if image is larger that 200k
            bMyImage = Nothing
    
            'Resize image
                Dim fileLengthb = New FileInfo(gstrPicture(intIndex)).Length
                If fileLengthb > gconMaxSize Then                      'size larger than 200kb
                    bMyImage = Image.FromFile(gstrPicture(intIndex))
                    intThumbWidth = bMyImage.Width / 10
                    intThumbHeight = bMyImage.Height / 10
                    bMyImage = bMyImage.GetThumbnailImage(intThumbHeight, intThumbWidth, Nothing, New IntPtr)
                Else
                    bMyImage = Image.FromFile(gstrPicture(intIndex))
                End If
    
                sngWidth = bMyImage.Width
                sngHeight = bMyImage.Height
               
            sngHeightRatio = 1
            sngWidthRatio = 1
    
            If sngWidth > sngHeight Then
                sngHeightRatio = sngHeight / sngWidth
            End If
            If sngWidth < sngHeight Then
                sngWidthRatio = sngWidth / sngHeight
            End If
       
        End Sub

  2. #2
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    5,201

    Re: Images causing Out of Memory Condition

    I may be wrong, but this doesn't look right to me.

    In the resize code you're setting bMyImage to Nothing without checking to see if it already had an image in it and disposing it.

    This also looks pointless to me, but I could be wrong:

    Image.FromFile(gstrPicture(intIndex)).Dispose() 'to free up memory

    It looks like you're loading an image from a file and immediately disposing it. Why load it to begin with if you're going to immediately dispose it. That isn't freeing up any memory, it should just use some and immediately not use it, so you don't gain any memory, i.e. you don't waste any memory you just waste a bit of time.

    Here you use bMyImage twice without disposing the first usage.
    I guess you do that since you're using bMyImage on both sides of the assignment, but that seems like it would be a resource leak as well.
    Code:
                    bMyImage = Image.FromFile(gstrPicture(intIndex))
                    intThumbWidth = bMyImage.Width / 10
                    intThumbHeight = bMyImage.Height / 10
                    bMyImage = bMyImage.GetThumbnailImage(intThumbHeight, intThumbWidth, Nothing, New IntPtr)
    Last edited by passel; Jul 12th, 2019 at 01:13 PM.

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    40,938

    Re: Images causing Out of Memory Condition

    Image.FromFile() loads a image file from disk, so this line doesn't make much sense:
    Code:
                    Image.FromFile(gstrPicture(intIndex)).Dispose()             'to free up memory
    ...this takes the time to load the file (again), and stores it in memory (again), just to the mark the memory that this new copy has used as no longer required.


    The order of your first code snippet also seems a bit odd, but that may be just because of how you cut it down to post. This is how I think it should be:
    Code:
                    If _MyTemplateStatus = AppTemplateStatus.FontBorderChanged Then
                        
                        If File.Exists(gstrPicture(intIndex)) Then
                           Call ResizeImage()
                           Dim bnewBitMap As New Bitmap(bMyImage, newWidth, newHeight)
                           bMyImage.Dispose()
                           lblSymbols.Image = bnewBitMap
                           bnewBitMap = Nothing  
                           'Dispose Images
                           GC.Collect()
                    End If
    Also note that it would probably be better to convert ResizeImage to a function (which returns the value in bMyImage) and use a variable inside the first snippet to store it. Narrowing the scope of the variable will reduce the chances of memory issues.


    edit: oops... it seems I took a while, passel got in a few minutes before me!

  4. #4

    Thread Starter
    New Member
    Join Date
    Jul 2016
    Posts
    9

    Re: Images causing Out of Memory Condition

    Quote Originally Posted by passel View Post
    I may be wrong, but this doesn't look right to me.

    In the resize code you're setting bMyImage to Nothing without checking to see if it already had an image in it and disposing it.

    This also looks pointless to me, but I could be wrong:

    Image.FromFile(gstrPicture(intIndex)).Dispose() 'to free up memory

    It looks like you're loading an image from a file and immediately disposing it. Why load it to begin with if you're going to immediately dispose it. That isn't freeing up any memory, it should just use some and immediately not use it, so you don't gain any memory, i.e. you don't waste any memory you just waste a bit of time.

    Here you use bMyImage twice without disposing the first usage.
    I guess you do that since you're using bMyImage on both sides of the assignment, but that seems like it would be a resource leak as well.
    Code:
                    bMyImage = Image.FromFile(gstrPicture(intIndex))
                    intThumbWidth = bMyImage.Width / 10
                    intThumbHeight = bMyImage.Height / 10
                    bMyImage = bMyImage.GetThumbnailImage(intThumbHeight, intThumbWidth, Nothing, New IntPtr)
    Thank you for your reply. I'm confused on this dispose. You (and si_the_geek also) commented on the Image.FromFile(gstrPicture(intIndex)).Dispose() not making any sense. This code is after calling ResizeImage. How should I be Disposing it?

  5. #5

    Thread Starter
    New Member
    Join Date
    Jul 2016
    Posts
    9

    Re: Images causing Out of Memory Condition

    Quote Originally Posted by si_the_geek View Post
    Image.FromFile() loads a image file from disk, so this line doesn't make much sense:
    Code:
                    Image.FromFile(gstrPicture(intIndex)).Dispose()             'to free up memory
    ...this takes the time to load the file (again), and stores it in memory (again), just to the mark the memory that this new copy has used as no longer required.


    The order of your first code snippet also seems a bit odd, but that may be just because of how you cut it down to post. This is how I think it should be:
    Code:
                    If _MyTemplateStatus = AppTemplateStatus.FontBorderChanged Then
                        
                        If File.Exists(gstrPicture(intIndex)) Then
                           Call ResizeImage()
                           Dim bnewBitMap As New Bitmap(bMyImage, newWidth, newHeight)
                           bMyImage.Dispose()
                           lblSymbols.Image = bnewBitMap
                           bnewBitMap = Nothing  
                           'Dispose Images
                           GC.Collect()
                    End If
    Also note that it would probably be better to convert ResizeImage to a function (which returns the value in bMyImage) and use a variable inside the first snippet to store it. Narrowing the scope of the variable will reduce the chances of memory issues.


    edit: oops... it seems I took a while, passel got in a few minutes before me!
    Thanks for replying. There is a lot of code after the call to ResizeImage, but I could rework it. Thanks for the tips.

  6. #6
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    5,201

    Re: Images causing Out of Memory Condition

    Quote Originally Posted by AngelSpeaks View Post
    Thank you for your reply. I'm confused on this dispose. You (and si_the_geek also) commented on the Image.FromFile(gstrPicture(intIndex)).Dispose() not making any sense. This code is after calling ResizeImage. How should I be Disposing it?
    The image you loaded in the ResizeImage is referenced by bMyImage.
    Once you're done with the loaded image, you would dispose of that, i.e. bMyImage.Dispose, which is what si_the_geek showed in his post.

    But I believe this line:

    bMyImage = bMyImage.GetThumbnailImage(intThumbHeight, intThumbWidth, Nothing, New IntPtr)

    in the resize image method is flawed, because bMyImage is referenceing a bitmap and you clobber that reference with the return value of the GetThumbnailImage method call. You should be using a different variable on the left side of the = so that after you have the thumbnail, you can do a bMyImage.Dispose to get rid of the big bitmap. Of course, once you disposed of the big bitmap, you could then assign the local variable used on the left side of the = to bMyImage so bMyImage would reference the thumbnail.
    Code:
    Dim locBitmap as Bitmap = bMyImage.GetThumbnailImage(intThumbHeight, intThumbWidth, Nothing, New IntPtr)
    bMyImage.Dispose
    bMyImage = locBitmap

  7. #7

    Thread Starter
    New Member
    Join Date
    Jul 2016
    Posts
    9

    Re: Images causing Out of Memory Condition

    Thanks again Passel. The line in question was copied from an example that I found. I thought it was a bit weird too.

    Made the change you suggested, it's still on the slow side, but the whole communication board loaded. Thanks. I will continue to retweek using what you and si_the_geek suggest.
    Last edited by AngelSpeaks; Jul 13th, 2019 at 06:45 AM.

  8. #8
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,067

    Re: Images causing Out of Memory Condition

    You may gain some speed by commenting out the GC.Collects. If you get the disposing right, GC.Collect shouldn't be necessary. The effect of Collect is to force an immediate round of garbage collecting, with a processing overhead each time, instead of leaving it to the more efficient logic of the Garbage Collector. BB

  9. #9

    Thread Starter
    New Member
    Join Date
    Jul 2016
    Posts
    9

    Re: Images causing Out of Memory Condition

    Thanks for the suggestion boops boops.

    I did comment it out, but noticed no improvement. It was worth a try!
    Last edited by AngelSpeaks; Jul 14th, 2019 at 07:43 AM.

  10. #10

    Thread Starter
    New Member
    Join Date
    Jul 2016
    Posts
    9

    Re: Images causing Out of Memory Condition

    Update, my out of memory condition seems to be resolved, the larger images are taking a long time to load. I am not surprised about this, I have a lot of very large images on my computer (I'm an amateur photographer) and even File Explorer takes a long time when I view my folders in large or medium icon view.

    I did add a message box warning the user that they should consider resizing the images.

    Thanks everyone for your help.

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