This is a migrated thread and some comments may be shown as answers.

Listview Image Gallery - Out of Memory exception

1 Answer 114 Views
ListView
This is a migrated thread and some comments may be shown as answers.
cimscims
Top achievements
Rank 1
cimscims asked on 09 Feb 2012, 04:16 PM
I am using the below sample to build a listview image gallery.

http://demos.telerik.com/aspnet-ajax/controls/examples/integration/listviewsliderwindowrotator/defaultcs.aspx?product=listview

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
                _photos.Add(photo)
            Next
            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
            Get
                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()
            Get
                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
            Get
                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

1 Answer, 1 is accepted

Sort by
0
Veli
Telerik team
answered on 14 Feb 2012, 01:29 PM
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.

Veli
the Telerik team
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
Tags
ListView
Asked by
cimscims
Top achievements
Rank 1
Answers by
Veli
Telerik team
Share this question
or