I can not find the error of this code when saving the image of webCam, whenever I click save as and type the file name it returns me with this error
using System;
using System.Drawing;
using System.Windows.Forms;
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;
#pragma warning disable CS0162 // Unreachable code detected
for (int i = 0; i < no_of_cam; i++)
#pragma warning restore CS0162 // Unreachable code detected
{
try
{
// obtém o dispositivo de entrada do vídeo
Camera = CamContainer.VideoInputDevices[i];
// inicializa a Captura usando o dispositivo
CaptureInfo = new DirectX.Capture.Capture(Camera, null);
// Define a janela de visualização do vídeo
CaptureInfo.PreviewWindow = this.picWebCam;
// Capturando o tratamento de evento
CaptureInfo.FrameCaptureComplete += AtualizaImagem;
// Captura o frame do dispositivo
CaptureInfo.CaptureFrame();
// Se o dispositivo foi encontrado e inicializado então sai sem checar o resto
break;
}
catch (Exception ex)
{
throw ex;
}
}
{
this.TopMost = false;
this.FormBorderStyle = FormBorderStyle.None;
this.WindowState = FormWindowState.Maximized;
}
}
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, System.EventArgs e)
{
try
{
//abre a opção de salvar como, para selecionar a pasta
SaveFileDialog saveFileDialog1 = new SaveFileDialog
{
Filter = "JPeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif",
Title = "Salvar o arquivo de imagem",
InitialDirectory = @"C:\Users\ssbfci\Documents\FOTOS DO FERRAMENTAL\"
};
saveFileDialog1.ShowDialog();
// se o nome do arquivo não for vazio, abre para salvar
if (saveFileDialog1.FileName != "")
{
// salva a imagem por fileStream
System.IO.FileStream fs =
(System.IO.FileStream)saveFileDialog1.OpenFile();
// Salva a imagem no formato certo
switch (saveFileDialog1.FilterIndex)
{
case 1:
this.btnSalvar.Image.Save(fs,
System.Drawing.Imaging.ImageFormat.Jpeg);
break;
case 2:
this.btnSalvar.Image.Save(fs,
System.Drawing.Imaging.ImageFormat.Bmp);
break;
case 3:
this.btnSalvar.Image.Save(fs,
System.Drawing.Imaging.ImageFormat.Gif);
break;
}
fs.Close();
}
}
catch (Exception ex)
{
MessageBox.Show("Erro " + ex.Message);
}
}
private void pictureBox1_Click(object sender, EventArgs e)
{
}
}
}
Designer
namespace WindowsFormsApplication1
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
this.picWebCam = new System.Windows.Forms.PictureBox();
this.picImagem = new System.Windows.Forms.PictureBox();
this.btnCaptura = new System.Windows.Forms.Button();
this.btnSalvar = new System.Windows.Forms.Button();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.textBox1 = new System.Windows.Forms.TextBox();
((System.ComponentModel.ISupportInitialize)(this.picWebCam)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.picImagem)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
this.SuspendLayout();
//
// picWebCam
//
this.picWebCam.BackColor = System.Drawing.Color.White;
this.picWebCam.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.picWebCam.Location = new System.Drawing.Point(172, 133);
this.picWebCam.Margin = new System.Windows.Forms.Padding(4);
this.picWebCam.Name = "picWebCam";
this.picWebCam.Size = new System.Drawing.Size(715, 503);
this.picWebCam.TabIndex = 0;
this.picWebCam.TabStop = false;
//
// picImagem
//
this.picImagem.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.picImagem.Location = new System.Drawing.Point(1021, 133);
this.picImagem.Margin = new System.Windows.Forms.Padding(4);
this.picImagem.Name = "picImagem";
this.picImagem.Size = new System.Drawing.Size(697, 503);
this.picImagem.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
this.picImagem.TabIndex = 1;
this.picImagem.TabStop = false;
//
// btnCaptura
//
this.btnCaptura.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.btnCaptura.Location = new System.Drawing.Point(237, 668);
this.btnCaptura.Margin = new System.Windows.Forms.Padding(4);
this.btnCaptura.Name = "btnCaptura";
this.btnCaptura.Size = new System.Drawing.Size(581, 55);
this.btnCaptura.TabIndex = 2;
this.btnCaptura.Text = "Capturar Imagem";
this.btnCaptura.UseVisualStyleBackColor = true;
this.btnCaptura.Click += new System.EventHandler(this.btnCaptura_Click);
//
// btnSalvar
//
this.btnSalvar.Click += new System.EventHandler(this.btnSalvar_Click);
this.btnSalvar.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.btnSalvar.Location = new System.Drawing.Point(1080, 668);
this.btnSalvar.Margin = new System.Windows.Forms.Padding(4);
this.btnSalvar.Name = "btnSalvar";
this.btnSalvar.Size = new System.Drawing.Size(581, 55);
this.btnSalvar.TabIndex = 3;
this.btnSalvar.Text = "Salvar Imagem";
this.btnSalvar.UseVisualStyleBackColor = true;
//
// pictureBox1
//
this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
this.pictureBox1.Location = new System.Drawing.Point(378, 730);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(1065, 310);
this.pictureBox1.TabIndex = 4;
this.pictureBox1.TabStop = false;
//
// textBox1
//
this.textBox1.BackColor = System.Drawing.SystemColors.ActiveCaption;
this.textBox1.BorderStyle = System.Windows.Forms.BorderStyle.None;
this.textBox1.Font = new System.Drawing.Font("Times New Roman", 48F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.textBox1.ForeColor = System.Drawing.Color.Red;
this.textBox1.Location = new System.Drawing.Point(515, 29);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(893, 74);
this.textBox1.TabIndex = 5;
this.textBox1.Text = "CAIXA DE PEÇA SEPARADA";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.SystemColors.ActiveCaption;
this.ClientSize = new System.Drawing.Size(1864, 1062);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.pictureBox1);
this.Controls.Add(this.btnSalvar);
this.Controls.Add(this.btnCaptura);
this.Controls.Add(this.picImagem);
this.Controls.Add(this.picWebCam);
this.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.Margin = new System.Windows.Forms.Padding(4);
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Capturando Imagem da WebCam";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.picWebCam)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.picImagem)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.PictureBox picWebCam;
private System.Windows.Forms.PictureBox picImagem;
private System.Windows.Forms.Button btnCaptura;
private System.Windows.Forms.Button btnSalvar;
private System.Windows.Forms.PictureBox pictureBox1;
private System.Windows.Forms.TextBox textBox1;
}
}