Draw image HUGE memory load ??????

Draw image HUGE memory load ??????

Post by Mihai Marinesc » Tue, 04 Mar 2003 09:02:16

Hi all,

I need some help here with this simple program. It takes a folder and shows
the images on the screen. The images are about 1.5 MB. The problem is that
the program uses a HUGE load of memory. for 3 pictures it takes about 65 MB
which is very weird. I tried to stream the files and the showing them but
without any luck. What is it wrong with this code ? Is there other ways to
get this done without using so much memory ?

Best Regards

Mihai Marinescu

String[] test = new string[len];

Graphics g = e.Graphics;

int x = 0;

int y = 0;

int width = 120;

int height = 120;

int rX =10;

int rY =10;

int rW =120;

int rH =120;

Rectangle destRect1;

GraphicsUnit units = GraphicsUnit.Pixel;

for (int i=0; i<len; i++)


Image _image = Image.FromFile(test[i]);

_image = _image.GetThumbnailImage(120,120,null, new IntPtr());

destRect1 = new Rectangle(rX,rY,rW,rH);

e.Graphics.DrawImage(_image, destRect1, x, y, width, height, units);




1. HUGE memory usage while loading images

I'm witnessing large consumption of RAM while loading and
unloading very large images (~1.7MB worth of JPEG data)
It's shocking to see your program consume like 200MB of
memory when all it does is view images!

Any suggestions what I should do? Manually called Garbage
collection? Or dispose the object or something like that?

Here's my code that does the loading:-

Private Sub lstFiles_SelectedIndexChanged(ByVal sender As
Object, ByVal e As System.EventArgs) Handles
        If lstFiles.SelectedIndex = -1 Then Exit Sub

        If Not oThread Is Nothing Then
            If oThread.ThreadState =
Threading.ThreadState.Running Then
                'Stop the previous thread first
                oThread = Nothing
            End If
        End If

        ''Create a new thread and ask it to load the
        oThread = New Threading.Thread(AddressOf
End Sub

    Private Sub LoadImage()
        Dim oBitmap As Bitmap
        Dim oBack As Bitmap
            oBitmap = New Bitmap(CStr(lblCurrPath.Text &
            'First get dimensions constrained to picture
box, GetDimensions is a function that constraints resizing
'to the same aspect ratio.

            Dim oPt As Point
            oPt = GetDimensions(oBitmap.Width,
            'Create a new image for the picture box

            oBack = New Bitmap(oBitmap, picDisplay.Width,
            Dim g As Graphics = Graphics.FromImage(oBack)
            g.DrawImage(oBitmap, 0, 0, oPt.X, oPt.Y)
            picDisplay.Image = oBack
            oBitmap = Nothing
            oBack = Nothing
        Catch ex As Threading.ThreadAbortException
            Trace.WriteLine("Loading Interuppted")
            oBitmap = Nothing
            oBack = Nothing
        End Try
    End Sub

2. Font in FP

3. Memory leak when loading Images in thread

4. If I only had the money...

5. Memory Error when loading an Image

6. Regular Expressions

7. MSXML, C# and huge memory leak

8. R5000 performance specs

9. aspnet_wp process taking up *huge* amounts of memory

10. Can i Apply Image attributes to an Image without actually drawing it on the screen?

11. Huge Form, Huge Headache (small classic question)

12. Loading Huge XML Files

13. Gathering metadata from images without loading the entire image