IT KNOWLEDGE

IT KNOWLEDGE
BC

Friday, May 30, 2014

Brows Image to Show in PictureBox (VB.Net)

Last tutorial, I have shown you how to invert image in PictureBox. As you see the image in PictureBox gets from using wizard by importing from any directory of computer before running code. So even thought you do not use the code, you still get the image show on your PictureBox after debugging app. Otherwise with this tutorial we will show you how to get image shows in PictureBox using code. The App has been designed as below:

No Control Type Name Text Event
1 Form1 FormBrowsImage Brows Image
2 PictureBox1 PictureBoxBrows
3 Button1 ButtonBrows Brows Click

  • To see the picture stretch fit to the picture box like this, you have to set Size Mode of picture box to StretchImage by click arrow sign on the top-right oof PictureBox and then choose StretchImage in ComboBox.

Code
Imports System.IO
Imports System.Object
Public Class FormBrowsImage
    Dim ImgPath As String
    Dim filename As String = ""
    Dim StorePath As String = Application.StartupPath & "\images\"
    Private Sub ButtonBrows_Click(sender As Object, e As EventArgs) Handles ButtonBrows.Click
        Dim oPen As New OpenFileDialog()
        oPen.Filter = "img(img(*.JPG)|*.jpg|img(*.PNG)|*.png|img(*.gif)|*.gif|All files (*.*)|*.*)"
        oPen.FilterIndex = 1
        oPen.RestoreDirectory = True
        If oPen.ShowDialog() = Windows.Forms.DialogResult.OK Then
            ImgPath = oPen.FileName.ToString()
            filename = System.IO.Path.GetFileName(ImgPath)
            If (File.Exists(StorePath & filename)) Then
                If MsgBox("The file Exists.Do you want to replace?", _
                    MsgBoxStyle.Exclamation + MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
                    File.Copy(ImgPath, StorePath + filename, True)
                    PictureBoxBrows.Image = Image.FromFile(StorePath & filename)
                End If
            Else
                File.Copy(ImgPath, StorePath & filename)
                PictureBoxBrows.Image = Image.FromFile(StorePath & filename)
            End If
        End If
    End Sub
End Class


  • Note : you have to import two namespace to run this App:
    • Imports System.IO
    • Imports System.Object
















Please Give Us Your 1 Minute In Sharing This Post!
SOCIALIZE IT →
FOLLOW US →
SHARE IT →
Powered By: itech-9999

0 comments: