Display jpeg in vb.net form by using path stored in Access.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • madasamonkey
    New Member
    • Mar 2008
    • 11

    Display jpeg in vb.net form by using path stored in Access.

    If someone could take pity and help me out with this before I drive myself completely nuts, then I would really appreciate it.

    Here's what I'm trying to do:

    I have an Access 2003 database containing a table which (amongst other things) holds the filenames of numerous jpeg images stored in an external folder. O/S is Windows XP Pro. btw.

    I also have a vb.net 2008 form connected to the database. That part works fine, all the fields show up on the form, and I am able to step through all of the records with no problem.

    But here's where I'm stuck. What I want to do is have a picture field on my form which will display different thumbnail jpegs which are unique to each dbase record. The information for the path is provided in part by a string containing the path to the image folder. To this I want to append each individual image name which I'll get from the relevant field on my form as each database entry appears.

    But all I'm getting is a box with a red cross in it. I can get the picture box on my form to display a single image that stays the same with each record, but obviously I want the image to change depoending on the record being viewed.

    Any ideas?
    TIA.
  • dbpros
    New Member
    • Mar 2008
    • 15

    #2
    Can you access the Image control in code?
    If so, try looking at some of the methods and properties of it, and find which one is the path to the image. Try setting the path on an image control (using the Properties options in design mode). Then access the control at run time (using a button) and try manipulating this file path to one of the ones in your database.

    You should just need to modify this value to change the image display.

    Hope this helps.

    Comment

    • madasamonkey
      New Member
      • Mar 2008
      • 11

      #3
      Thanks for your input.
      Things are moving along, (slowly).
      Here's what I have now:

      Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As System.EventArg s) Handles Button1.Click

      PictureBox1.Ima geLocation = "E:\Visual_Basi c_Projects\movi es_01\movies_01 \bin\Debug\thum b_pics\" & "tn_" & FilenameTextBox .Text & ".jpg"


      End Sub

      The goood news is that this code produces the desired thumbnail image, matching the database record in question. The problem though, is that it relies on the button click - I have to click the button to get the image to update to the currently displayed record. Naturally, I want the thumbnail image to appear automatically as each individual record is displayed, but this is still vexing me.

      Comment

      • madasamonkey
        New Member
        • Mar 2008
        • 11

        #4
        I now have things ironed out, in respect of my original query, but would appreciate a bit of input regarding vb.net forms. Is it possible to have a ListBox, or maybe a ListView box, or...... anything... which could contain images? I'm not talking about simply icons here either, but slightly larger images. I'm hoping to populate such a box at run time by referencing paths to images using an Access database, to produce a scrolling box of images and (hopefully) text alongside.

        Is this even remotely possible???

        Comment

        • dbpros
          New Member
          • Mar 2008
          • 15

          #5
          Originally posted by madasamonkey
          I now have things ironed out, in respect of my original query, but would appreciate a bit of input regarding vb.net forms. Is it possible to have a ListBox, or maybe a ListView box, or...... anything... which could contain images? I'm not talking about simply icons here either, but slightly larger images. I'm hoping to populate such a box at run time by referencing paths to images using an Access database, to produce a scrolling box of images and (hopefully) text alongside.

          Is this even remotely possible???
          I've never seen one of the standard Microsoft list or combo controls show thumbnails, though I suppose its possible. You might want to search for a custom 3rd party control to do that. Let me know what you find.

          Comment

          • Plater
            Recognized Expert Expert
            • Apr 2007
            • 7872

            #6
            There is an ImageList control that I think you can use in combination with them?
            Or you could add a FlowLayoutPanel to your Form and populate THAT panel with pictureboxes for each of them and create your own scrolling picture control.

            Comment

            • madasamonkey
              New Member
              • Mar 2008
              • 11

              #7
              I'm looking into the possibility of using a datagrid to achieve my goal of image and adjoining text in a scrolling box, but once again, getting the image to appear is proving tricky. Is it possible to have an image in a datagrid, as an embedded picture box control perhaps?

              Comment

              • Plater
                Recognized Expert Expert
                • Apr 2007
                • 7872

                #8
                Have you tried a DataGridViewIma geCell ?

                Comment

                • madasamonkey
                  New Member
                  • Mar 2008
                  • 11

                  #9
                  Er.... no. I'd never even heard of that until just now. It might be what I'm looking for, but it's a little difficult for me to decrypt the 'help' pages at the MSDN site. Any further help with that suggestion would certainly be appreciated. One thing that occured to me was to change one of the column types of the datagrid, and to that end, I attempted to edit the attributes using the Edit Columns dialog box. All seemed to be going well at first, changing the column type from DataGridViewTex tBoxColumn to DataGridViewIma geColumn. Unfortunately, a multitude of errors cropped up when I then tried to run it, presumably something to do wit the fact that the imagecolumn has only a file name in it, instead of an image, or a reference to an image.

                  Comment

                  • Plater
                    Recognized Expert Expert
                    • Apr 2007
                    • 7872

                    #10
                    The .Value property of the cells should take an Image(Bitmap) object yes?

                    Comment

                    • balabaster
                      Recognized Expert Contributor
                      • Mar 2007
                      • 798

                      #11
                      I got a ListBox to display images...not sure if that's helpful:

                      In the properties of the ListBox1 object, set the DrawMode property to OwnerDrawVariab le. Handle the DrawItem and MeasureItem events yourself:
                      Code:
                      Private Sub ListBox1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ListBox1.DrawItem
                        Dim oLB As ListBox = CType(sender, ListBox)
                        Dim oImg As Image = Image.FromFile(CStr(oLB.Items(e.Index)))
                        e.DrawBackground()
                        e.Graphics.DrawRectangle(Pens.Silver, e.Bounds)
                        e.Graphics.DrawImage(oImg, e.Bounds.X, e.Bounds.Y, oImg.Width,  oImg.Height)
                        If (e.State And DrawItemState.Selected) <> 0 Then e.DrawFocusRectangle()
                      End Sub
                       
                      Private Sub ListBox1_MeasureItem(ByVal sender As Object, ByVal e As System.Windows.Forms.MeasureItemEventArgs) Handles ListBox1.MeasureItem
                        Dim oLB As ListBox = CType(sender, ListBox)
                        Dim oImg As Image = Image.FromFile(CStr(oLB.Items(e.Index)))
                        Dim oSize As New Size(oLB.Width, oImg.Height)
                        e.ItemHeight = oSize.Height
                      End Sub
                      Does that help? It does however mean that you have to paint your own text too, but that's not too complex. I would recommend creating a custom ListItem class that contains a property for your picture and one for the text you want displayed. Then when you paint the object you'd do:
                      Code:
                      Dim oImg As Image = Image.FromFile(CType(oLB.Items(e.Index), MyClass).ImagePath)
                      Dim sText As String = CType(oLB.Items(e.Index), MyClass).DisplayText)
                      TextRenderer.DrawText(e.Graphics, sText, oLB.Fond, e.Bounds, e.ForeColor, e.BackColor)

                      Comment

                      • madasamonkey
                        New Member
                        • Mar 2008
                        • 11

                        #12
                        The .Value property of the cells should take an Image(Bitmap) object yes?
                        But then doesn't that conflict with the text property of the data being brought in from the database? I need some way to convert a textual picturexxx.jpg value to the actual path of the jpg and display it as an image in the datagrid cell.

                        Comment

                        • Plater
                          Recognized Expert Expert
                          • Apr 2007
                          • 7872

                          #13
                          Well you will get a column with textbox (that contains the filename), you can HIDE that column, and add your own image column and set the value to be some path + the value from that hidden column that has the filename.

                          Comment

                          • madasamonkey
                            New Member
                            • Mar 2008
                            • 11

                            #14
                            Thanks. That's definitely moved me forward. I hadn't realised that I could just add an unbound column of any type to the datagridview. After adding an Imagetype column as you suggested, I thought I was close to solving this, but the answer still seems tantalizingly just out of reach.

                            The trouble now is that I'm having difficulty referring to the cells in the new column to assign jpg files based on a path and the filename in the corresponsing cell of my other column. Obviously I want the datagrid to be populated totally at run time with all of the images; a different one for each row, but all the leads I'm following up on the Net for this seem to revolve around just modifying one.cell.at.a.t ime, and I can't work out how to programatically get the grid to appear with all of these connections already established.

                            Any hints?

                            Comment

                            • Plater
                              Recognized Expert Expert
                              • Apr 2007
                              • 7872

                              #15
                              Pretty sure you need to post process it.
                              Loop through the rows and look at the hidden column for the filename and then use it to set the Image for the Image column.

                              You could probably stick it all in a function and call it on the DataBindingComp lete event.

                              I was hoping DataGridView's had an event for "RowDataBou nd" like it's web counterpart, but I didn't see it.

                              Comment

                              Working...