开发者

Out of Memory Exception while working with Images in .NET(VB.Net 2008)

开发者 https://www.devze.com 2023-03-13 05:29 出处:网络
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, r

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 of ImageType and Page is of 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开发者_JS百科
            lorigionalFile.SelectActiveFrame(FrameDimension.Page, i)

            'Will get errow in below line(randomly), 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


I don't know VB, but it looks like you create a new bitmap memory in your for-loop above without freeing the memory in between.

Use Imagemagick instead to extract images from tif and then operate on each of those instead.

Just a thought.

0

精彩评论

暂无评论...
验证码 换一张
取 消