2 Replies - 11140 Views - Last Post: 05 June 2008 - 02:15 AM Rate Topic: -----

#1 arvindmewara  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 26-May 08

Loading an Image in VB.NET Window Form

Post icon  Posted 04 June 2008 - 10:09 PM

hi all,

i want to load an image from ms access database in vb.net picture box control(window form.)

how to do this?

thanks
arvind
Is This A Good Question/Topic? 0
  • +

Replies To: Loading an Image in VB.NET Window Form

#2 allensmith  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 11
  • Joined: 04-June 08

Re: Loading an Image in VB.NET Window Form

Posted 04 June 2008 - 11:36 PM

Hello Arvind,

You can use the given code to show images from a MSAccess Database table into Windows Picture Box. To
implement the given code, drop a picture box and two buttons on a Windows form. Two buttons have been used to
move back and forward to display the images from the table

BEGIN CODE

Imports System.Data.OleDb
Imports System.IO

Public Class Form1

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


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

		conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\DataBases\Nwind.mdb;User Id=admin;Password=;"

		DA = New OleDbDataAdapter("Select Photo from Employees", conn)

		DA.Fill(DS)

		counter = 0

		Me.PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage

	End Sub

	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

		PictureBox1.Image = GetImageFromByteArray(DS.Tables(0).Rows.Item(counter).Item(0))

		If counter < Me.DS.Tables(0).Rows.Count - 1 Then

			counter += 1

		End If


	End Sub

	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 = IIf((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 Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

		PictureBox1.Image = GetImageFromByteArray(DS.Tables(0).Rows.Item(counter).Item(0))

		If counter > 0 Then

			counter -= 1

		End If

	End Sub

End Class



END CODE


MSAccess will return the images in the form of byte arrays. We need to convert these bytes into Image format.
GetImageFromByteArray() does the same in the above code. It takes the arrray of bytes and convert them into
images which can then easily set into a Picture box.

I hope this will help.

Regards,

Allen
Was This Post Helpful? 1
  • +
  • -

#3 arvindmewara  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 26-May 08

Re: Loading an Image in VB.NET Window Form

Posted 05 June 2008 - 02:15 AM

thanks dear,
you solved my problem.

i want to know one more thing.

Public Function runquiz()
		Try
			If t1 <= nq - 1 Then
				Timer1.Enabled = True
				If con.State = ConnectionState.Open Then con.Close()
				con.Open()
				Dim ad1 As New OleDbDataAdapter("SELECT * FROM onlineexam ORDER BY RND(q_id)", con)
				Dim ds1 As New DataSet
				ad1.Fill(ds1, "onlineexam")
				Dim dt1 As DataTable
				dt1 = ds1.Tables(0)
				RichTextBox1.Text = dt1.Rows(t1).Item(1)
				optiona.Text = dt1.Rows(t1).Item(2)
				optionb.Text = dt1.Rows(t1).Item(3)
				optionc.Text = dt1.Rows(t1).Item(4)
				optiond.Text = dt1.Rows(t1).Item(5)

[[color=#FF0000]b]picturebox.image=?[/b][/color]

				t1 = t1 + 1
				con.Close()
			Else
				Timer1.Enabled = False
				RichTextBox1.Text = "Question"
				optiona.Text = "Option A"
				optionb.Text = "Option B"
				optionc.Text = "Option C"
				optiond.Text = "Option D"
				Button1.Enabled = False
				MsgBox("Thanks for Participation : " & namebox.Text & vbCrLf & "Your score is : " & scorebox.Text, MsgBoxStyle.Information, "Completed")
			End If
		Catch ex As Exception
			MsgBox(ex.Message)
		End Try
	End Function


this is my code for quiz application.
what i want when i click on start quiz that time image will come from database based on current question.
because i have some image which is related to question and i want to show that image.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1