[VB.Net] Lets users change Background Image ?

Discussion in 'Code Snippets and Tutorials' started by singapore, Dec 27, 2010.

  1. singapore

    singapore Level III

    Joined:
    Jan 4, 2010
    Messages:
    554
    Likes Received:
    10
    As above.

    How do i do that?

    Many thanks in advance.
     
  2. tharoux

    tharoux Level IV

    Joined:
    Dec 30, 2006
    Messages:
    2,733
    Likes Received:
    126
    Location:
    In front of my PC, Montreal
  3. Junior

    Junior Administrator
    Staff Member

    Joined:
    Nov 8, 2009
    Messages:
    3,350
    Likes Received:
    169
    Location:
    I come from a land down under! (Maaaate!)
  4. singapore

    singapore Level III

    Joined:
    Jan 4, 2010
    Messages:
    554
    Likes Received:
    10
    oh yeah. lol forgot to include. its for the form iteself.

    say maybe a button that lets user browse for a .jpeg file and then set it as that form's background.

    Tharoux: tried googlling it but couldn't find it.
     
  5. Kaito

    Kaito Level IV

    Joined:
    Jun 29, 2007
    Messages:
    2,692
    Likes Received:
    15
  6. tharoux

    tharoux Level IV

    Joined:
    Dec 30, 2006
    Messages:
    2,733
    Likes Received:
    126
    Location:
    In front of my PC, Montreal
    well the second ;)
    Yesterday, it was the first.

    Code (Text):
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         Dim ofd As New OpenFileDialog
    3.         ofd.ShowDialog()
    4.         If DialogResult = Windows.Forms.DialogResult.OK AndAlso ofd.FileName <> "" Then
    5.         Try
    6.             Me.BackgroundImage = Image.FromFile(ofd.FileName)
    7.             Me.BackgroundImageLayout = ImageLayout.Stretch
    8.         Catch ex As Exception
    9.             MessageBox.Show(Err.Description)
    10.         End Try
    11.  
    12.         End If
    13.     End Sub
    14.  
     
  7. Lightning

    Lightning Administrator
    Staff Member

    Joined:
    Nov 8, 2008
    Messages:
    3,021
    Likes Received:
    195
    Gender:
    Male
    Location:
    Florida, USA
    You said that you wanted image files?

    Code (Text):
    1.  
    2. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    3.         Dim ofd As New OpenFileDialog
    4.         With ofd
    5.             .CheckFileExists = True
    6.             .Filter = "Image Files (*.bmp, *.jpg, *.png, *.gif)|*.bmp;*.jpg;*.png;*.gif"
    7.             .Title = "Select an Image"
    8.             .ShowDialog()
    9.         End With
    10.  
    11.         If DialogResult = Windows.Forms.DialogResult.OK AndAlso ofd.FileName <> "" Then
    12.             Try
    13.                 If File.Exists(ofd.FileName) Then
    14.                     Me.BackgroundImage = Image.FromFile(ofd.FileName)
    15.                     Me.BackgroundImageLayout = ImageLayout.Stretch
    16.                 Else
    17.                     MsgBox("The selected file does not exist.")
    18.                     Exit Sub
    19.                 End If
    20.             Catch ex As Exception
    21.                 MsgBox(ex.Message)
    22.                 Debug.Print(ex.Message)
    23.             End Try
    24.  
    25.         End If
    26.     End Sub
    27.  
     
    singapore likes this.
  8. singapore

    singapore Level III

    Joined:
    Jan 4, 2010
    Messages:
    554
    Likes Received:
    10
    lol i dunno why but when i google that exact same thing i can't find it.

    my 2nd is VB Helper: HowTo: The let user drag an image in VB .NET

    tried the given one but nothing comes out. no errors and the form's background doesn't change.

    i tried changing extension of my image file from jpeg to png and bitmap but nothing works.
     
  9. Lightning

    Lightning Administrator
    Staff Member

    Joined:
    Nov 8, 2008
    Messages:
    3,021
    Likes Received:
    195
    Gender:
    Male
    Location:
    Florida, USA
    Singapore, what do you mean? I don't exactly understand (PM?).

    But if you are looking for allowing the user to Drag-and-Drop the picture -- you should try enabling AllowDrop for your form. And then, have a sub that handles it.
     
  10. singapore

    singapore Level III

    Joined:
    Jan 4, 2010
    Messages:
    554
    Likes Received:
    10
    thanks for the help ppl. i managed to solve it by doing this

    Code (Text):
    1. Dim ofd As New OpenFileDialog
    2.         With ofd
    3.             .CheckFileExists = True
    4.             .Filter = "Image Files (*.bmp, *.jpg, *.png, *.gif)|*.bmp;*.jpg;*.png;*.gif"
    5.             .Title = "Select an Image"
    6.             .ShowDialog()
    7.         End With
    8.  
    9.         Me.BackgroundImage = New Bitmap(ofd.FileName)
    10.         Me.BackgroundImageLayout = ImageLayout.Stretch
    11.  
    12.         Refresh()