VB.NET - cannot get Video Preview to work - vb.net

Overflow. I have an application which is supposed to convert a video via DirectShowSource.
I have a checkbox which enables a button called "btnviewcrop" which shows a new form called crp.vb for cropping/shearing pixels off the video.
Now, I have a Panel1 that I want to set as the owner (the video drawing surface) but when I set it to open, the application crashes (Error: Object reference not set to an instance of an object.) and I do not understand how to fix it.
Here is my button code:
Imports Microsoft.DirectX.AudioVideoPlayback
Private Sub btnviewcrop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnviewcrop.Click
Me.ShowInTaskbar = False
Me.Enabled = False
crp.Show()
Dim cropperv As Video
cropperv.Owner = crp.preview 'VS2010 reports that cropperv has been used before being assigned a value'
cropperv.FromFile(labinputfile.Text, True)
cropperv.Play()
End Sub

I don't think you ever assign anything to cropperv. Did you mean
Dim cropperv As New DirectX.AudioVideoPlayback

Good point, and if I do this:
Dim cropperv As New Video(labinputfile.Text, True)
cropperv.Owner = crp.preview
cropperv.Play()
Then all works out fine besides the resizing needs done.
I appreciate the help, Matti.

Related

VB.net PrintForm Not Working in New Thread

I am developing a e-filing app and I need to print an adhesive label with some info to attach to the physical folder.
I already designed the label as a Form put the logo and everything that I need there. Then on the Form.Shown event I put the command to print:
Me.PrintLabelForm.Print() (This is VisualStudio PowerPack Control)
And here is where I bump into a problem. The print out is totally empty (I already changed margins setup the printer, etc). The issue is that the form is not actually fully loaded, I switch the method to the print preview and the controls are there but they are empty.
I tried several approaches but I have been not able to do this automatically. One solution that I found was to have a button to do the Me.PrintLabelForm.Print() then it works because the form is already fully loaded and displayed but this is not an option. I need the form to open automatically, print and close.
An option that I think it should work will be to have a new thread with a timer then printing so I did this:
Private Sub LabelPrint_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub LabelPrint_Shown(sender As Object, e As EventArgs) Handles Me.Shown
PrintLabelForm.PrinterSettings.DefaultPageSettings.Margins.Left = 0.1
PrintLabelForm.PrinterSettings.DefaultPageSettings.Margins.Right = 0.1
PrintLabelForm.PrinterSettings.DefaultPageSettings.Margins.Top = 0.1
PrintLabelForm.PrinterSettings.DefaultPageSettings.Margins.Bottom = 0.1
PrintLabelForm.PrinterSettings.DefaultPageSettings.Landscape = True
Dim PrintThread As New System.Threading.Thread(AddressOf PrintSub)
PrintThread.Start()
End Sub
Private Sub PrintSub()
Threading.Thread.Sleep(1000)
Me.PrintLabelForm.Print()
Me.Close()
End Sub
The idea was to have the PrintSub to give the app enough time to finish to render the whole thing then print but I am getting this error:
**An unhandled exception of type 'System.Exception' occurred in Microsoft.VisualBasic.PowerPacks.dll
Additional information: The window being printed must be visible and contain focus.**
So I wonder how to make this thread have the window form in focus in order to be able to print.
That is all. Thanks for all the help.
Always work with the form only from main thread.
You found it right – form printing will not run from new thread.
When you do any actions on forms, you must perform all the work from Dispatcher thread. It is the thread on which all event methods run. If you fail doing so, you can encounter many side effects. (Not only problem with printing. I've been there and this advice from senior Windows programmer helped me to get things back to normal.) So do not use form printing from any other thread.
If you want a workaround for this, print form to the image (in main thread) and then you can print the image using new thread.
This has nothing to do with .NET, this is related to internals of Windows Forms technology. Welcome to Windows programming.
I manage to solve it putting this line in the Form.Shown
PrintLabelForm.Print(Me, PrintForm.PrintOption.ClientAreaOnly)
I don't know why or how but it works.
Thanks to all of you guys for your help. Let's hope I don't find myself trying to do stuff when the form is fully displayed.
This is my full code let's hope it works for someone else:
Imports Microsoft.VisualBasic.PowerPacks.Printing
Public Class PrintAdhesiveLabel
Private Sub LabelPrint_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub LabelPrint_Shown(sender As Object, e As EventArgs) Handles Me.Shown
PrintLabelForm.PrinterSettings.DefaultPageSettings.Margins.Left = 0.1
PrintLabelForm.PrinterSettings.DefaultPageSettings.Margins.Right = 0.1
PrintLabelForm.PrinterSettings.DefaultPageSettings.Margins.Top = 0.1
PrintLabelForm.PrinterSettings.DefaultPageSettings.Margins.Bottom = 0.1
PrintLabelForm.PrinterSettings.DefaultPageSettings.Landscape = True
PrintLabelForm.Print(Me, PrintForm.PrintOption.ClientAreaOnly)
Me.Close()
End Sub
End Class
Perhaps this is relevant:
Only the form that currently has focus can be printed by using this
method. If you have set the Form property to another form before
calling this method, the image of the form may not be rendered as
expected. To avoid this, call the Focus method of the form before you
call Print.
So call Me.PrintLabelForm.Focus() before calling Me.PrintLabelForm.Print():
Private Sub PrintSub()
Threading.Thread.Sleep(1000)
Me.PrintLabelForm.Focus()
Me.PrintLabelForm.Print()
Me.Close()
End Sub

Cefsharp download window for showing progress in VB.Net

I am creating a browser using Cefsharp in VB.Net, and I have been trying to create a download window that shows the progress of the current download. I don't know if I am doing something wrong or if it is the way CEF works, but I put in a download handler by adding browser.DownloadHandler = New DownloadHandler to Form1_Load and creating a new class like this (with downloading being the form I created for showing the progress):
Public Class DownloadHandler
Implements IDownloadHandler
Public Function OnBeforeDownload(downloadItem As DownloadItem, ByRef downloadPath As String, ByRef showDialog As Boolean) As Boolean Implements IDownloadHandler.OnBeforeDownload
downloadPath = downloadItem.SuggestedFileName
showDialog = True
downloading.Show()
Return True
End Function
Public Function OnDownloadUpdated1(downloadItem As DownloadItem) As Boolean Implements IDownloadHandler.OnDownloadUpdated
My.Settings.downloadpercent = downloadItem.PercentComplete.ToString
Return False
End Function
End Class
On the downloading form I have this code for showing the progress:
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Dim percentcomplete As Integer = My.Settings.downloadpercent * 5
Me.PictureBox1.Size = New Size(percentcomplete, 25)
End Sub
Private Sub downloading_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Timer1.Start()
End Sub
This may not be the best way to show a ProgressBar, but I have a picture box that just has a green bar, and the total width of the form is 500px. The code is telling it to read the PercentComplete setting put in My.Settings.downloadpercent, multiply it by 5, so when the progress in 100%, it will go across the whole form.
The problem is that the ProgressBar is not being updated to show the current progress. It goes a little bit, but then it just stops. Am I doing something wrong, or is OnDownloadUpdated not a good place to put that? Any suggestions of how to fix this?
Edit:
I am using CefSharp 39.0.0-pre03. Also, when the save file dialog comes up, no matter if you click Save or Cancel, the browser always triggers a LoadError, so it loads the custom HTML error page I made, and since it requires a URL for loading HTML, I put in "http://rendering/"... So I guess that would be a domain change. That issue (in the comments) could be the problem, but then we also need to figure out why it is triggering a LoadError.
A simple solution that may be of interest to those who faced the same or similar problem:
Declare Public your Picturebox, in a module:
Module Module1
Public PictureBox1 As New PictureBox
End Module
In MainForm you can declare the attributes you want (after the InitializeComponent). Example:
With PictureBox1
.BorderStyle = Border3DStyle.Flat
.Image = Bitmap.FromFile(YourImageFileName)
'......
End With
In the DownloadHandler Class:
Public Function OnDownloadUpdated1(downloadItem As DownloadItem) As Boolean
Implements IDownloadHandler.OnDownloadUpdated
Dim percentcomplete As Integer=downloadItem.PercentComplete * 5
PictureBox1.Size = New Size(percentcomplete, 25)
Return False
End Function

Using animated gif in splash screen vb.net

I'm having trouble creating a .gif be animated in my already created visual studio 2013 project.
Been looking online a bit for the "proper" way to do it. Following this MSDN question: Link I copied some of the code to see how it would work. I've also seen references to using a timer.
Public Not Inheritable Class SplashScreen
Private progressGifPath As String = "C:\Documents\Visual Studio 2013\Projects\CameraFinder\icons" + "\orangeLoading.gif"
Private _AnimatedGif As New Bitmap(progressGifPath)
Private m_IsAnimating As Boolean
Public Property IsAnimating() As Boolean
Get
Return m_IsAnimating
End Get
Set(ByVal value As Boolean)
m_IsAnimating = value
End Set
End Property
Private Sub VICameraFinderLoading_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'Set up the dialog text at runtime according to the application's assembly information.
If My.Application.Info.Title <> "" Then
Else
End If
End Sub
Private Sub PlayGIF()
If Not m_IsAnimating Then
ImageAnimator.Animate(_AnimatedGif, New EventHandler(AddressOf Me.OnFrameChanged))
m_IsAnimating = True
End If
End Sub
Private Sub OnFrameChanged(ByVal o As Object, ByVal e As EventArgs)
If m_IsAnimating Then
ImageAnimator.UpdateFrames()
Dim aGraphics As Graphics = PictureBox.CreateGraphics
aGraphics.DrawImage(_AnimatedGif, New Point(30, 30))
aGraphics.Dispose()
End If
End Sub
End Class
So I tried this, but I don't see anything on the picture box drawn (I confirmed to see it was brought to the front). And apparently there can be a size limit to the gif included. Any ideas on what would be the better way to include it?
Thanks!
Figured it out. I hadn't set the image of the picture box to the gif.
EDIT:
To clarify, the code above wasn't the problem at all. I actually didn't need it in any way. I removed it from the splash screen class, completely. The solution was to go into my picture box's property (F4) and set the backgroundImage to the .gif's filepath (thus importing it as an embedded resource). Doing so has allowed for animation in the splash screen.

Auto Login Procedure (Hide Form) vb.NET Windows Forms

Okay, I am having a bit of trouble here. I am creating a log in window for an application, but I am trying to get the application to automatically log in (i.e. perform the functions that happen when the user logs in) when it starts, without showing the log in screen, if the settings already have a stored email and password. I have a notification System Tray Icon that shows when the app is running, and when the form is not visible, a balloon notification pops up so the user knows that it is still running, and click on the icon to open the log in screen.
Take a look at the following code. I know that this If Not event is being called and working correctly, because it performs everything inside the statement EXCEPT hiding the form. Why does it not change to invisible? I also tried Me.Hide, and same issue. The Balloon Notification pops up, the text boxes fill with the previously stored data...but the form stays visible...
Private Sub RadFrmLogin_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'Checks settings to see if email and password have already been stored and enters them into text fields, proceeds to automatically update access list
If Not String.IsNullOrEmpty(My.Settings.Email) And Not String.IsNullOrEmpty(My.Settings.Password) Then
TxtEmail.Text = My.Settings.Email
TxtPassword.Text = My.Settings.Password
Me.Visible = False
'Displays Balloon Tip
ntfySystemTrayIcon.ShowBalloonTip(800)
End If
End Sub
As an added note, I added a test button to hide the form, and it works perfectly:
Private Sub BtnHide_Click(sender As Object, e As EventArgs) Handles BtnHide.Click
'Hides form(for testing notification tray icon and balloon tip
Me.Visible = False
ntfySystemTrayIcon.ShowBalloonTip(1000)
End Sub
(removed my stupid default debug instructions since they did not help at all)
Update
okay, so there were similar questions before, take a look here: C#/.NET - WinForms - Instantiate a Form without showing it
short explanation: usually something like form1.show is used, so it is always changed to visible = true after the form_load is finished.
Either use the instructed event form_shown and add the visible=false
or another user recommended to change start properties to minimized and activate to hide program in taskbar. This helps to prevent that annoying flickering. I guess after that you can change the options back.
Update 2 The following seems to work well:
Private _IsVisible As Boolean
Public Property IsVisible() As Boolean
Get
Return _IsVisible
End Get
Set(ByVal value As Boolean)
_IsVisible = value
If _IsVisible Then
Me.WindowState = FormWindowState.Normal
Me.ShowInTaskbar = True
Me.Visible = True
Me.Activate()
Else
Me.WindowState = FormWindowState.Minimized
Me.ShowInTaskbar = False
Me.Visible = False
End If
End Set
End Property
If you want to get rid of the small taskbar flickering, then change the forms property showInTaskbar. When it is changed during the form_load, then there seem to be a short movement at the taskbar.
And to make it perfect, in form.Shown add following code:
Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
Me.Visible = IsVisible
End Sub
now it is enough to use
IsVisible = False
in form_Load, or if you want to show it
IsVisible = True
Just some ideas:
If all your tasks are completed in the _Load event try just calling End. Of course that would remove your tray icon as well.
Another possibility is to call Me.Visible in the _Shown event. This may cause a flash on the screen. If so perhaps you could position the form off the screen in _Load.

Capture New Window in WebBrowser Control VB.NET

I've been searching a bit and I haven't been able to exactly find what I need. I need to contain a popup window within the WebBrowser control in VB.NET
I found this project: http://www.codeproject.com/KB/cpp/ExtendedWebBrowser.aspx
But I've been having trouble parsing out what I need from it. It looks like it implements what I need, but I'm not really sure how the heck it's doing it. I just need to capture a popup and display it in a new WebBrowser object.
Private Sub WebBrowser1_NewWindow(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles WebBrowser1.NewWindow
Dim myelement As HtmlElement = WebBrowser1.Document.ActiveElement
Dim target As String = myelement.GetAttribute("href")
Dim newinstance As New WebBrowser
newinstance.Show()
newinstance.Navigate(target)
e.Cancel = True
End Sub