How Can I Display an Image Using VB.NET?

  • Thread starter mcknia07
  • Start date
In summary, the conversation discusses the use of the OpenFileDialog class to select and display an image file. The code shows how to set the filter for the desired file type and how to handle potential errors. It also includes the use of the Bitmap class to display the selected image.
  • #1
mcknia07
284
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
  • #2
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
 

Related to How Can I Display an Image Using VB.NET?

What is the best way to get a picture to appear on a webpage?

The best way to get a picture to appear on a webpage is to use the img tag and specify the image source using the src attribute. You can also set the image height and width using the height and width attributes.

Why is my picture not appearing on the webpage?

There could be several reasons why your picture is not appearing on the webpage. Some common reasons include incorrect image source, incorrect file extension (such as using .jpg instead of .jpeg), or the image file being corrupted. Make sure to double check your code and the image file itself.

Can I resize the picture to make it fit better on the webpage?

Yes, you can resize the picture by setting the height and width attributes in the img tag. You can also use CSS to further customize the size and placement of the image on the webpage.

How can I add a caption to my picture?

To add a caption to your picture, you can use the figure and figcaption tags. Wrap your image in the figure tag and add your caption in the figcaption tag. You can then use CSS to style the caption as desired.

Is it possible to have multiple pictures appear on the same webpage?

Yes, you can have multiple pictures appear on the same webpage by using multiple img tags and specifying different image sources for each one. You can also use CSS to arrange the pictures in a specific layout on the webpage.

Similar threads

  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
4
Views
5K
  • Programming and Computer Science
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
5K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
4K
Back
Top