Cant view webcam after being ran once. C# - c#

the title may sound confusing but ill explain it better here. Im making a program which displays the webcam capture in a picturebox using the "easy web cam" external reference. If i turn on my computer, go into VS, open the project and run, it will work, displaying my webcam capture. If i stop the program and then run it again, when i try to display it, i get a popup asking me to select a video source, none of the options is even my webcam and then another popup will appear saying
"An error ocurred while capturing the video image. The video capture will now be terminated.
Object reference not set to ann instance of an object"
The only thing i can think of is that the first time its setting up the camera but when i close it im not turning it off properly so when i run it again, it wont work. anyway heres the relevant code, bare in mind if answering, im not that experienced when coding so sometimes you might have to spell stuff out
using WebCam_Capture;
namespace WindowsWebRef
{
public partial class Frm_Main : Form
{
public Frm_Main()
{
InitializeComponent();
}
WebCam webcam;
private void button1_Click(object sender, EventArgs e)
{
webcam.Start();
}
private void Frm_Main_Load(object sender, EventArgs e)
{
webcam = new WebCam();
webcam.InitializeWebCam(ref WebCamIMG);
}
And the webcam class...
class WebCam
{
private WebCamCapture webcam;
private System.Windows.Forms.PictureBox _FrameImage;
private int FrameNumber = 30;
public void InitializeWebCam(ref System.Windows.Forms.PictureBox ImageControl)
{
webcam = new WebCamCapture();
webcam.FrameNumber = ((ulong)(0ul));
webcam.TimeToCapture_milliseconds = FrameNumber;
webcam.ImageCaptured += new WebCamCapture.WebCamEventHandler(webcam_ImageCaptured);
_FrameImage = ImageControl;
}
void webcam_ImageCaptured(object source, WebcamEventArgs e)
{
_FrameImage.Image = e.WebCamImage;
}
public void Start()
{
webcam.TimeToCapture_milliseconds = FrameNumber;
webcam.Start(0);
}

Restart your computer and be sure to add in webcam.Stop(); otherwise your program will hold on to the webcam and make it unavailable to use in any other application (or a different instance of the program.)

Related

c# VLC.Dotnet play and then pause issue

I hope someone will be able to help me with my issue. i want to take a screenshot of the video at a specific time index, however when i try to change the time all i get is a blank black screen. i added buttons which play and pause, it allows me to play and pause the video, if i do that and then change the time index, i get an image. im confused as to why it doesn’t work using code. i even preform btnplay.PeformClick(); to play the video and when i do btnpause.PerformClick() to pause the video it doesn’t.
it seems that i can only get an image of the video if i have to physically hit the play and then pause button on my form, im trying to achieve this using code
private void Form1_Load(object sender, EventArgs e)
{
////////////////////LC4 VLC Settings///////////////////////////////////////////////////////////////////////////////////////////
control = new VlcControl();
var currentAssembly = Assembly.GetEntryAssembly();
var currentDirectory = new FileInfo(currentAssembly.Location).DirectoryName;
var libDirectory = new DirectoryInfo(Path.Combine(currentDirectory, "libvlc", IntPtr.Size == 4 ? "win-x86" : "win-x64"));
control.BeginInit();
control.VlcLibDirectory = libDirectory;
control.Dock = DockStyle.Fill;
control.EndInit();
panel1.Controls.Add(control);
main_form_LC4_data();
}
void main_form_LC4_data()
{
long vOut3 = 20;
playfile("path to file");
First_Frame(vOut3);
}
void playfile(string final)
{
control.SetMedia(new Uri(final).AbsoluteUri);
control.Time = 0;
control.Update();
}
void First_Frame(long vOut3)
{
control.Time = vOut3;
}
private void button9_Click(object sender, EventArgs e)
{
control.Play();
Console.WriteLine("PLAY");
}
private void button8_Click(object sender, EventArgs e)
{
control.Pause();
Console.WriteLine("PAUSE");
}
Above is my code in a nut shell
i have tried things like this
private void button10_Click(object sender, EventArgs e)
{
First_Frame(first_frame); // jump to index
}
and then calling up button10.PerformClick(); however it doesnt seem to work. once again if i physically hit the buttons on my form it works perfectly, however not in the way of coding it.
as an example :
play.PeformClick();
Pause.PeformClick();
time = vOut3;
I do hope this isnt to confusing im really stuck and am still hoping someone can help me
Thank you
A few things:
control.Update();
this does nothing.
You need to wait for the Playing event to be raised after setting the time, otherwise libvlc doesn't have the time to decode the frame and display it (setting the time is asynchronous)

How to paste clipboard data to another application in c# windows form

I have a form where i have copied a text in clipboard, now my requirement is that after setting clipboard data if i click in inside another application like notepad
data gets pasted there.
private void ListView1_EPC_Click(object sender, EventArgs e)
{
string str = ListView1_EPC.SelectedItems[0].SubItems[1].Text;
Clipboard.SetText(str);
}
thanks in advance.
That is possible: You can attach to the system wide mousedown. This has already been implemented in C#, have a look here... https://www.codeproject.com/Articles/7294/Processing-Global-Mouse-and-Keyboard-Hooks-in-C
This project provides a simple event for a global mouse down (GlobalEventProvider component). What you then need to do is to send a ctrl-v using SendKeys, when this mouse down has been triggered...
public class ForeignWindowPasteHelper
{
private readonly GlobalEventProvider eventProvider = new GlobalEventProvider();
public ForeignWindowPasteHelper()
{
this.eventProvider.MouseClick += this.GlobalMouseDown;
}
private void GlobalMouseDown(object sender, MouseEventArgs e)
{
SendKeys.Send("^{v}");
}
}
This will trigger a Cntrl+V (Insert) on every keydown. You will need to implement some logic, when this needs to be triggered. But thats not hard.

bringing a windows form, form to the front without minimising other windows

Hi I'm relatively new to C# and completely new to windows form and basically trying to create a subliminal messaging program that at timed intervals will quickly display a message for it to then be hidden again.
I've managed to by looking through various other posts created another form that will pop up and then hide very quickly using
msgObject.Activate();
that brings the form to the front. However it is stopping me from being able to type when I'm working and I basically wanting to know if it is possible to make some kind of message or form appear at the front of all my other programs without it interrupting my current work or opening or closing of other windows and tasks if that makes sense. As currently it brings the form to the front of everything but will also stop me from being able to type etc.
I'm not sure if this is possible with my current method of using a form but if there is a way of achieving the result I'm after I'd be very grateful to find out
Thanks.
Here is more of my code to clarify
public partial class FormHomePage : Form
{
private bool startSubliminal = false;
msg msgObject = new msg();
List<string> subliminalMessages = new List<string>();
public FormHomePage()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void timer1_Tick(object sender, EventArgs e)
{
if (startSubliminal)
{
msgObject.Show();
msgObject.BringToFront();
msgObject.Activate();
}
}
private void button1_Click(object sender, EventArgs e)
{
subliminalMessages.Add(txtBox1.Text);
msgObject.LabelText = txtBox1.Text;
txtBox1.Text = string.Empty;
startSubliminal = true;
msgObject.Show();
msgObject.BringToFront();
}
private void timer2_Tick(object sender, EventArgs e)
{
msgObject.Hide();
}
}
How are you showing the second form (the message form) in the first place? You're probably using .Show() (right?), which will make the form "steal" the focus anyway. The same applies to .Activate() and .BringToFront().
Instead, what you can do is to show the message form and make sure it stays on top the current form, and then activate the current/main form once again.
Something like this should work:
var frm = new YourMsgForm();
frm.Show(this);
this.Activate();
Here's a demonstration:
Note that I used .Show(this) instead of .Show(), that's in order to set the current form as the Owner of the new one, that way we guarantee that the new form will stay on top of the current one. This is equivalent to calling frm.Owner = this; then frm.Show();.
Another way to make sure the form stays on top is by setting the TopMost property instead of the Owner property. However, doing so will make the new form on top of the other windows as well (not just your application).

How to capture image with webcam?

I want to capture image with webcam.
I use WebCam_Capture.dll. i add a WebCamCapture control in form.
when form load:
private void Form1_Load(object sender, EventArgs e)
{
this.WebCamCapture.TimeToCapture_milliseconds = 1;
WebCamCapture.Start(0);
}
and in ImageCaptured event :
private void WebCamCapture_ImageCaptured(object source,
WebCam_Capture.WebcamEventArgs e)
{
this.pictureBox1.Image = e.WebCamImage;
}
but when i run, i get error:
An error ocurred while capturing the video image. The video capture will now be terminated.
Object reference not set to an instance of an object.
I recommend you to use OpenCV Library.
it is an open source library for image proccessing from intel, developed by intel.
but, it has a wrapper for .net, you can download this wrapper from : this link

C# Splash screen not working

I've been stuck here for a few hours looking up code for hours and making batches of my own. One of the code ex I got from here gave me a instant svHost error.
Now my splash screen is my HWID / Serial check, if valid it opens the main form if not it closes the app completely.. But the issue is I've tried some way's with splash.show(); on the main form but it just freezes and goes all stupid for a few minutes, another thing most of the methods use timers I just need it so when HWID check is valid I can start the main form, I've tried application.Run(new mainForm()); if the HWID was correct then changed the program.cs file around but still no luck, I really need the help. It would be wonderful, thank's.
I've used splash screen animation while I'm connecting to socket. I've used backgroundworker for it.
Here's my code:
Main Form
LoadingScreen frmLoadingScreen = new LoadingScreen();
.....
bkwNetworkConnector.RunWorkerAsync();
frmLoadingScreen.ShowDialog();
/***********************************************************************************************************************/
private void bkwNetworkConnector_DoWork(object sender, DoWorkEventArgs e)
{
try
{
hostSocket = new TcpClient();
hostSocket.Connect(strIp, intPort);
}
catch (Exception exp)
{
}
}
private void bkwNetworkConnector_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
frmLoadingScreen.Close();
}
LoadingScreen Form:
with imagebox control. Load GIF image in it.
private void LoadingScreen_Load(object sender, EventArgs e)
{
pbAnimationBox.Image = Properties.Resources.LoadingAnimation; // win 8 animation
}
I'm using LoadingScreen Form as splash screen.
Hope this will hep you....

Categories