943,077 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Unsolved
  • Views: 1244
  • VB.NET RSS
Jan 10th, 2010
0

How to generate random images from access database using hashset?

Expand Post »
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.


VB.NET Syntax (Toggle Plain Text)
  1. Private Function GetImageFromByteArray(ByVal picData As Byte()) As Image
  2. If picData Is Nothing Then
  3. Return Nothing
  4. End If
  5.  
  6. ' is this is an embedded object?
  7. Dim bmData As Integer = If((picData(0) = 21 AndAlso picData(1) = 28), 78, 0)
  8.  
  9. ' load the picture
  10. Dim img As Image = Nothing
  11. Try
  12. Dim ms As New MemoryStream(picData, bmData, picData.Length - bmData)
  13. img = Image.FromStream(ms)
  14. Catch
  15. End Try
  16.  
  17. ' return what we got
  18. Return img
  19.  
  20. End Function
  21.  
  22. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  23.  
  24. Panel1.Visible = False
  25. Panel2.Hide()
  26.  
  27.  
  28. Dim conn As New OleDbConnection
  29. Dim DA As OleDbDataAdapter
  30. Dim DS As New DataSet
  31.  
  32.  
  33. conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=db1.mdb;User Id=admin;Password=;"
  34.  
  35. DA = New OleDbDataAdapter("Select Empty, EmptyName, Target, TargetName from PicturesDisplayed", conn)
  36. DA.Fill(DS)
  37.  
  38. Me.PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
  39.  
  40.  
  41. Dim oledbconnection As OleDbConnection
  42. 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;")
  43.  
  44. oledbconnection.Open()
  45. Dim command As OleDbCommand
  46. command = New OleDbCommand("select count (*) from PicturesDisplayed", oledbconnection)
  47. Dim count As Integer
  48. count = command.ExecuteScalar()
  49. oledbconnection.Close()
  50.  
  51.  
  52. Dim randomImage As New HashSet(Of Integer)
  53. ' Create a integer and new Random object
  54. Dim intPic As Integer
  55. Dim rand As New Random
  56.  
  57. 'Pick a random number between 0 and the number of images in database
  58. intPic = rand.Next(0, count)
  59. randomImage.Add(intPic)
  60.  
  61. PictureBox1.Height = 256
  62. PictureBox1.Width = 256
  63.  
  64. PictureBox2.Height = 256
  65. PictureBox2.Width = 256
  66.  
  67.  
  68. Dim pic1x As Integer = _
  69. (Me.ClientSize.Width - PictureBox1.Width) \ 2
  70. Dim pic1y As Integer = _
  71. (Me.ClientSize.Height - PictureBox1.Height) \ 2
  72. PictureBox1.Location = New Point(pic1x, pic1y)
  73.  
  74.  
  75. Dim pic2x As Integer = _
  76. (Me.ClientSize.Width - PictureBox2.Width) \ 2
  77. Dim pic2y As Integer = _
  78. (Me.ClientSize.Height - PictureBox2.Height) \ 2
  79. PictureBox2.Location = New Point(pic2x, pic2y)
  80.  
  81. ' Now set the picturebox image equal to the image chosen from the array randomly using the random
  82. PictureBox1.Image = GetImageFromByteArray(DS.Tables(0).Rows.Item(intPic).Item(0))
  83. PictureBox1.Visible = True
  84. PictureBox2.Image = GetImageFromByteArray(DS.Tables(0).Rows.Item(intPic).Item(2))
  85. PictureBox2.Visible = False
  86.  
  87.  
  88. If (randomImage.Count = count) Then
  89. Me.Close()
  90. End If
  91.  
  92. End Sub
Reputation Points: 10
Solved Threads: 0
Newbie Poster
liqin is offline Offline
4 posts
since Jan 2010
Jan 11th, 2010
0
Re: How to generate random images from access database using hashset?
>won't be any repeated images appearing

Delete that row.

VB.NET Syntax (Toggle Plain Text)
  1. ....
  2.  
  3. IF DS.Tables(0).Rows.RowState<>DataRowState.Deleted Then
  4. PictureBox1.Image = GetImageFromByteArray(DS.Tables(0).Rows.Item(intPic).Item(0))
  5. PictureBox1.Visible = True
  6. PictureBox2.Image = GetImageFromByteArray(DS.Tables(0).Rows.Item(intPic).Item(2))
  7. PictureBox2.Visible = False
  8. DS.Tables(0).Rows.Item(intPic).Delete()
  9. End If
Moderator
Reputation Points: 2134
Solved Threads: 1227
Posting Genius
adatapost is offline Offline
6,524 posts
since Oct 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in VB.NET Forum Timeline: Need help new line
Next Thread in VB.NET Forum Timeline: vb.net, crystal reports: looking for overall approach





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC