locked
Out of Memory Exception (working with Images) RRS feed

  • Question

  • Hello All,

    I am facing a strange issue while dealing with big images ( size approx 48 mb, a Tiff file having 175 pages.). Now when I am trying to convert the Imagefile into the bitmap and doing some operation, randomly system will throw "Out of memory exception"

    Below is my code snippet. I am getting error (randomly) on line " pages = New Bitmap(lorigionalFile) " where LoriginalFile is an ImageType and Page is Bitmap.

     

    So Please guide me what to do to remove this type of error or stoping the memory leakage.

     

    ----------------------------------------------------------------------------------------------------------

                Dim lorigionalFile As System.Drawing.Image
                Dim SaveEncodeParam As EncoderParameter 'Encoder parameter to create multi page image
                Dim EncoderParams As EncoderParameters = New EncoderParameters(1) 'Encoder parameter Array
                Dim pages As Bitmap 'Used to save image page
                Dim NextPage As Bitmap 'Used to save next image page
                Dim PageNumber As Integer
                Lfr = File.Open(FileName, FileMode.Open, FileAccess.ReadWrite)

                lorigionalFile = System.Drawing.Image.FromStream(Lfr)

                ''Get number of page count in image
                PageNumber = getPageNumber(lorigionalFile)

                'set first page as active frame
                cintPagenumber = PageNumber

                'loop to every page of attached document
                For i As Integer = 0 To PageNumber - 1
                    'set active from as per loop variable
                    lorigionalFile.SelectActiveFrame(FrameDimension.Page, i)

    ''Will get errow in below line, when there are other programs running in background

                    pages = New Bitmap(lorigionalFile)


    ''''

                       'image store in Image
                        CalImageContainer.Add(pages)
                        'make a copy on image container
                        CalImageContainerCopy.Add(pages)

                Next
                Lfr.Close()

            End Try

     

     

     

    Thanks

    Pratik vohera

    Thursday, June 16, 2011 11:58 AM

Answers

  • Hi,

    I think the main problemis a design thing. What I understood:

    You got a compressen file with a 175 Bitmaps in it. And you are creating these 175 Bitmaps in Memory now (Where they will be stored without compression!). So you are using a lot of memory to store these files and yes: 175 Bitmaps consume a lot of memory, so it is very likely that you run out of memory.

    And also another hint: Bitmaps implement IDisposable - so when you no longer need a Bitmap, please call Dispose().

    With kind regards,

    Konrad

    • Proposed as answer by Paul Zhou Monday, June 20, 2011 9:27 AM
    • Marked as answer by Paul Zhou Friday, June 24, 2011 5:48 AM
    Thursday, June 16, 2011 12:16 PM

All replies

  • Hi,

    I think the main problemis a design thing. What I understood:

    You got a compressen file with a 175 Bitmaps in it. And you are creating these 175 Bitmaps in Memory now (Where they will be stored without compression!). So you are using a lot of memory to store these files and yes: 175 Bitmaps consume a lot of memory, so it is very likely that you run out of memory.

    And also another hint: Bitmaps implement IDisposable - so when you no longer need a Bitmap, please call Dispose().

    With kind regards,

    Konrad

    • Proposed as answer by Paul Zhou Monday, June 20, 2011 9:27 AM
    • Marked as answer by Paul Zhou Friday, June 24, 2011 5:48 AM
    Thursday, June 16, 2011 12:16 PM
  • the size of tiff can be an issue
    Mark Answered, if it solves your query
    Rohit Arora
    Thursday, June 16, 2011 1:43 PM