tags:

views:

21

answers:

1

I have a VB.NET Windows Forms app with a logo image on the form as a System.Drawing.Bitmap inside a PictureBox. I used the Visual Studio Designer to add the logo .bmp image so I don't currently have any VB code doing anything with it.

I'd like to make the current logo a clickable object/button so when I click on it a file browser dialog opens and I can select a new image to replace the current image.

The current image is a local resource and is set in a PictureBox as a System.Drawing.Bitmap. How would I replace that System.Drawing.Bitmap with a file selected from the file browser dialog?

A: 

Hi David you can change the image of the picturebox by using the picturebox.click event I've added below

Private Sub PictureBox1_Click(ByVal sender As System.Object, _
  ByVal e As System.EventArgs) Handles PictureBox1.Click
    Dim OpenFileDialog1 As New OpenFileDialog

    If OpenFileDialog1.ShowDialog Then
        Try
            Dim NewPic As New System.Drawing.Bitmap(OpenFileDialog1.FileName)
            PictureBox1.Image = NewPic
            PictureBox1.SizeMode = PictureBoxSizeMode.Zoom
        Catch ex As Exception
            MsgBox("An error has occurred" & Chr(13) & Chr(13) & ex.Message)
        End Try
    End If
End Sub

Hope this helps you

Donnavan de Groot
You'd obviously also want to add filters for specific file types (jpg gif etc) that I forgot to add in the above code - that way you wouldn't need to use the try statement to catch errors if the user selects an invalid file format For info on using file filtershttp://msdn.microsoft.com/en-us/library/microsoft.win32.filedialog.filter.aspx
Donnavan de Groot
Thank you! Is there any way to make the new image perminent or the new "default image"? I mean perminent so that the application could be redistributed to someone else, perhaps in a different organization and the new image would be the new default.
David
Perhaps look at copying the image file to the startup directory as logo.jpg for example then on the load event check if this file exists if so load that as the initial image for the picture box ---> There may be a better way to do this but nothings coming to mind at the moment
Donnavan de Groot