Listview Image Gallery - Out of Memory exception

2 posts, 0 answers
  1. cimscims
    cimscims avatar
    14 posts
    Member since:
    Jul 2010

    Posted 09 Feb 2012 Link to this post

    I am using the below sample to build a listview image gallery.

    I am getting the Out of Memory exception. I have attached the code and error.

    Would you please help me with this problem.
    Imports System
    Imports System.Collections.Generic
    Imports System.Drawing.Imaging
    Imports System.IO
    Imports System.Linq
    Imports System.Web
    Imports System.Drawing
    Imports System.Threading
    Namespace Data
        Public NotInheritable Class DataProvider
            Private Sub New()
            End Sub
            <ThreadStatic()> Private Shared _photos As List(Of Photo)
            Public Shared Function GetData() As IList(Of Photo)
                If _photos IsNot Nothing Then
                    Return _photos
                End If
                _photos = New List(Of Photo)()
                For Each file As String In Directory.GetFiles(HttpContext.Current.Server.MapPath("Res1\User1\Residence1\"))
                    Dim photo = New Photo()
                    photo.Name = Path.GetFileName(file)
                    Dim image__1 As Image = Image.FromFile(file)
                    Using memoryStream = New MemoryStream()
                        image__1.Save(memoryStream, ImageFormat.Png)
                        photo.Data = memoryStream.ToArray()
                    End Using
                Return _photos
            End Function
        End Class
        Public Class Photo
            Private Shared ReadOnly _key As New Object()
            <ThreadStatic()> Private Shared _counter As Integer
            Public Sub New()
                Id = GetId()
            End Sub
            Public Property Name() As String
                    Return m_Name
                End Get
                Set(ByVal value As String)
                    m_Name = value
                End Set
            End Property
            Private m_Name As String
            Public Property Data() As Byte()
                    Return m_Data
                End Get
                Set(ByVal value As Byte())
                    m_Data = value
                End Set
            End Property
            Private m_Data As Byte()
            Public Property Id() As Integer
                    Return m_Id
                End Get
                Private Set(ByVal value As Integer)
                    m_Id = value
                End Set
            End Property
            Private m_Id As Integer
            Protected Shared Function GetId() As Integer
                SyncLock _key
                    _counter += 1
                End SyncLock
                Return _counter
            End Function
        End Class
    End Namespace

  2. Veli
    Veli avatar
    2002 posts

    Posted 14 Feb 2012 Link to this post

    Hi cims,

    We cannot identify the cause of this exception from the source code you provided. As it appears in the stack trace, your data provider (source code in App_Code) fails with this exception, seemingly after an attempt to retrieve an image from file. The exception can be caused by a very large image file you may be attempting to retrieve. If this issue persists, consider opening a regular support ticket where you can send us a stripped down runnable project demonstrating the exception. Thus, we will be able to better understand your scenario, test the application locally and advise you further.

    the Telerik team
    Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
Back to Top