I am trying to generate random images from access database using hashset as it is said to be unique and there won't be any repeated images appearing. But, it doesn't seem to work.

How can i get it to work? By the way, i am very new to the concept of hashset.

Private Function GetImageFromByteArray(ByVal picData As Byte()) As Image
        If picData Is Nothing Then
            Return Nothing
        End If

        ' is this is an embedded object? 
        Dim bmData As Integer = If((picData(0) = 21 AndAlso picData(1) = 28), 78, 0)

        ' load the picture 
        Dim img As Image = Nothing
        Try
            Dim ms As New MemoryStream(picData, bmData, picData.Length - bmData)
            img = Image.FromStream(ms)
        Catch
        End Try

        ' return what we got 
        Return img

    End Function

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Panel1.Visible = False
        Panel2.Hide()


        Dim conn As New OleDbConnection
        Dim DA As OleDbDataAdapter
        Dim DS As New DataSet


        conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=db1.mdb;User Id=admin;Password=;"

        DA = New OleDbDataAdapter("Select Empty, EmptyName, Target, TargetName from PicturesDisplayed", conn)
        DA.Fill(DS)

        Me.PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage


        Dim oledbconnection As OleDbConnection
        oledbconnection = New System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\Administrator\Desktop\db1.mdb;Jet OLEDB:System Database=system.mdw;")

        oledbconnection.Open()
        Dim command As OleDbCommand
        command = New OleDbCommand("select count (*) from PicturesDisplayed", oledbconnection)
        Dim count As Integer
        count = command.ExecuteScalar()
        oledbconnection.Close()


        Dim randomImage As New HashSet(Of Integer)
        ' Create a integer and new Random object        
        Dim intPic As Integer
        Dim rand As New Random

        'Pick a random number between 0 and the number of images in database 
        intPic = rand.Next(0, count)
        randomImage.Add(intPic)

        PictureBox1.Height = 256
        PictureBox1.Width = 256

        PictureBox2.Height = 256
        PictureBox2.Width = 256


        Dim pic1x As Integer = _
        (Me.ClientSize.Width - PictureBox1.Width) \ 2
        Dim pic1y As Integer = _
        (Me.ClientSize.Height - PictureBox1.Height) \ 2
        PictureBox1.Location = New Point(pic1x, pic1y)


        Dim pic2x As Integer = _
        (Me.ClientSize.Width - PictureBox2.Width) \ 2
        Dim pic2y As Integer = _
        (Me.ClientSize.Height - PictureBox2.Height) \ 2
        PictureBox2.Location = New Point(pic2x, pic2y)

        ' Now set the picturebox image equal to the image chosen from the array randomly using the random
        PictureBox1.Image = GetImageFromByteArray(DS.Tables(0).Rows.Item(intPic).Item(0))
        PictureBox1.Visible = True
        PictureBox2.Image = GetImageFromByteArray(DS.Tables(0).Rows.Item(intPic).Item(2))
        PictureBox2.Visible = False


        If (randomImage.Count = count) Then
            Me.Close()
        End If

    End Sub

>won't be any repeated images appearing

Delete that row.

....

IF DS.Tables(0).Rows.RowState<>DataRowState.Deleted Then
 PictureBox1.Image = GetImageFromByteArray(DS.Tables(0).Rows.Item(intPic).Item(0))
        PictureBox1.Visible = True
        PictureBox2.Image = GetImageFromByteArray(DS.Tables(0).Rows.Item(intPic).Item(2))
        PictureBox2.Visible = False
        DS.Tables(0).Rows.Item(intPic).Delete()
End If
This article has been dead for over six months. Start a new discussion instead.