开发者

How to change an image in a VB.NET Windows Forms app. System.Drawing.Bitmap in a PictureBox

开发者 https://www.devze.com 2023-01-08 11:47 出处:网络
I have a VB.NET Windows Forms app with a logo image on the form as a System.Drawing.Bitmap inside a PictureBox.

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 obje开发者_Python百科ct/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?


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号