I'm a beginner in C Sharp, and I'm using the VS community, so I found a web cam program and adapted the code where I want to use it here, but when I click on save image, it appears this error:
error object reference not set to an instance of an object
And it does not save, can someone help me? I already changed the path that saves but did not change anything.
Follow the code:
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Imaging;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public DirectX.Capture.Filter Camera;
public DirectX.Capture.Capture CaptureInfo;
public DirectX.Capture.Filters CamContainer;
Image capturaImagem;
public string caminhoImagemSalva = null;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
CamContainer = new DirectX.Capture.Filters();
try
{
int no_of_cam = CamContainer.VideoInputDevices.Count;
for (int i = 0; i < no_of_cam; i++ )
{
try
{
// Obtém o dispositivo de entrada de video
Camera = CamContainer.VideoInputDevices[i];
// inicializa e captura o dispostivo
CaptureInfo = new DirectX.Capture.Capture(Camera, null);
// Define a janela de visualização
CaptureInfo.PreviewWindow = this.picWebCam;
// Capturando o tratamento do evento
CaptureInfo.FrameCaptureComplete += AtualizaImagem;
// Captura o frame do dispositivo
CaptureInfo.CaptureFrame();
// Se o dispositivo foi encontrado sem checar o resto
break;
}
catch (Exception ex)
{
throw ex;
}
}
}
catch (Exception ex)
{
MessageBox.Show(this, ex.Message);
}
}
public void AtualizaImagem(PictureBox frame)
{
try
{
capturaImagem = frame.Image;
this.picImagem.Image = capturaImagem;
}
catch (Exception ex)
{
MessageBox.Show("Erro " + ex.Message);
}
}
private void btnCaptura_Click(object sender, EventArgs e)
{
try
{
CaptureInfo.CaptureFrame();
}
catch (Exception ex)
{
MessageBox.Show("Erro " + ex.Message);
}
}
private void btnSalvar_Click(object sender, EventArgs e)
{
try
{
caminhoImagemSalva = @"c:\dados\" + "ImagemWebCam" + DateTime.Now.Day.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Year.ToString() + DateTime.Now.Millisecond.ToString() + ".jpg";
picImagem.Image.Save(caminhoImagemSalva, ImageFormat.Jpeg);
MessageBox.Show("Imagem salva com sucesso");
}
catch(Exception ex)
{
MessageBox.Show("Erro " + ex.Message);
}
}
private void picWebCam_Click(object sender, EventArgs e)
{
}
private void pictureBox1_Click(object sender, EventArgs e)
{
}
}
}
breakpoint:
<?xml version="1.0" encoding="UTF-8"?>
-<BreakpointCollection xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
-<Breakpoints>
-<Breakpoint>
<Version>15</Version>
<IsEnabled>1</IsEnabled>
<IsVisible>1</IsVisible>
<IsEmulated>0</IsEmulated>
<IsCondition>0</IsCondition>
<ConditionType>WhenTrue</ConditionType>
<LocationType>SourceLocation</LocationType>
-<TextPosition>
<Version>4</Version>
<FileName>.\CapturaImagemWebCam\Form1.cs</FileName>
<startLine>93</startLine>
<StartColumn>16</StartColumn>
<EndLine>93</EndLine>
<EndColumn>75</EndColumn>
<MarkerId>0</MarkerId>
<IsLineBased>0</IsLineBased>
<IsDocumentPathNotFound>0</IsDocumentPathNotFound>
<ShouldUpdateTextSpan>1</ShouldUpdateTextSpan>
-<Checksum>
<Version>1</Version>
<Algorithm>00000000-0000-0000-0000-000000000000</Algorithm>
<ByteCount>0</ByteCount>
<Bytes/>
</Checksum>
</TextPosition>
<NamedLocationText>WindowsFormsApplication1.Form1.btnSalvar_Click(object sender, EventArgs e)</NamedLocationText>
<NamedLocationLine>6</NamedLocationLine>
<NamedLocationColumn>0</NamedLocationColumn>
<HitCountType>NoHitCount</HitCountType>
<HitCountTarget>1</HitCountTarget>
<Language>3f5162f8-07c6-11d3-9053-00c04fa302a1</Language>
<IsMapped>0</IsMapped>
<BreakpointType>PendingBreakpoint</BreakpointType>
-<AddressLocation>
<Version>0</Version>
<MarkerId>0</MarkerId>
<FunctionLine>0</FunctionLine>
<FunctionColumn>0</FunctionColumn>
<Language>00000000-0000-0000-0000-000000000000</Language>
</AddressLocation>
<DataCount>4</DataCount>
<IsTracepointActive>0</IsTracepointActive>
<IsBreakWhenHit>1</IsBreakWhenHit>
<IsRunMacroWhenHit>0</IsRunMacroWhenHit>
<UseChecksum>1</UseChecksum>
<Labels/>
<RequestRemapped>0</RequestRemapped>
<parentIndex>-1</parentIndex>
</Breakpoint>
</Breakpoints>
</BreakpointCollection>