Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 149,934 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,527 people online right now. Registration is fast and FREE... Join Now!




Loading an Image in VB.NET Window Form

 
Reply to this topicStart new topic

Loading an Image in VB.NET Window Form

arvindmewara
4 Jun, 2008 - 09:09 PM
Post #1

New D.I.C Head
*

Joined: 26 May, 2008
Posts: 5

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
User is offlineProfile CardPM
+Quote Post

allensmith
RE: Loading An Image In VB.NET Window Form
4 Jun, 2008 - 10:36 PM
Post #2

New D.I.C Head
*

Joined: 4 Jun, 2008
Posts: 11



Thanked: 3 times
My Contributions
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

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

User is offlineProfile CardPM
+Quote Post

arvindmewara
RE: Loading An Image In VB.NET Window Form
5 Jun, 2008 - 01:15 AM
Post #3

New D.I.C Head
*

Joined: 26 May, 2008
Posts: 5

thanks dear,
you solved my problem.

i want to know one more thing.

CODE

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.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 03:56PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month