locked
Image Gallery RRS feed

  • Question

  • Hi Is there any code snippet available for Image Gallery in VB.net  2.0

    kindly help.

    with regards

    Friday, May 28, 2010 10:11 PM

Answers

  • In order to get help with this problem, you have to break it into separate question because image gallery will be a full program. You can try to break it this down this way

    1) Ask how to load multiple image or search image on system

    2) how to create image thumbnail

    3) How to create picturebox and label control at runtime to hold thumbnail image and image name.

    4) How to add event handler to each thumbnail picturebox

    5) Image viewer (picturebox) that will display the full image when you click on thumbnail image

     

    kaymaf


    CODE CONVERTER SITE

    http://www.carlosag.net/Tools/CodeTranslator/ .

    http://www.developerfusion.com/tools/convert/csharp-to-vb/ .

    • Proposed as answer by Cor Ligthert Saturday, May 29, 2010 5:40 AM
    • Marked as answer by Liliane Teng Wednesday, June 2, 2010 6:53 AM
    Friday, May 28, 2010 10:30 PM
  • This code displays all JPG files in a folder as thumbnails, and shows each image in the ful windows if it is double-clicked. Click the image to return to thumbnail view.

    Public Class Form1

        
    Dim files() As String
        Dim Suspend As Boolean = False
        Dim Cols As Integer = -1
        
    Dim Rows As Integer = -1
        
    Dim ImageList As List(Of String) = New List(Of String)

        
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            files = System.IO.Directory.GetFiles(
    "C:\Users\Public\Pictures\Sample Pictures""*.jpg")
        
    End Sub

        Private Sub Form1_Paint(ByVal sender As ObjectByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
            
    If Not Suspend Then
                ImageList.Clear()
                Rows = -1
                Cols = -1
                
    Dim Row As Integer = 0
                
    Dim Col As Integer = 0
                e.Graphics.Clear(Color.Snow)
                
    For Each S As String In files
                    ImageList.Add(S)
                    
    Dim BMP As Bitmap = New Bitmap(S)
                    e.Graphics.DrawImage(BMP, 
    New Rectangle(Col, Row, 100, 100), _
                                         
    New Rectangle(0, 0, IIf(BMP.Width > BMP.Height, BMP.Width, BMP.Height), _
                                          IIf(BMP.Width > BMP.Height, BMP.Width, BMP.Height)), _
                                         GraphicsUnit.Pixel)
                    Col += 120
                    
    If Col > Me.ClientSize.Width - 110 Then
                        If Cols = -1 Then Cols = Col
                        Row += 120
                        Rows = Row
                        Col = 0
                    
    End If
                Next
            End If
            Rows \= 120
            Cols \= 120
        
    End Sub

        Private Sub Form1_ResizeBegin(ByVal sender As ObjectByVal e As System.EventArgs) Handles Me.ResizeBegin
            Suspend = 
    True
        End Sub

        Private Sub Form1_ResizeEnd(ByVal sender As ObjectByVal e As System.EventArgs) Handles Me.ResizeEnd
            Suspend = 
    False
            Me.Invalidate()
        
    End Sub

        Private Sub Form1_DoubleClick(ByVal sender As ObjectByVal e As System.EventArgs) Handles Me.DoubleClick
            
    Dim Xpos As Integer = (MousePosition.X - Me.Left) \ 120
            
    Dim Ypos As Integer = (MousePosition.Y - Me.Top) \ 120
            
    Dim Index As Integer = Xpos + Ypos * Cols
            
    If Index < ImageList.Count Then
                Dim PB1 As PictureBox = New PictureBox
                PB1.Location = 
    New Point(0, 0)
                PB1.SizeMode = PictureBoxSizeMode.Zoom
                PB1.Image = Image.FromFile(ImageList(Index))
                PB1.Width = 
    Me.ClientSize.Width
                PB1.Height = 
    Me.ClientSize.Height
                
    AddHandler PB1.Click, AddressOf ClickHandler
                
    Me.Controls.Add(PB1)
            
    End If
        End Sub

        Private Sub ClickHandler(ByVal sender As ObjectByVal e As System.EventArgs)
            
    Me.Controls.Remove(sender)
            sender = 
    Nothing
        End Sub
    End
     Class

    • Proposed as answer by Cor Ligthert Saturday, May 29, 2010 5:40 AM
    • Marked as answer by Liliane Teng Wednesday, June 2, 2010 6:53 AM
    Saturday, May 29, 2010 3:37 AM

All replies

  • In order to get help with this problem, you have to break it into separate question because image gallery will be a full program. You can try to break it this down this way

    1) Ask how to load multiple image or search image on system

    2) how to create image thumbnail

    3) How to create picturebox and label control at runtime to hold thumbnail image and image name.

    4) How to add event handler to each thumbnail picturebox

    5) Image viewer (picturebox) that will display the full image when you click on thumbnail image

     

    kaymaf


    CODE CONVERTER SITE

    http://www.carlosag.net/Tools/CodeTranslator/ .

    http://www.developerfusion.com/tools/convert/csharp-to-vb/ .

    • Proposed as answer by Cor Ligthert Saturday, May 29, 2010 5:40 AM
    • Marked as answer by Liliane Teng Wednesday, June 2, 2010 6:53 AM
    Friday, May 28, 2010 10:30 PM
  • This code displays all JPG files in a folder as thumbnails, and shows each image in the ful windows if it is double-clicked. Click the image to return to thumbnail view.

    Public Class Form1

        
    Dim files() As String
        Dim Suspend As Boolean = False
        Dim Cols As Integer = -1
        
    Dim Rows As Integer = -1
        
    Dim ImageList As List(Of String) = New List(Of String)

        
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            files = System.IO.Directory.GetFiles(
    "C:\Users\Public\Pictures\Sample Pictures""*.jpg")
        
    End Sub

        Private Sub Form1_Paint(ByVal sender As ObjectByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
            
    If Not Suspend Then
                ImageList.Clear()
                Rows = -1
                Cols = -1
                
    Dim Row As Integer = 0
                
    Dim Col As Integer = 0
                e.Graphics.Clear(Color.Snow)
                
    For Each S As String In files
                    ImageList.Add(S)
                    
    Dim BMP As Bitmap = New Bitmap(S)
                    e.Graphics.DrawImage(BMP, 
    New Rectangle(Col, Row, 100, 100), _
                                         
    New Rectangle(0, 0, IIf(BMP.Width > BMP.Height, BMP.Width, BMP.Height), _
                                          IIf(BMP.Width > BMP.Height, BMP.Width, BMP.Height)), _
                                         GraphicsUnit.Pixel)
                    Col += 120
                    
    If Col > Me.ClientSize.Width - 110 Then
                        If Cols = -1 Then Cols = Col
                        Row += 120
                        Rows = Row
                        Col = 0
                    
    End If
                Next
            End If
            Rows \= 120
            Cols \= 120
        
    End Sub

        Private Sub Form1_ResizeBegin(ByVal sender As ObjectByVal e As System.EventArgs) Handles Me.ResizeBegin
            Suspend = 
    True
        End Sub

        Private Sub Form1_ResizeEnd(ByVal sender As ObjectByVal e As System.EventArgs) Handles Me.ResizeEnd
            Suspend = 
    False
            Me.Invalidate()
        
    End Sub

        Private Sub Form1_DoubleClick(ByVal sender As ObjectByVal e As System.EventArgs) Handles Me.DoubleClick
            
    Dim Xpos As Integer = (MousePosition.X - Me.Left) \ 120
            
    Dim Ypos As Integer = (MousePosition.Y - Me.Top) \ 120
            
    Dim Index As Integer = Xpos + Ypos * Cols
            
    If Index < ImageList.Count Then
                Dim PB1 As PictureBox = New PictureBox
                PB1.Location = 
    New Point(0, 0)
                PB1.SizeMode = PictureBoxSizeMode.Zoom
                PB1.Image = Image.FromFile(ImageList(Index))
                PB1.Width = 
    Me.ClientSize.Width
                PB1.Height = 
    Me.ClientSize.Height
                
    AddHandler PB1.Click, AddressOf ClickHandler
                
    Me.Controls.Add(PB1)
            
    End If
        End Sub

        Private Sub ClickHandler(ByVal sender As ObjectByVal e As System.EventArgs)
            
    Me.Controls.Remove(sender)
            sender = 
    Nothing
        End Sub
    End
     Class

    • Proposed as answer by Cor Ligthert Saturday, May 29, 2010 5:40 AM
    • Marked as answer by Liliane Teng Wednesday, June 2, 2010 6:53 AM
    Saturday, May 29, 2010 3:37 AM
  • Hi Acamar, 

    Thanks for replying with valuable code snipped , is it possible to use the same for web based application

     

    with reards

    Wednesday, June 2, 2010 3:09 PM