How Can I Display an Image Using VB.NET?

  • Thread starter Thread starter mcknia07
  • Start date Start date
Click For Summary
The discussion centers around a Visual Basic code snippet for a Windows Forms application that allows users to display an image file. The initial code uses an OpenFileDialog to filter for JPG files and attempts to read the selected file's content into a text box, handling exceptions for file not found and other unexpected errors. The revised code improves functionality by directly loading the selected image into an image control using a Bitmap object when the dialog result is confirmed as OK. This change enhances user experience by visually displaying the image rather than reading its content as text. Key points include the importance of error handling and the transition from text representation to image display in the application.
mcknia07
Messages
284
Reaction score
8
Public Class Form1

Private Sub btnDisplayPic_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplayPic.Click

OpenFileDialog1.Filter = "Image Files (*.jpg)|*jpg"

OpenFileDialog1.ShowDialog()

Try
txtPicFile.Text = My.Computer.FileSystem.ReadAllText(OpenFileDialog1.FileName)
Catch myfilenotfound As System.IO.FileNotFoundException
MessageBox.Show("File not found - select a different file!")

Catch ex As Exception
MessageBox.Show("Unexpected error!")

End Try


End Sub
End Class


here is my code...
 
Technology news on Phys.org
Public Class Form1

Private Sub btnDisplayPic_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplayPic.Click

OpenFileDialog1.Filter = "Image Files (*.jpg)|*jpg"

if dlg.ShowDialog() = DialogResult.OK
imgPhoto.Image = new Bitmap(dlg.OpenFile());
end if

End Sub
End Class
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

Replies
2
Views
2K
Replies
4
Views
5K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 6 ·
Replies
6
Views
6K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 31 ·
2
Replies
31
Views
25K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 7 ·
Replies
7
Views
76K
  • · Replies 11 ·
Replies
11
Views
3K