Emgu.CV on VB.Net Slow PictureBox Image - vb.net

Using Emgu.CV Version 2.2.1.1150
I'm trying to query frames and display on picture box and the code works well on desktop computer with a Microsoft LifeCam 720p Picturebox is smooth and fluid but when I try the program in a Windows 10 tablet (CHUWI HI10X) the image is lagging.
If I test the tablet camera in the Win10 camera app they work very well and the video is HD and smooth.
I updated the drivers of these cameras to check if it was that but no luck.
Public Class frmCamera
Dim imgFrame As Image(Of Bgr, Byte)
Dim vidCapture As Capture
Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
vidCapture = New Capture(1)
vidCapture.QueryFrame()
AddHandler Application.Idle, AddressOf AppIdle
End Sub
Public Sub AppIdle(ByVal sender As System.Object, ByVal e As System.EventArgs)
imgFrame = vidCapture.QueryFrame().Resize(800, 600, Emgu.CV.CvEnum.INTER.CV_INTER_LINEAR)
PictureBox1.Image = imgFrame.ToBitmap()
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
frmMain.Show()
RemoveHandler Application.Idle, AddressOf AppIdle
vidCapture.Dispose()
Me.Close()
End Sub
End Class
I've tried multiple frame sizes and different interpolation types with no luck. Also changed from using a imageBoxFrameGrabber to PictureBox converting to Bitmap but still same results. Also tried using a Timer but that looks worse.
Thanks!
Edit:
Using a Timer my code ends up like this with no luck.
Public Class frmCamera
Dim imgFrame As Image(Of Bgr, Byte)
Dim vidCapture As Capture
Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Timer1.Interval = Timer1.Interval
RichTextBox1.Text = "Interval:" + Convert.ToString(Timer1.Interval)
Timer1.Start()
vidCapture = New Capture(1)
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
frmMain.Show()
vidCapture.Dispose()
Me.Close()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
imgFrame = vidCapture.QueryFrame().Resize(800, 600, Emgu.CV.CvEnum.INTER.CV_INTER_LINEAR)
' PictureBox1.Image.Dispose()
PictureBox1.Image = imgFrame.ToBitmap()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Timer1.Stop()
Timer1.Interval = Timer1.Interval + 2
RichTextBox1.Text = "Interval:" + Convert.ToString(Timer1.Interval)
Timer1.Start()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Timer1.Stop()
Timer1.Interval = Timer1.Interval - 2
RichTextBox1.Text = "Interval:" + Convert.ToString(Timer1.Interval)
Timer1.Start()
End Sub
End Class
Tried disposing the image in the picture box as suggested and end up with a:
System.NullReferenceException: 'Object reference not set to an instance of an object.'
At the moment of dispose.
Thanks again Jimi!

Related

StopWatch and Timer Accuracy

I'm using the following code to control a label to output a stopwatch. The buttons work, and the label is outputting the information mostly correctly, however it only update every so often and i would like for the label to update the information every millisecond.
Private SW As New Stopwatch
Dim timercount As Integer = 1 'The number of seconds
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
SW.Start()
Timer1.Start()
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Timer1.Enabled = False 'Stop the timer
timercount = 0 'Reset to 0 seconds
Label10.Text = "00:00:00.000"
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Dim ts As TimeSpan = SW.Elapsed
Label10.Text = ts.ToString("hh\:mm\:ss\.fff")
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
Timer1.enabled = False
End Sub
Any help is much appreciated.
You can set the timer to a reasonable value, say 20 times a second, to rapidly display it changing.
You can create the timer and stopwatch in code, which I think is easier than adding a control to the form—you can see right there in the code what is being programmed. I used sensible names for the buttons so that you can tell what is meant to do what.
If you keep the sub to display the data separate from the code that processes the data, it is easy to change the display in just one place, in case someone decided they wanted it written as words, for example, or maybe the hours in a separate box, or something.
Public Class Form1
Dim sw As New Stopwatch
Dim tim As Timer
Private Sub ShowElapsedTime()
lblTime.Text = sw.Elapsed.ToString("hh\:mm\:ss\.fff")
End Sub
Private Sub bnStart_Click(sender As Object, e As EventArgs) Handles bnStart.Click
sw.Restart()
tim.Enabled = True
End Sub
Private Sub bnStop_Click(sender As Object, e As EventArgs) Handles bnStop.Click
sw.Stop()
tim.Enabled = False
ShowElapsedTime()
End Sub
Private Sub bnReset_Click(sender As Object, e As EventArgs) Handles bnReset.Click
tim.Enabled = False
sw.Reset()
ShowElapsedTime()
End Sub
Private Sub tim_Tick(sender As Object, e As EventArgs)
ShowElapsedTime()
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
tim = New Timer With {.Interval = 50}
AddHandler tim.Tick, AddressOf tim_Tick
ShowElapsedTime()
End Sub
End Class

Sound effects play while audio background is playing Visual Basic

im currently new at making VB and i would like to ask if how to play sound effects while a background music is playing, for example, while a background music is playing, when i click the button it produces a sound effects. as far as i did, when i hover my mouse on the button, the current background music stops and the sound effect executes and after i hover my mouse, the background music plays agaian
here is my code..
Public Class Form2
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
My.Computer.Audio.Play("C:\Users\android_kh5sy35fe2\Desktop\WWM\auswahlrunde_loop.wav",
AudioPlayMode.BackgroundLoop)
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
End Sub
Private Sub Button1_MouseHover(sender As Object, e As EventArgs) Handles Button1.MouseHover
My.Computer.Audio.Play("C:\Users\android_kh5sy35fe2\Desktop\WWM\Hawking.wav",
AudioPlayMode.Background)
End Sub
Private Sub Button1_MouseLeave(sender As Object, e As EventArgs) Handles Button1.MouseLeave
My.Computer.Audio.Play("C:\Users\android_kh5sy35fe2\Desktop\WWM\auswahlrunde_loop.wav",
AudioPlayMode.BackgroundLoop)
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
End Sub
Private Sub Button2_MouseHover(sender As Object, e As EventArgs) Handles Button2.MouseHover
My.Computer.Audio.Play("C:\Users\android_kh5sy35fe2\Desktop\WWM\Finn.wav",
AudioPlayMode.Background)
End Sub
Private Sub Button2_MouseLeave(sender As Object, e As EventArgs) Handles Button2.MouseLeave
My.Computer.Audio.Play("C:\Users\android_kh5sy35fe2\Desktop\WWM\auswahlrunde_loop.wav",
AudioPlayMode.BackgroundLoop)
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
End Sub
Private Sub Button3_MouseHover(sender As Object, e As EventArgs) Handles Button3.MouseHover
My.Computer.Audio.Play("C:\Users\android_kh5sy35fe2\Desktop\WWM\Homer.wav",
AudioPlayMode.Background)
End Sub
Private Sub Button3_MouseLeave(sender As Object, e As EventArgs) Handles Button3.MouseLeave
My.Computer.Audio.Play("C:\Users\android_kh5sy35fe2\Desktop\WWM\auswahlrunde_loop.wav",
AudioPlayMode.BackgroundLoop)
End Sub
End Class
thanks for the replies.. cheers!
My.Computer.Audio.Play is unique (static).
You can use a SoundPlayer, that is a class, so you can instantiate it multiple times (for each sound).
Dim music As String = "" ' *.wav file location
Dim media As New Media.SoundPlayer(music)
media.Play() ' Async, creates a new thread
Dim sound As String = "" ' *.wav file location
Dim media As New Media.SoundPlayer(sound)
media.PlaySync() ' Sync, locks the current thread

How to make a picturebox hide when dragged over another picturebox?

So I nailed the ability to drag a picturebox around the Windows form. I need it to hide itself when it's dragged and dropped over another picture. I've tried a few methods but none seem to work and I am now back at square one, the only code I have is so that I can move the picturebox around the form.
I Solved Your Problem
Here Is How The Form Should Look Like
And Here Is The Code I Made:
Private Sub CheckTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckTimer.Tick
CheckTimer.Stop()
CheckTimer.Interval = 1
If PictureBox2.Location = New System.Drawing.Point(TextBox1.Text, TextBox2.Text) Then
PictureBox2.Visible = False
End If
CheckTimer.Start()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TextBox1.Text = PictureBox1.Location.X
TextBox2.Text = PictureBox1.Location.Y
CheckTimer.Start()
End Sub
Hope This Code Was Helpful To You.
I made an better code than the old one, try it.
Here Is How The Form Should Look Like:
and here is the code :
Public Class Form1
Dim Point As New Point
Dim X, Y As Integer
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Timer1.Stop()
Timer1.Interval = 1
Point = Cursor.Position
PictureBox2.Location = New System.Drawing.Point(Point.X - X, Point.Y - Y)
Timer1.Start()
End Sub
Private Sub PictureBox2_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox2.MouseDown
X = Cursor.Position.X - PictureBox2.Location.X
Y = Cursor.Position.Y - PictureBox2.Location.Y
Timer1.Start()
End Sub
Private Sub PictureBox2_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox2.MouseUp
Timer1.Stop()
If PictureBox2.Location.X < PictureBox1.Size.Width Then
PictureBox2.Visible = False
End If
End Sub
End Class
I hope this code was useful to you.

Button Click Event Within Another

I have two buttons Start button and Stop button .I run my program by clicking on start button. I want to stop the program during the start button . But the program will not responde until the start buttun finish its job. How i can do check the stop buttun during the start. i heard about threading but i do not know how i can do it.
Private Sub Button_Start (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
//my code
//check always if the user push stop if no continue if yes go to this sub
//my code
end sub
Private Sub Button_Stop (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
stopClick = True
Dim Response As Integer
Response = MessageBox.Show("Do you really want to exit?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If Response = vbYes Then
Me.Close()
End If
End Sub
you can use threading put button1 code in a function and use the thread .
you can refer to this example
'Thread created to handle the Background process for start_function
Dim t As System.Threading.Thread
Private Sub start_function()
While True
'your code here for Eg:
Dim i As Integer
i = i + 1
End While
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
t = New System.Threading.Thread(AddressOf Me.start_function)
t.Start()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
t.Abort()
End Sub
Drag a backgroundworker component onto your form.
Imports System.ComponentModel
Public Class Form1
Private Sub btnStart_Click(sender As Object, e As EventArgs) Handles btnStart.Click
Me.Text = "Busy Doing Work"
BackgroundWorker1.WorkerSupportsCancellation = True
BackgroundWorker1.RunWorkerAsync()
End Sub
Private Sub btnStop_Click(sender As Object, e As EventArgs) Handles btnStop.Click
Me.Text = "Asking to Cancel"
BackgroundWorker1.CancelAsync()
End Sub
Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
While Not BackgroundWorker1.CancellationPending
System.Threading.Thread.Sleep(1000)
End While
End Sub
Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
Me.Text = "Cancelled"
End Sub
End Class

Refresh or Redraw picturebox in VB.net

I'm trying to view youtube captcha in my vb application but I don't know how to refresh/redraw the picture box. The youtube captcha is in http://www.youtube.com/cimg and the captcha is changing everytime I refresh the page. How can I do that using button or timer in vb.net to change the captcha. Here is the code I used to load the captcha to picturebox.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
captchaBox.ImageLocation = "http://www.youtube.com/cimg"
End Sub
i try to use captchaBox.Refresh() but it's not working. I hope someone can help me. thx
Refresh just redraws the control, it doesn't retrieve the image again. You need to reset the ImageLocation property for it to work. For example with a timer:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
captchaBox.ImageLocation = "http://www.youtube.com/cimg"
Timer1.Enabled = True
Timer1.Interval = 1000
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
captchaBox.ImageLocation = "http://www.youtube.com/cimg"
End Sub
Try this:
Private Sub Form1_Load() Handles MyBase.Load
Timer1.Start()
PictureBox1.ImageLocation = "http://www.youtube.com/cimg"
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
PictureBox1.Image = Nothing
PictureBox1.ImageLocation = "http://www.youtube.com/cimg"
End Sub