Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Resizing multiple images runs out of memory

Posted on 2008-10-07
3
Medium Priority
?
842 Views
Last Modified: 2012-05-05
Hi all,

Below is a function that I wrote that resizes images for a website. After it processes about 250 images, I run out of memory, leading me to believe that my dispose isn't working? There are 900 photos to process total. What have I missed?
Function resizeImage(ByVal imageName As String, ByVal newWidth As Integer, ByVal imageType As String, ByVal homeDirectory As String) As String
 
        Dim originalFileName As String
        Dim newFileName As String
        Dim tmpImage As Drawing.Bitmap
        Dim newImage As Drawing.Bitmap
        Dim g As Graphics
        Dim newHeight As Integer
 
        originalFileName = pickupFolder & imageName
        If File.Exists(originalFileName) = True Then
            Try
                tmpImage = New Bitmap(Bitmap.FromFile(originalFileName))
                newHeight = (newWidth * tmpImage.Height) / tmpImage.Width
 
                newImage = New Bitmap(newWidth, newHeight)
                g = Graphics.FromImage(newImage)
 
 
                ' draw it with the new size
                g.DrawImage(tmpImage, 0, 0, newWidth, newHeight)
 
                newFileName = imageType & Replace(imageName, " ", "")
                newImage.Save("c:\inetpub\wwwroot\" & homeDirectory & "\photos\" & newFileName, Drawing.Imaging.ImageFormat.Jpeg)
                g.Dispose()
                newImage.Dispose()
                tmpImage.Dispose()
 
                tmpImage = Nothing
                newImage = Nothing
                g = Nothing
 
                resizeImage = newFileName
 
            Catch ex As Exception
 
                streamWriter.WriteLine(originalFileName & " gave error: " & ex.Message)
                Console.WriteLine(originalFileName & " gave error: " & ex.Message)
 
            End Try
 
        Else
            streamWriter.WriteLine(originalFileName & " does not exist")
            Console.WriteLine(originalFileName & " does not exist")
        End If
 
        tmpImage = Nothing
        newImage = Nothing
        g = Nothing
 
    End Function

Open in new window

0
Comment
Question by:DEWebWorks
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 14

Accepted Solution

by:
rachitkohli earned 2000 total points
ID: 22658453
I would suggest making an image from the file using file stream object.

Dim fs as IO.FileStream
fs=new IO.FileStream("FILEPATH", ACCESSTYPE)
fs.close

tmpImage=Bitmap.FromStream(fs)

'Please check for the syntax, as am directly typing here without testing it...
0
 

Author Closing Comment

by:DEWebWorks
ID: 31503786
Very nice! Thanks.
0
 

Author Comment

by:DEWebWorks
ID: 22658504
Here's the final code:

    Function resizeImage(ByVal imageName As String, ByVal newWidth As Integer, ByVal imageType As String, ByVal homeDirectory As String) As String
 
        Dim originalFileName As String
        Dim newFileName As String
        Dim tmpImage As Drawing.Bitmap
        Dim newImage As Drawing.Bitmap
        Dim g As Graphics
        Dim newHeight As Integer
 
        '---
        Dim fs As IO.FileStream
        '---
 
        originalFileName = pickupFolder & imageName
        If File.Exists(originalFileName) = True Then
            Try
                'tmpImage = New Bitmap(Bitmap.FromFile(originalFileName))
                '---
                fs = New IO.FileStream(originalFileName, FileMode.Open)
 
                tmpImage = Bitmap.FromStream(fs)
                fs.Close()
                '---
 
                newHeight = (newWidth * tmpImage.Height) / tmpImage.Width
 
                newImage = New Bitmap(newWidth, newHeight)
                g = Graphics.FromImage(newImage)
 
 
                ' draw it with the new size
                g.DrawImage(tmpImage, 0, 0, newWidth, newHeight)
                g.Dispose()
 
                newFileName = imageType & Replace(imageName, " ", "")
                newImage.Save("c:\inetpub\wwwroot\" & homeDirectory & "\photos\" & newFileName, Drawing.Imaging.ImageFormat.Jpeg)
 
                newImage.Dispose()
                tmpImage.Dispose()
 
                tmpImage = Nothing
                newImage = Nothing
                g = Nothing
 
                resizeImage = newFileName
 
            Catch ex As Exception
 
                streamWriter.WriteLine(originalFileName & " gave error: " & ex.Message)
                Console.WriteLine(originalFileName & " gave error: " & ex.Message)
 
            End Try
 
        Else
            streamWriter.WriteLine(originalFileName & " does not exist")
            Console.WriteLine(originalFileName & " does not exist")
        End If
 
        tmpImage = Nothing
        newImage = Nothing
        g = Nothing
 
    End Function

Open in new window

0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question