Displaying image from folder/file in vb.net - vb.net-2010

Dim ImagePath As String = "images/spaceship2.png"
Dim img1 As Bitmap
Dim newImage As Image = Image.FromFile("images/spaceship2.png")
img1 = New Bitmap(ImagePath)
pb2.ImageLocation = ImagePath
pb1.Image = newImage
I want to display image from folder, for example, student with an id number of 22137471, the picture with the name of 22137471 will be display on my picture box, between, i saw this code somewhere in google.

i want to display image from folder, for example, student with an id
number of 22137471, the picture with the name of 22137471 will be
display on my picture box
Try something like...
Dim id As String = "22137471"
Dim folder As String = "c:\some path\folder"
Dim filename As String = System.IO.Path.Combine(folder, id & ".png")
PictureBox1.Image = Image.FromFile(filename)
Here's an updated version that doesn't lock the original image file:
Dim id As String = "22137471"
Dim folder As String = "c:\some path\folder"
Dim filename As String = System.IO.Path.Combine(folder, id & ".png")
Try
Using fs As New System.IO.FileStream(filename, IO.FileMode.Open)
PictureBox1.Image = New Bitmap(Image.FromStream(fs))
End Using
Catch ex As Exception
Dim msg As String = "Filename: " & filename &
Environment.NewLine & Environment.NewLine &
"Exception: " & ex.ToString
MessageBox.Show(msg, "Error Opening Image File")
End Try

You can try this:
PictureBox1.Image = Image.FromFile("c:\some path\folder\myImage.jpg")

Related

How to get file's path created by this code?

I'm using this code to save files in my app
Dim filePath = String.Format("image{0:yyyyMMddHHmmss}.png", DateTime.Now)
PictureBox1.Image.Save(IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), (filePath)))
So now I have a textbox1 and I want to show the path of last saved image in it
how?
Regards,,,,
What I've done in the past is generate the path in one step and then use the generated variable to do the saving and to display.
So instead of:
Dim filePath = String.Format("image{0:yyyyMMddHHmmss}.png", DateTime.Now)
PictureBox1.Image.Save(IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), (filePath)))
Try:
'Generate the Path
Dim path As String = IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), String.Format("image{0:yyyyMMddHHmmss}.png", DateTime.Now))
'Save using the generated path
PictureBox1.Image.Save(path)
'Display the path
textbox1.Text = path
Thanks all I've done it successfully `
Dim filename As String = String.Format("image{0:yyyyMMddHHmmss}.png", DateTime.Now)
Dim filePath1 = (IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), (filename)))
Dim filePath2 = (IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), ("RMSS")))
If IO.Directory.Exists(IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), (" RMSS"))) = True Then
TextBox1.Text = filePath1
TextBox2.Text = filePath2 & "\" & filename
PictureBox1.Image.Save(filePath1)
My.Computer.FileSystem.MoveFile(TextBox1.Text, TextBox2.Text, True)
Else
TextBox1.Text = filePath1
TextBox2.Text = filePath2 & "\" & filename
PictureBox1.Image.Save(filePath1)
My.Computer.FileSystem.MoveFile(TextBox1.Text, TextBox2.Text, True)
End If

creating more than one image file when button clicked

Private Sub btnPrint_Click(sender As Object, e As EventArgs) Handles btnPrint.Click
CaptureScreen()
'used to created file path
Dim orderNum As String = Val(txtNum.Text) 'gets value of Invoice/PO number
Dim orderType As String = GroupBox3.Text 'gets value of either its a Invoice or PO`
'saves printscreen to PictureBox
PictureBox1.Image = bmpBackground
'saves printscreen to file path
PictureBox1.Image.Save("\\PCLIQFS\Shared_Data\PCLiq Scale-Shots\" + orderType + " " + orderNum + ".jpg", Imaging.ImageFormat.Jpeg)
'creates variable of filePath
Dim filePath As String = "\\PCLIQFS\Shared_Data\PCLiq Scale-Shots\" + orderType + " " + orderNum + ".jpg"
'checks to see if file is already in filePath
If File.Exists(filePath) Then
Dim folderPath = Path.GetDirectoryName(filePath)
Dim fileName = Path.GetFileNameWithoutExtension(filePath)
Dim extension = Path.GetExtension(filePath)
Dim fileNumber = 0
Do
'increments file name
fileNumber += 1
filePath = Path.Combine(folderPath,
String.Format("{0} ({1}){2}",
fileName,
fileNumber,
extension))
Loop While File.Exists(filePath)
End If
'saves new image
PictureBox1.Image.Save(filePath)
End Sub
I want to save an image when the user clicks the print button, but it creates two images at once since the images is already there. I only want to create another image if the print button is clicked again. How to I make it so that the code the increments the file name only runs if the print button is clicked again?
Comment the following line;
PictureBox1.Image.Save("\\PCLIQFS\Shared_Data\PCLiq Scale-Shots\" + orderType + " " + orderNum + ".jpg", Imaging.ImageFormat.Jpeg)
because you are saving the image at the end of the code.

Display Camera Image in VS PictureBox W/0 Saving To PC Drive

If I save (to disk) the image transferred from the camera, I can display it in a PictureBox, as follows:
Dim picNum As Integer = 1
Dim imgName As String
Dim imgExt As String
Dim WiaDialog1 As New WIA.CommonDialog
Dim WiaDevice1 As WIA.Device = WiaDialog1.ShowSelectDevice(WIA.WiaDeviceType.CameraDeviceType, False, False)
imgName = WiaDevice1.Items(picNum).Properties("Item Name").Value
imgExt = "." & WiaDevice1.Items(picNum).Properties("Filename extension").Value
Dim image1 As WIA.ImageFile = WiaDevice1.Items(picNum).Transfer(WIA.FormatID.wiaFormatPNG)
image1.SaveFile("C:\PhotoSort\CameraNew2\" & imgName & imgExt)
image1 = "C:\PhotoSort\CameraNew2\" & imgName & imgExt"
PictureBox1.Image = image1
I want to display the transferred image, in a similar VB code manner,. without saving it to disk
You can display image from physical location like
Picturebox1.Image=Image.FromFile("Path");

saving images after editing,that time saving only last edited images in winform application

I am working on windows form application. I have a GridView like this:
in cell content click i wrote code like this:
If e.ColumnIndex = 4 Then
Dim OFDLogo As New OpenFileDialog()
OFDLogo.Filter = "JPEG(*.jpg)|*.jpg|BMP(*.bmp)|*.bmp"
If OFDLogo.ShowDialog() = DialogResult.OK Then
gv.Rows(e.RowIndex).Cells(6).Value = Image.FromFile(OFDLogo.FileName)
End If
End If
and save button i wrote code like this:
Dim cmpny As String = "Delete from CompanyMaster_tbl"
Exetransaction(cmpny)
For i As Integer = 0 To gv.RowCount - 2
sqlInsertT2 = "Insert Into DepartmentMaster_tbl(dtname,dtphone,dtEmail,Cid) Values ('" + myTI.ToTitleCase(gv.Rows(i).Cells(1).Value) + "','" + gv.Rows(i).Cells(2).Value + "','" + gv.Rows(i).Cells(3).Value + "'," & Ccid & ");"
Exetransaction(sqlInsertT2)
Dim departmnt As String = gv.Rows(i).Cells(1).Value
Dim departid As Integer = RecordID("dtId", "DepartmentMaster_tbl", "dtName", departmnt)
Dim sql As String
'----------------------------------
Dim image As Image = TryCast(gv.Rows(i).Cells(4).Value, Image)
If image IsNot Nothing Then
Dim ms As New MemoryStream()
image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif)
Dim imagedata As Byte() = ms.ToArray()
sql = "update DepartmentMaster_tbl set empimage=#photo where dtId='" & departid & "'"
Dim cmd As New SqlCommand(sql, con.connect)
cmd.Parameters.Add("#photo", SqlDbType.Image)
cmd.Parameters("#photo").Value = imagedata
cmd.ExecuteNonQuery()
con.disconnect()
End If
Next
In first time I try to save this,saving both images, after loading the same page again I edited one image and I try to save, but that time only saving edited image What is wrong with my code?
In cell content click I am taking row_index

Load Image files from folder

I have a checked list box and a thumbnail area to display them where I am trying to load only images from a specific folder and need to display in thumbnails area but the problem is there is a thumbs.db file which is also being added to the checked list box which I don't need it.
So how do I actually load only the image files without the thumbs.db file.
Here is my code:
Private Sub LoadProjectToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles LoadProjectToolStripMenuItem.Click
Using ofdlg As New Windows.Forms.OpenFileDialog
ofdlg.DefaultExt = "trk"
ofdlg.Filter = "Project|*.trk"
ofdlg.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
If ofdlg.ShowDialog = Windows.Forms.DialogResult.OK Then
Dim SaveData As New gCanvasData
Using objStreamReader As New StreamReader(ofdlg.FileName)
Dim x As New XmlSerializer(GetType(gCanvasData))
SaveData = CType(x.Deserialize(objStreamReader), gCanvasData)
objStreamReader.Close()
End Using
With SaveData
'gTSSizer_gAZoom.Value = 100
GCanvas1.ImageXYReset()
GCanvas1.Image = .Image
GCanvas1.gAnnotates = .gAnnotates
GCanvas1.RebuildAll()
GCanvas1.AssembleBitmap()
End With
Dim fullpath As String
fullpath = Application.StartupPath + "\" & System.IO.Path.GetFileNameWithoutExtension(ofdlg.FileName) + "\"
For Each fi As FileInfo In New DirectoryInfo(fullpath).GetFiles
CheckedListBox1.Items.Add(Application.StartupPath + "\" & System.IO.Path.GetFullPath(ofdlg.FileName))
For i As Integer = 0 To CheckedListBox1.Items.Count - 1
CheckedListBox1.SetItemChecked(i, True)
ThumbControl1.AddFolder(fullpath, True)
Next i
Next
End If
End Using
End Sub
Either filter it inside of the For Each Loop:
For Each fi As FileInfo In New DirectoryInfo(fullpath).GetFiles
If Not {".jpg", ".png", ".bmp"}.Contains(fi.Extension) Then Continue For
' ...
Next
or do it in the GetFiles:
DirectoryInfo(fullpath).GetFiles(".jpg")
Found the solution at last:
Dim fullpath As String
fullpath = Application.StartupPath & "\" & System.IO.Path.GetFileNameWithoutExtension(ofdlg.FileName) + "\"
Dim FileDirectory As New IO.DirectoryInfo(fullpath)
Dim FileJpg As IO.FileInfo() = FileDirectory.GetFiles("*.jpg")
Dim FileGif As IO.FileInfo() = FileDirectory.GetFiles("*.gif")
Dim FileBmp As IO.FileInfo() = FileDirectory.GetFiles("*.bmp")
For Each File As IO.FileInfo In FileJpg
CheckedListBox1.Items.Add(File.FullName)
Dim str As String
str = Directory.GetCurrentDirectory() & "\" & "Backup\"
Next
For Each File As IO.FileInfo In FileGif
CheckedListBox1.Items.Add(File.FullName)
Dim str As String
str = Directory.GetCurrentDirectory() & "\" & "Backup\"
Next
For Each File As IO.FileInfo In FileBmp
CheckedListBox1.Items.Add(File.FullName)
Dim str As String
str = Directory.GetCurrentDirectory() & "\" & "Backup\"
Next
For i As Integer = 0 To CheckedListBox1.Items.Count - 1
CheckedListBox1.SetItemChecked(i, True)
Next i
Change DirectoryInfo(fullpath).GetFiles to DirectoryInfo(fullpath).EnumerateFiles() And add a search pattern for the image file extensions you want. http://msdn.microsoft.com/en-us/library/dd383574.aspx