Click here to Skip to main content
15,884,473 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I'm having issues with adding an image from database to imagelist, the following is the code I use, it returns an image, but when added to image list the image gets all screwed up so it doesn't show.

'Load the image into a byte array
ImageArray = CType(dbrdr("COMP_PICTURE"), Byte())
'Create Memory Stream from Byte Array
Dim ms As New System.IO.MemoryStream(ImageArray)
'Add the memory stream to the image list
Dim img As System.Drawing.Image
img = Image.FromStream(ms)
ms.Close()
ilComponentImages.Images.Add(dbrdr("COMP_ID"), img)
ImageIndex = ilComponentImages.Images.IndexOfKey(dbrdr("COMP_ID"))


Any help appreciated
Posted
Updated 5-Mar-22 20:23pm
Comments
Dalek Dave 23-Aug-10 11:51am    
Edit for Code Block

How are you sure the image is not screwed up already ? How is it screwed up ? Is it that the image has too high a bit depth for the image list ? Some clarity is needed for us to comment further.
 
Share this answer
 
Comments
tiggerc 24-Aug-10 4:58am    
Resolved, thanks everyone for the pointers, it was depth that was causing the issue.
HOW TO ADD IMAGES IN IMAGELIST FROM DAATA BASE
 
Share this answer
 
Comments
Kats2512 14-Aug-17 10:50am    
rubbish post, this is not even a solution.

stop spamming!
Using SQLite as the datasource, works for me

Database has a Table Named "tblImages" with 2 Fields "ID" (Integer) & "ImageBlob" (Blob)
VB
Dim imglist as New ImageList
Dim Database as string = "C:\Images.db"
Dim CommandString As String
Dim CN As New SQLite.SQLiteConnection("Data Source=" & Database & ";Version=3;")
    CN.Open()
Dim cmd As New SQLiteCommand
    cmd.Connection = CN
Dim Da As New SQLiteDataAdapter
Try
imglist.Images.Clear()
imglist.ColorDepth = ColorDepth.Depth32Bit
CommandString = "select * from tblImages"

Dim dt_images As New DataTable
cmd.CommandText = CommandString
Da.SelectCommand = cmd

Da.Fill(dt_images)
For Each dr As DataRow In dt_images.Rows
  Dim img_buffer = CType(dr("ImageBlob"), Byte())
  Dim img_stream As New MemoryStream(img_buffer, True)

  img_stream.Write(img_buffer, 0, img_buffer.Length)
  imglist.Images.Add(dr("ID").ToString(), New Bitmap(img_stream))
  img_stream.Close()
Next
  CN.Close()
Catch ex As Exception
  MsgBox(ex.Message)    
End Try


This will load every Image from the table into a Imagelist, You can either have it declared in code or have one on your form, either will work. Just make sure if you have it declared to link it to the control using its imagelist property if that is what you are planning to do.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900