I'm so confused when I'm using ImageList in .NET. First of all I have a global object (ImageManager) that contains three different ImageLists:
smallImages
(size: 16x16) - contains 50 images (added from Designer)largeImages
(size: 48x48) - contains 0 imagesgridImages
(size: 16x16) - contains 17 images (added from Designer)
ImageManager is initiliazed once on startup. gridImages
is used in Grid control (JanusGridEx) and it's passed by value. In runtime (in Grid control) I'm adding three new images to gridImages
. I was trying to add it in three difference ways:
- First try
Dim ImageBuffer As Byte() = CType(DataRow(pColumnImageName), Byte()) gridImages.Images.Add(imageKey, New Icon(New MemoryStream(ImageBuffer)))
- Second try
Dim ImageBuffer As Byte() = CType(DataRow(pColumnImageName), Byte()) Dim newImage As Image = Image.FromStream(New MemoryStream(ImageBuffer)) gridImages.Images.Add(newImage)
- Third try (I think this is the best way - correct me if I'm wrong)
Dim ImageBuffer As Byte() = CType(DataRow(pColumnImageName), Byte()) Using ms As New MemoryStream(ImageBuffer , 0, ImageBuffer.Length) gridImages.Images.Add(imageKey, Image.FromStream(ms)) End Using
Now the problem is, when the last image is added to that gridImages
and I want to ie. get the last Image by using imageKey or index (gridImages.Images(20))
I receive exception: System.ArgumentException: Parameter is not valid.
callstack:
System.ArgumentException: Parameter is not valid. at System.Drawing.Bitmap.LockBits(Rectangle rect, ImageLockMode flags, PixelFormat format, BitmapData bitmapData) at System.Drawing.Bitmap.LockBits(Rectangle rect, ImageLockMode flags, PixelFormat format) at System.Windows.Forms.ImageList.GetBitmap(Int32 index) at System.Windows.Forms.ImageList.ImageCollection.get_Item(Int32 index) at Janus.Windows.GridEX.Internal.JNSBB.a(Graphics , Brush , String , Image , Rectangle , ImageHorizontalAlignment , ImageVerticalAlignment , JNSDQ , Font , Boolean , Int32 , ImageList , Boolean ) at Janus.Windows.GridEX.EditControls.JNSAQ.a(Graphics , Int32 , Rectangle ) at Janus.Windows.GridEX.EditControls.JNSAQ.a(Graphics ) at Janus.Windows.GridEX.EditControls.JNSAQ.OnPaint(PaintEventArgs e) at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer) at System.Windows.Forms.Control.WmPaint(Message& m) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.ScrollableControl.WndProc(Message& m) at System.Windows.Forms.ContainerControl.WndProc(Message& m) at Janus.Windows.GridEX.EditControls.JNSAQ.WndProc(Message& msg) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, >IntPtr lparam)
First Question: Why is that? When I create a new instance of Image and even try to save it on a disk it works correctly.
Second Question: Before I pass the ImageList by value to the Grid control I create a new instance of ImageList and copy all the images from the ImageList (ImageManager) to that instance (DeepCopy). When I make the DeepCopy and add new Images to the new ImageList it works fine too. Why is that? Why is it working?