Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ImageList collection max item limit 1

Status
Not open for further replies.

Sorwen

Technical User
Nov 30, 2002
1,641
US
Am I diong something wrong or does ImageList collection have a limit of 356 items. I get the error "Out of Memory", but it doesn't matter if I do images 32 bit or 4 bit or what size it is still stops at 356 so it seems it only matters number of images. Is this the case or am I missing something? If it is the case is there any other way to assign an image to a listview item? I had no luck when I tried making a custom listview/listviewitem.

-I hate Microsoft!
-Forever and always forward.
 
Thanks. I guess it is something with the other parts then. I don't have the exact code with me, but the section of code and the parts that could be wrong should be like this.

First I globally delcare:
Code:
Dim ThumbnailAbort As Image.GetThumbnailImageAbort

Then I for each loop through only picture file names and here is the part where it adds it to the ImageList:
Code:
Dim pic As Image
        pic = Image.FromFile(fullpath)

        picture_il.Images.Add(filename, pic.GetThumbnailImage(120, 120, ThumbnailAbort, System.IntPtr.Zero))

        pic.Dispose()

It fails on the picture_il.image.add with the out of memory. Does anyone know, since there doesn't seem to be a limit on an ImageList, should I be disposing of ThumbnailAbort or System.IntPtr.Zero in some way?

-I hate Microsoft!
-Forever and always forward.
 
I just remembered that chrissie1 showed me something with thumbnails before (which got lost in the search I did). In that the Dim for the thumbnailabort was in the loop. I kept getting an error with it in the loop, but in that example it was set = nothing each time which I had not tried. I'll have to try that when I get home and see if that is it.

-I hate Microsoft!
-Forever and always forward.
 
Nope still out of memory.

-I hate Microsoft!
-Forever and always forward.
 
I just tested this because it was bugging me to know. I was able to put 1000 120x120 px images into an ImageList by writing/running the code below.

Code:
        Dim pic As Image
        pic = Image.FromFile("c:\Test.bmp")
        Dim picList As New ImageList()
        picList.ImageSize = New System.Drawing.Size(120, 120)
        For i As Integer = 1 To 1000
            Dim callback As System.Drawing.Image.GetThumbnailImageAbort
            picList.Images.Add(i, pic.GetThumbnailImage(120, 120, callback, System.IntPtr.Zero))
        Next

Senior Software Developer
 
I wonder what the difference is? I'm on VS 2k5 are you on it or 2k3? I just thought I wonder if maybe the filename it is getting is too long. Since you use a count and I use the filename who knows how long the name on some of these pictures (some are wall papers I've downloaded for testing this). I'll try that when I get home.

-I hate Microsoft!
-Forever and always forward.
 
I did that in vs2005.

Have you put a breakpoint on that line and individually tested all of the objects & methods in it? Use the quickwatch and the immediate window to test everything you can think of. Something is not right, and you just have to find it.


Senior Software Developer
 
yes, everything checks out fine just like the lines before. I'm going to try taking out that image and see what happens. Here is the exact error.

System.OutOfMemoryException was unhandled
Message="Out of memory."
Source="System.Drawing"
StackTrace:
at System.Drawing.Image.GetThumbnailImage(Int32 thumbWidth, Int32 thumbHeight, GetThumbnailImageAbort callback, IntPtr callbackData)
at pmv.pvm_frm.LoadPicPnl(String PicDir) in F:\Sorwen\My Documents\Visual Studio 2005\Projects\pmv\pmv\pvm_frm.vb:line 153
at pmv.pvm_frm.file_tv_AfterSelect(Object sender, TreeViewEventArgs e) in F:\Sorwen\My Documents\Visual Studio 2005\Projects\pmv\pmv\pvm_frm.vb:line 96
at System.Windows.Forms.TreeView.OnAfterSelect(TreeViewEventArgs e)
at System.Windows.Forms.TreeView.TvnSelected(NMTREEVIEW* nmtv)
at System.Windows.Forms.TreeView.WmNotify(Message& m)
at System.Windows.Forms.TreeView.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.SendMessage(HandleRef hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at System.Windows.Forms.Control.SendMessage(Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.Control.ReflectMessageInternal(IntPtr hWnd, Message& m)
at System.Windows.Forms.Control.WmNotify(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at System.Windows.Forms.NativeWindow.DefWndProc(Message& m)
at System.Windows.Forms.Control.DefWndProc(Message& m)
at System.Windows.Forms.TreeView.WmMouseDown(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.TreeView.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(ApplicationContext context)
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
at pmv.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()

I assumed that the system.drawing was due to the fact it was adding an Image to the imagelist, but I guess it could be the the thumbnail instead though I don't know why.

-I hate Microsoft!
-Forever and always forward.
 
That was it. I should have tried it sooner. It was something with the picture. Not sure what though. It's is smaller than most, but there are others around the same pixels and size as well as smaller. Thanks for your help. It really gave me a place to start eliminating causes.

-I hate Microsoft!
-Forever and always forward.
 
I can't help but wonder what it didn't like about that image. Was it a GIF, TIF, ICO, JPG, PNG???

I know that the GetThumbnailImage Method does first look for an embedded thumbnail image within the image... maybe it found something there to fail on. Just strange.

Senior Software Developer
 
Yea I'm wondering too. The file is It was a jpg. The I have no problem viewing the image in the picture box (click on the image and it has a larger preview image or double click and it opens up to size). Also windows viewer and the copy of ACDSee I have no problems, but I don't know that either loads from the thumbnail data at any point.

Maybe once I figure out what I'm going to do long term to get this working how I want it will not matter. Since it all loads into a imagelist it distorts the picture, but I needed something in as a place holder so I could work on the other 90% of the program. I have it preload a place holder image and then replace that image with an actual image of the picture. So I just stuck it all in a Try Catch and on error it just leaves the place holder Image and then continues to load the rest of the pictures. Out of the 1114 (various gif, bmp, and jpg files) it was the only one with a problem.

-I hate Microsoft!
-Forever and always forward.
 
You should just try opening that JPG with Paint and doing a SaveAs... then test the new image.

I have also bumped into that same issue with an ImageList needing all of the images to be the same size. How often does that happen where it's actually useful? You will probably have to use the Graphics class to resize the images properly. This is off the hip, but you may also want to create your own custom Image Collection if there isn't already one that allows images of various sizes. I don't think that would be too hard.


Senior Software Developer
 
Hmmm. I didn't think about that. I was trying to have a custom ListView or ListViewItem, but I could only get it to half work. I'll try it from the angle of a custom Image Collection and see what that gets me.

-I hate Microsoft!
-Forever and always forward.
 
Oddly enough I have actually run into a few GDI memory limitations recently. I am generating a BMP of an audio signal from a WAV file that can be up to an hour long. Depending on the density of the graphing of the samples (ie: 1 pixel = 1 sample. 16000 samples = 1 second. 3600 seconds = 1 hour. adds up to a 57 million pixel wide bitmap). Luckily, I don't imagine my users are going to need to see things at that deep of resolution, but a 50,000 pixel wide bitmap is totally possible. Unfortunately, GDI chokes hard on images anywheres close to that big. Even the GDI+ functions start bombing out. I tried a few different things that did work though.

My first attempt was to use Marshal.copy to copy a specific section of bytes out of the source image and into a new bitmap. That worked for pure bitmaps, but It was sucking up like 1.6TB of memory for the audio file I was working on.

My second attempt was to generate the image in 1200pixel wide chunks. And instead of storing them as Bitmaps, I save them to memory streams (saving a bitmap to a memory stream results in PNG loss-less compression). Then I just track the array of memory streams, pick out the ones close to the part of the audio file the user is currently looking at/listening to, and bitblt the appropriate parts to the image to load in the picture box.

The PNG compression adds a bit of processor overhead, but it cuts the memory requirement from 1.6TB to ~35megs. I also added a width field to my memory stream class so instead of having to generate a bitmap from each stream to get the width for finding the correct images, I just check the integer value associated with the memory stream.

Not that any of that helps you, but yes, there are undocumented (so far as I can find) memory limitations on graphics functions based on GDI.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Those can be fun... I once built a realtime line graph of RFID Tag Read Rates on a handheld CE device. That little dandy built a new image every 1/10th of a second (could have run faster, but what's the point on that tiny screen) and worked pretty well. Had lots of fun with that one! ;o)

Senior Software Developer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top