VB: How to save image to folder in the following vb code? - vb.net

Public Class Form1
'Webcam
Public Touchless As New TouchlessLib.TouchlessMgr
Public Camera1 As TouchlessLib.Camera = Touchless.Cameras.ElementAt(1)
Public Camera2 As TouchlessLib.Camera = Touchless.Cameras.ElementAt(0)
Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
PictureBox3.Image = Touchless.Cameras.ElementAt(1).GetCurrentImage
PictureBox4.Image = Touchless.Cameras.ElementAt(0).GetCurrentImage
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Touchless.CurrentCamera = Camera1
Touchless.CurrentCamera.CaptureHeight = 250
Touchless.CurrentCamera.CaptureWidth = 300
Touchless.CurrentCamera = Camera2
Touchless.CurrentCamera.CaptureHeight = 250
Touchless.CurrentCamera.CaptureWidth = 300
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
PictureBox1.Image = Touchless.Cameras.ElementAt(1).GetCurrentImage
PictureBox2.Image = Touchless.Cameras.ElementAt(0).GetCurrentImage
End Sub
' Save the picture.
Private Sub Button2_Click(ByVal sender As _
System.Object, ByVal e As System.EventArgs) Handles _
Button2.Click
' Compose the picture's base file name.
Dim file_name As String = Application.ExecutablePath
file_name = file_name.Substring(0, _
file_name.LastIndexOf("\bin")) & _
"\test."
' Get a Bitmap.
Dim bm As Bitmap = PictureBox4.Image
' Save the picture as a bitmap, JPEG, and GIF.
bm.Save(file_name & "bmp", _
System.Drawing.Imaging.ImageFormat.Bmp)
bm.Save(file_name & "jpg", _
System.Drawing.Imaging.ImageFormat.Jpeg)
bm.Save(file_name & "gif", _
System.Drawing.Imaging.ImageFormat.Gif)
MsgBox("Ok")
End Sub
In the above code, i want to save image to a c:/ drive with a custom file name and with replace of default folder "\bin" and name "\test." in the above code...what is the correct code to save image with custom destination & file name option..?
Thank U

Take a look at the following part of the sample you posted.
bm.Save(file_name & "bmp", _System.Drawing.Imaging.ImageFormat.Bmp)
Your image will be saved with the name of the value that the string "file_name" holds. (plus the string "bmp")
So you should assign the path + file name you want to use to the string that's currently used to save your image.
So replace
Dim file_name As String = Application.ExecutablePath
file_name = file_name.Substring(0, _
file_name.LastIndexOf("\bin")) & _
"\test."
with
file_name = "C:\yourFileNameHere."
Then you can save your image using
bm.Save(file_name & ".bmp", _System.Drawing.Imaging.ImageFormat.Bmp)
If you want to give the name from within your form you could use a textbox and pass the text from the textbox to the string
file_name = TextBox1.Text;

Using Image.Save method: http://msdn.microsoft.com/en-us/library/ktx83wah%28v=vs.110%29.aspx
or using filestream if you have a bytestream of the object: http://msdn.microsoft.com/en-us/library/vstudio/system.io.filestream
Using FS As New IO.FileStream("C:\location\file.ext", IO.FileMode.OpenOrCreate, IO.FileAccess.Write)
FS.Write(item.imageBytes, 0, item.imageBytesLength)
End Using
to get a bytestream from an image, use:
Using FS As New IO.FileStream("C:\location\file.ext", IO.FileMode.Open, IO.FileAccess.Read)
Dim _theBytes(FS.Length) As Byte
FS.Read(_theBytes, 0, FS.Length)
_imageBytes = _theBytes
End Using

Related

Spliting the string and put value into textbox

I am using visual basic 2010.I have string of data in below format.I wanted to Split the comma seprated value and put into Individual text box.For The last
Temp_read:348,HV_Read:647,SPD:0,DIS:0". I would like to split values alone and put into text box.
can someone suggest me how can i do it. Is there any example code.
Public Class Form1
Dim selectedItem1 As String
Dim Data As String
Private Sub SMCB1_clientIP_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SMCB1_clientIP.TextChanged
End Sub
Private Sub SMCB1_Connect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SMCB1_Connect.Click
Dispay_Show.Text = SMCB1_clientIP.Text
Dispay_Show.Text = SMCB1_clientIP.Text & vbNewLine & SMCB1_Port.Text & vbNewLine
Data = "SMCB3,3,0,0,0,0,0,0,0,0,0,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,Temp_read:348,HV_Read:647,SPD:0,DIS:0"
Dispay_Show.Text = SMCB1_clientIP.Text & vbNewLine & SMCB1_Port.Text & vbNewLine & Data
Data.Split()
End Sub
Private Sub SMCB1_Disconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SMCB1_Disconnect.Click
End Sub
End Class
Image
Data = "SMCB3,3,0,0,0,0,0,0,0,0,0,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,Temp_read:348,HV_Read:647,SPD:0,DIS:0"
I have written code as below.There are 28 text boxes . value should get recorded into each text box. With below code i could record upto 28 value.
But From 24 the paramter contains
Temp_read:348,HV_Read:647,SPD:0,DIS:0"
Need to separate string ":" and put reading to particular text box.
Option Explicit On
Public Class Form1
Dim selectedItem1 As String
Dim Data As String
Dim WrdArray() As String
Dim line As String
Private Sub SMCB1_clientIP_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SMCB1_clientIP.TextChanged
End Sub
Private Sub SMCB1_Connect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SMCB1_Connect.Click
' Dispay_Show.Text = SMCB1_clientIP.Text
'Dispay_Show.Text = SMCB1_clientIP.Text & vbNewLine & SMCB1_Port.Text & vbNewLine
Data = "SMCB3,3,0,0,0,0,0,0,0,0,0,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,Temp_read:348,HV_Read:647,SPD:0,DIS:0"
' Dispay_Show.Text = SMCB1_clientIP.Text & vbNewLine & SMCB1_Port.Text & vbNewLine & Data
Dispay_Show.Text = Data
Dim strArray() As String
Dim intCount As Integer
Dim Tempr_read As String
Dim voltage As String
Dim SPD As String
Dim Dis_value As String
Data = "SMCB3,3,0,0,0,0,0,0,0,0,0,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,Temp_read:348,HV_Read:647,SPD:0,DIS:0"
strArray = Split(Data, ",")
SMCB1_Name.Text = strArray(0)
SMCB1_DeviceId.Text = strArray(1)
SMCB1_String1.Text = strArray(2)
SMCB1_String2.Text = strArray(3)
SMCB1_String3.Text = strArray(4)
SMCB1_String4.Text = strArray(5)
SMCB1_String5.Text = strArray(6)
SMCB1_String6.Text = strArray(7)
SMCB1_String7.Text = strArray(8)
SMCB1_String8.Text = strArray(9)
SMCB1_String9.Text = strArray(10)
SMCB1_String10.Text = strArray(11)
SMCB1_String11.Text = strArray(12)
SMCB1_String12.Text = strArray(13)
SMCB1_String13.Text = strArray(14)
SMCB1_String14.Text = strArray(15)
SMCB1_String15.Text = strArray(16)
SMCB1_String16.Text = strArray(17)
SMCB1_String17.Text = strArray(18)
SMCB1_String18.Text = strArray(19)
SMCB1_String19.Text = strArray(20)
SMCB1_String20.Text = strArray(21)
SMCB1_String21.Text = strArray(22)
SMCB1_String22.Text = strArray(23)
SMCB1_String23.Text = strArray(24)
SMCB1_String24.Text = strArray(25)
' Tempr_read = Split(Data(strArray(26),":")
SMCB1_Temp.Text = strArray(26)
SMCB1_Hvread.Text = strArray(27)
SMCB1_SPD.Text = strArray(28)
SMCB1_DIS.Text = strArray(29)
For intCount = LBound(strArray) To UBound(strArray)
Debug.Print(Trim(strArray(intCount)))
Next
End Sub
Private Sub SMCB1_Disconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SMCB1_Disconnect.Click
End Sub
End Class
I suggest using 2 instances of the InStrRev function - one for the colon and the other for the comma whilst looping backwards through the string and building your array.
In this way you will be able to capture the data to the right of the colon (and before the comma), yet disregard the label between the comma and the colon (if that is in fact what you intend to do).
Please hit me up if you would like a worked example.
Regards

VB.NET 2008 Not recognizing Path.GetFileName Method

I have read this post but nothing works.
Visual Basic not recognizing Path.GetFileName Method
I am following this page in trying to get the filename in my Visual Basic application but i can't compile it Microsoft Official Page
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
size_mode_list.Items.AddRange([Enum].GetNames(GetType(PictureBoxSizeMode)))
For Each drive As DriveInfo In DriveInfo.GetDrives
Dim node As TreeNode = _
file_view_tree.Nodes.Add(drive.Name)
node.Tag = ItemType.Drive
node.Nodes.Add("FILLER")
Next
End Sub
Private Sub file_view_tree_BeforeExpand(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewCancelEventArgs) Handles file_view_tree.BeforeExpand
Dim currentNode As TreeNode = e.Node
currentNode.Nodes.Clear()
Try
'Now go get all the files and folders
Dim fullPathString As String = currentNode.FullPath
'Handle each folder
For Each dolderString As String In _
Directory.GetDirectories(fullPathString)
Dim newNode As TreeNode = _
currentNode.Nodes.Add(path.getFileName(folderString))
Dim x As String = path.GetFileName("")
newNode.Tag = ItemType.Folder
newNode.Nodes.Add("FILLER")
Next
'Handle each file
For Each fileString As String In _
Directory.GetFiles(fullPathString)
'Get just the file name portion (without the path) :
Dim newNode As TreeNode = _
currentNode.Nodes.Add(path.getFileName(fileString))
newNode.Tag = ItemType.File
Next
Catch ex As Exception
End Try
End Sub
'Don't forget to import this
Imports System.IO
'Declare these
Private Enum ItemType
Drive
Folder
File
End Enum
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For Each drive As DriveInfo In DriveInfo.GetDrives
Dim node As TreeNode = _
file_view_tree.Nodes.Add(drive.Name)
node.Tag = ItemType.Drive
node.Nodes.Add("FILLER")
Next
End Sub
Private Sub file_view_tree_BeforeExpand(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewCancelEventArgs) Handles file_view_tree.BeforeExpand
Dim currentNode As TreeNode = e.Node
currentNode.Nodes.Clear()
Try
'Now go get all the files and folders
Dim fullPathString As String = currentNode.FullPath
'Handle each folder
For Each folderString As String In _
Directory.GetDirectories(fullPathString)
Dim newNode As TreeNode = _
currentNode.Nodes.Add(Path.GetFileName(folderString))
Dim x As String = Path.GetFileName("")
newNode.Tag = ItemType.Folder
newNode.Nodes.Add("FILLER")
Next
'Handle each file
For Each fileString As String In _
Directory.GetFiles(fullPathString)
'Get just the file name portion (without the path) :
Dim newNode As TreeNode = _
currentNode.Nodes.Add(Path.GetFileName(fileString))
newNode.Tag = ItemType.File
Next
Catch ex As Exception
End Try
End Sub

How to display a random photo from a folder in a picture box and print out the photo name without extension

I have a folder named "MyPhotos", I want to display a random photo at each time a command button is clicked from "Myphoto" and print out the name of the displayed photo in the form without extension . I'm trying the following code, but still can't get the photo name
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As _ System.EventArgs) Handles Button1.Click
Dim DirectoryPath As String = Application.ExecutablePath
Directorypath = DirectoryPath.Substring(0, Directorypath.LastIndexOf("\bin")) & "\MyPhotos"
Dim bm As New Bitmap(GetRandomImageFilePath(Directorypath))
picImage.Image = bm
End Sub
Public Function GetRandomImageFilePath(ByVal folderPath As String) As String
Dim files() As String = Directory.GetFiles(folderPath, "*.jpg")
Dim random As Random = New Random()
Return files(random.Next(0, files.Length))
End Function
Any help please ?
I added this to your button method. All I used was FileInfo to get the details.
Dim finfo As FileInfo = New FileInfo(GetRandomImageFilePath(DirectoryPath))
Dim filename As String = finfo.Name.Replace(finfo.Extension, "")
Dim bm As New Bitmap(finfo.FullName)
picImage.Image = bm
You already have the full path being returned by GetRandomImageFilePath.
If you want just the filename, do this in your calling Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As _ System.EventArgs) Handles Button1.Click
Dim DirectoryPath As String = Application.ExecutablePath
Directorypath = DirectoryPath.Substring(0, Directorypath.LastIndexOf("\bin")) & "\MyPhotos"
Dim imagePath as String = GetRandomImageFilePath(Directorypath)
Dim fileInfo as New FileInfo(imagePath)
Dim imageName as String = fileInfo.Name
Dim bm As New Bitmap(imagePath)
picImage.Image = bm
End Sub

Read Text File and Output Multiple Lines to a Textbox

I'm trying to read a text file with multiple lines and then display it in a textbox. The problem is that my program only reads one line. Can someone point out the mistake to me?
Imports System.IO
Imports Microsoft.VisualBasic.FileIO
Public Class Form1
Private BagelStreamReader As StreamReader
Private PhoneStreamWriter As StreamWriter
Dim ResponseDialogResult As DialogResult
Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click
'Dim PhoneStreamWriter As New StreamWriter(OpenFileDialog1.FileName)
'Is file already open
If PhoneStreamWriter IsNot Nothing Then
PhoneStreamWriter.Close()
End If
With OpenFileDialog1
.InitialDirectory = Directory.GetCurrentDirectory
.FileName = OpenFileDialog1.FileName
.Title = "Select File"
ResponseDialogResult = .ShowDialog()
End With
'If ResponseDialogResult <> DialogResult.Cancel Then
' PhoneStreamWriter = New StreamWriter(OpenFileDialog1.FileName)
'End If
Try
BagelStreamReader = New StreamReader(OpenFileDialog1.FileName)
DisplayRecord()
Catch ex As Exception
MessageBox.Show("File not found or is invalid.", "Data Error")
End Try
End Sub
Private Sub DisplayRecord()
Do Until BagelStreamReader.Peek = -1
TextBox1.Text = BagelStreamReader.ReadLine()
Loop
'MessageBox.Show("No more records to display.", "End of File")
'End If
End Sub
Private Sub SaveToolStripMenuItem_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles SaveToolStripMenuItem.Click
With SaveFileDialog1
.InitialDirectory = Directory.GetCurrentDirectory
.FileName = OpenFileDialog1.FileName
.Title = "Select File"
ResponseDialogResult = .ShowDialog()
End With
PhoneStreamWriter.WriteLine(TextBox1.Text)
With TextBox1
.Clear()
.Focus()
End With
TextBox1.Clear()
End Sub
Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
Dim PhoneStreamWriter As New StreamWriter(OpenFileDialog1.FileName)
PhoneStreamWriter.Close()
Me.Close()
End Sub
End Class
Here is a sample textfile:
Banana nut
Blueberry
Cinnamon
Egg
Plain
Poppy Seed
Pumpkin
Rye
Salt
Sesame seed
You're probably only getting the last line in the file, right? Your code sets TextBox1.Text equal to BagelSteramReader.ReadLine() every time, overwriting the previous value of TextBox1.Text. Try TextBox1.Text += BagelStreamReader.ReadLine() + '\n'
Edit: Though I must steal agree with Hans Passant's commented idea on this; If you want an more efficient algorithm, File.ReadAllLines() even saves you time and money...though I didn't know of it myself. Darn .NET, having so many features...
I wrote a program to both write to and read from a text file. To write the lines of a list box to a text file I used the following code:
Private Sub txtWriteToTextfile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtWriteToTextfile.Click
Dim FileWriter As StreamWriter
FileWriter = New StreamWriter(FileName, False)
' 3. Write some sample data to the file.
For i = 1 To lstNamesList.Items.Count
FileWriter.Write(lstNamesList.Items(i - 1).ToString)
FileWriter.Write(Chr(32))
Next i
FileWriter.Close()
End Sub
And to read and write the contents of the text file and write to a multi-line text box (you just need to set the multiple lines property of a text box to true) I used the following code. I also had to do some extra coding to break the individual words from the long string I received from the text file.
Private Sub cmdReadFromTextfile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdReadFromTextfile.Click
Dim sStringFromFile As String = ""
Dim sTextString As String = ""
Dim iWordStartingPossition As Integer = 0
Dim iWordEndingPossition As Integer = 0
Dim iClearedTestLength As Integer = 0
Dim FileReader As StreamReader
FileReader = New StreamReader(FileName)
sStringFromFile = FileReader.ReadToEnd()
sTextString = sStringFromFile
txtTextFromFile.Text = ""
Do Until iClearedTestLength = Len(sTextString)
iWordEndingPossition = CInt(InStr((Microsoft.VisualBasic.Right(sTextString, Len(sTextString) - iWordStartingPossition)), " "))
txtTextFromFile.Text = txtTextFromFile.Text & (Microsoft.VisualBasic.Mid(sTextString, iWordStartingPossition + 1, iWordEndingPossition)) & vbCrLf
iWordStartingPossition = iWordStartingPossition + iWordEndingPossition
iClearedTestLength = iClearedTestLength + iWordEndingPossition
Loop
FileReader.Close()
End Sub

File size in vb.net

In Form1 I have a Textbox1, in this textbox I have the location of a file "C:\folder\file.iso"
In the Form2 I want to get the file size of the file in Textbox1 so I tried this
Dim fileDetail As IO.FileInfo
fileDetail = My.Computer.FileSystem.GetFileInfo(Form1.Textbox1.Text)
Label1.Text = Size: fileDetail.Length
End Sub
I dont get an error, but the size of the file isn't showed in the label.
Edit: This doesn't seem to work
Private Sub Unscramble_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If System.IO.File.Exists(Form1.TextBox2.Text) Then
Dim fi As New System.IO.FileInfo(Form1.TextBox2.Text)
Label3.Text = "Size: " & fi.Length.ToString()
End If
End Sub
It still doesn't give me the size of the file nor it gives the "Size:"
Dim fileDetail = My.Computer.FileSystem.GetFileInfo(form1.Textbox1.Text)
Label1.Text = "Size : " & fileDetail.Length
' this is the first(main) form'
Public Class Form1
Private Sub Button1_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
' create the form2 by PASSING it the file path in constructor'
Dim f2 As New Form2(TextBox1.Text)
f2.ShowDialog()
End Sub
End Class
' this is the second form'
Public Class Form2
Inherits Form
Private _filePath As String
Private Label1 As Label
Public Sub New(ByVal filePath As String)
_filePath = filePath
End Sub
' this is the _Load method of the second form'
Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
MyBase.OnLoad(e)
If IO.File.Exists(_filePath) Then
Dim fi As New IO.FileInfo(_filePath)
Label1.Text = "Size :" & fi.Length.ToString()
End If
End Sub
End Class
Code works perfect but something in my project is blocking it.
Created a new project and it worked perfect.
'label3.Text is my all string with file size.
Label3.Text = "Size : " & My.Computer.FileSystem.GetFileInfo("C:\Download\my song.mp3").Length & " Bytes"
'Output: Size: 2344 Bytes
Label3.Text = "Size : " & System.Math.Round(My.Computer.FileSystem.GetFileInfo("C:\Download\my song.mp3").Length / 1024) & " KB"
'Output: Size: 2 KB
There is two choice, Which you want