Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Reading and storing images between database and ImageList control
 

Reading and storing images between database and ImageList control

Is it possible to read an image file from a database (SQL Server 2000 in my case) and pass directly into an ImageList control without creating any bitmap files in a system folder?

With my current code(see below), the image is read from the database and then stored as a bitmap file under a windows folder titled "Tickmarks."  Then, as needed, the various pictureboxes are referenced to the appropriate file in the Tickmarks folder.  However, I don't want the bitmap files exposed to (and possibly manipulated by) the application's user.  Any ideas?

Dim objSQLConn As New SqlClient.SqlConnection(gstrConnString)
Dim objSQLCmd As New SqlClient.SqlCommand()
Dim drSQLDataReader As SqlClient.SqlDataReader
Dim objrow As Data.DataRow
Dim strBitmapPath As String, strFileName As String
Dim imgBitmap() As Byte
Dim lngBitmapSize As Long
Dim objDirInfo As New IO.DirectoryInfo(gstrApplicationPath & "\Tickmarks\")
Dim objFileInfo() As IO.FileSystemInfo
Dim objFileItem As IO.FileSystemInfo

//'assign path to bitmap files
strBitmapPath = gstrApplicationPath & "\Tickmarks\"

//'Assign the connection to the command
objSQLCmd.CommandType = CommandType.StoredProcedure
objSQLCmd.Connection = objSQLConn
objSQLConn.Open()

//'Populate the grid with the firm-wide reserved TM's
objSQLCmd.Parameters.Add("@Reserved", True)
objSQLCmd.CommandText = "1CommonTMGetList"
drSQLDataReader = objSQLCmd.ExecuteReader

While (drSQLDataReader.Read())
......//'Populate the tblTMAll table
......objrow = tblTMAll.NewRow
......objrow("TmID") = drSQLDataReader.Item("TmID")
......objrow("FileName") = drSQLDataReader.Item("FileName")
......objrow("LongDesc") = drSQLDataReader.Item("LongDesc")
......objrow("ShortDesc") = drSQLDataReader.Item("ShortDesc")
......tblTMAll.Rows.Add(objrow)
......//'Create bitmap and each record and place in "Tickmark" folder (if not already existant in folder))
......strFileName = drSQLDataReader.Item("FileName")
......objFileInfo = objDirInfo.GetFileSystemInfos()
......//'Iterate through the Tickmark folder to see whether the current record's file name already exists as a bitmap
......For Each objFileItem In objFileInfo
...........If objFileItem.Attributes <> IO.FileAttributes.Directory And objFileItem.Name = strFileName Then
................If objFileItem.Extension = ".bmp" Then
......................imgBitmap = drSQLDataReader.Item("Symbol")
......................lngBitmapSize = UBound(imgBitmap)
......................Dim fsBitmap As New IO.FileStream(strBitmapPath & strFileName & ".bmp", IO.FileMode.OpenOrCreate, IO.FileAccess.Write)
......................fsBitmap.Write(imgBitmap, 0, lngBitmapSize)
......................fsBitmap.Close()
................End If
...........End If
......Next
End While

drSQLDataReader.Close()
objSQLConn.Close()
MigrationUser 1  Friday, February 07, 2003 5:54 AM
i would imagine so.

you can take the data from the sql field and put it into a New System.IO.MemoryStream.  You can then load that MemoryStream directly into a System.Drawing.Image using the Load Method
MigrationUser 1  Friday, February 07, 2003 12:17 PM
Thanks for pointing me in the right direction Eric!  For anyone interested, added below is the corrected code to get the result I was looking for.

Dim objSQLConn As New SqlClient.SqlConnection(gstrConnString)
Dim objSQLCmd As New SqlClient.SqlCommand()
Dim drSQLDataReader As SqlClient.SqlDataReader
Dim objrow As Data.DataRow
Dim bytBitmap() As Byte, bytCursor() As Byte
Dim lngBitmapSize As Long, lngCursorSize As Long

//'Assign the connection to the command 
objSQLCmd.CommandType = CommandType.StoredProcedure 
objSQLCmd.Connection = objSQLConn 
objSQLConn.Open() 

//'Query the database and populate the grid with the tickmarks 
objSQLCmd.Parameters.Add("@Reserved", True) 
objSQLCmd.CommandText = "TMGetList" 
drSQLDataReader = objSQLCmd.ExecuteReader 

While (drSQLDataReader.Read()) 
......//'Populate the tblTMAll table 
......objrow = tblTMAll.NewRow 
......objrow("TmID") = drSQLDataReader.Item("TmID") 
......objrow("FileName") = drSQLDataReader.Item("FileName") 
......objrow("LongDesc") = drSQLDataReader.Item("LongDesc") 
......objrow("ShortDesc") = drSQLDataReader.Item("ShortDesc") 
......tblTMAll.Rows.Add(objrow) 
......//'Read the bitmap from the db into the ImageList holding all TickMark bitmaps
......bytBitmap = drSQLDataReader.Item("Symbol")
......lngBitmapSize = UBound(bytBitmap)
......Dim objMemStreamBit As New IO.MemoryStream(bytBitmap, 0, lngBitmapSize)
......ImageList.Images.Add(Image.FromStream(objMemStreamBit, True))
......objMemStreamBit.Close()
End While 

drSQLDataReader.Close() 
objSQLConn.Close()
MigrationUser 1  Saturday, February 08, 2003 8:30 AM

You can use google to search for other answers

Custom Search

More Threads

• Input String not in the correct format
• DataGrid bound to IList: Column mapping
• Help with periodically refreshing data in my dataGridView, threaded
• complex control in a single cell
• About DataGridView
• Connecting to an Access database from a Web application
• DataGridView population of non-bound columns
• Data change not being saved if control not exited before clicking Save button
• Fill a ComboBox trough a Stored Procedure in SQL
• When is DataTable.RowChanged event supposed to be triggered?